1

I want to do a translation of C# code below into TypeScript:

[JsType(JsMode.Json)]
public class position : JsArray<JsNumber>
{
}

[JsType(JsMode.Json)]
public class coordinateArray : JsArray<position>
{
}

[JsType(JsMode.Json)]
public class polygonRings : JsArray<coordinateArray>
{
}

I tried to do it like this:

export interface position {
    (): number[];
}

export interface coordinateArray {
    (): position[];
}

export interface polygonRings {
    (): coordinateArray[];
}

But when I try to cast it I have some problems:

Cannot convert 'coordinateArray' to 'position[]'.

In code:

(<position[]> lineString.coordinates).push(position);

2 Answers 2

2
export interface coordinateArray {
    (): position[];
}

What you've described isn't an array, it's a function type that, when invoked, returns an array:

var x: coordinateArray = ...;
var y = x(); // y: position[]

You probably want to define an index signature instead:

export interface coordinateArray {
    [index: number]: position;
}

This won't convert directly to a position[] because it's still not actually an array (a real position[] would have methods like splice and push, but coordinateArray doesn't), but is at least correct about what the shape of the type is.

Sign up to request clarification or add additional context in comments.

Comments

1

Calling the constructor method on an instance of coordinateArray would return type position[], but using the interface as a type wouldn't give you something compatible with position[].

If you have code that otherwise works, except for the compiler warning, you can tell the compiler you know better using:

(<position[]><any> ca)

Comments

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.