Reputation: 10156
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);
Upvotes: 1
Views: 400
Reputation: 221372
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.
Upvotes: 2
Reputation: 251262
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)
Upvotes: 1