1

I'm getting this error in Typescript 4.9. I understand why I'm getting the error but I'm not sure how to get around it. I've looked at nullish coalescing but that gives more errors. The parameter will always be a two-dimensional array with one or more sets of data.

private CreatePlots(data: [] ) {
    let valData = data[0][0];
    let plotInfo = <EpmsPlotQueryData>data[0][1];
    GUI.setPopupMsg("Loading query data " + this._processResults + " of " + this._PlotQueryList.length);
    plotInfo.createPlots(valData);
}

The error I'm getting for both references to data; enter image description here

enter image description here

I can change the function to have data defined as any to get past the errors i.e.

private CreatePlots(data)

but I would like to understand why I can't use an empty array like you can in javascript.

1 Answer 1

2

If you know the exact type of the parameter, you should declare it like this:

data: Array<[string, EpmsPlotQueryData]> or data: [string, EpmsPlotQueryData][] (both are the same), which is an array of tuples.

private CreatePlots(data: Array<[string, EpmsPlotQueryData]>) {
    let valData = data[0][0];
    let plotInfo = data[0][1];
    GUI.setPopupMsg("Loading query data " + this._processResults + " of " + this._PlotQueryList.length);
    plotInfo.createPlots(valData);
}

As for why you get the error:

  • data: any[] this is an array of anything
  • data: [number, string] this is a tuple, you are telling the compiler that data[0] is always number, data[1] is always string, and data[2] doesn't exist.
  • data: [] this is an empty tuple, you're telling the compiler that you'll get an empty tuple (an array which is always empty).
Sign up to request clarification or add additional context in comments.

4 Comments

CreatePlots is called after receiving data from a web service call. In this case data[0][0] would be a string and data[0]1] would be a "UserDefinedObject" EpmsPlotQueryData, in General the result from the webservice could be anything. Thank you for clarifying.
Then use Array<[string, EpmsPlotQueryData]> (an array of tuples) @MtnManChris I updated the answer accordingly.
the answer you supplied of an empty tuple is what I needed. This is so unlike java script where [] is an array of anything. I could specify but the results from the webservice for other calls can be of other object/types so the answer is to not specify a type.
They probably still share some common shape? Like Array<[string, any]>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.