-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to call map on array of variable depth #35045
Comments
Duplicate of #7294 (see comment). Related to #29011. Mentioned in documentation as caveats for improved behavior for calling union types:
|
While methods like |
We are trying to gradually introduce type checking in the Prettier project and facing this issue. Prettier works with ASTs that come from different parsers. E.g. suppose we have an object if (node.type === "ObjectExpression") {
return node.properties.every(
p => ...
);
} Even though |
I think it's inherently bad idea to rely on compiler to figure out the output of the ...
export function transformCoordArray(coords: Position[][], fn: PosTransfCb): Position[][]
export function transformCoordArray(coords: Position[], fn: PosTransfCb): Position[]
export function transformCoordArray(coords: Position, fn: PosTransfCb): Position
... or assert (<any> coords).map(c => transformCoordArray(c, fn)) as Position | Position[] | Position[][]; |
Fixed by #42620 I think? |
Yep! The error in the example in the original issue goes away with 4.3: playground |
I'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either
number[]
(a Point),number[][]
(a LineString),number[][][]
(a Polygon) ornumber[][][][]
(a MultiPolygon). I have a recursive implementation which works but does not type check.TypeScript Version: 3.7.2
Search Terms:
Code
(playground)
The full error message is:
The call is valid because
c
's type should bePosition | Position[]
, which is assignable toPosition | Position[] | Position[][]
.Interestingly, I can make the error go away by assigning to a new variable which has a broader type (
(Position | Position[])[]
):(playground)
Expected behavior:
No error.
Actual behavior:
The given error.
Playground Link: playground
Related Issues:
The text was updated successfully, but these errors were encountered: