Repository navigation
Unable to call map on array of variable depth #35045
Description
Activity
Duplicate of #7294 (see comment). Related to #29011.
Mentioned in documentation as caveats for improved behavior for calling union types:
This new behavior only kicks in when at most one type in the union has multiple overloads, and at most one type in the union has a generic signature. That means methods on
number[] | string[]likemap(which is generic) still won’t be callable.While methods like
pushare obviously problematic fornumber[] | string[], it looks safe to allow methods likemaporeveryforreadonly number[] | readonly string[]. Has this (special treatment of read-only arrays when merging union signatures) already been discussed in some issue?- addedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on Nov 14, 2019 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
nodeof typeimport('estree').Node | import('@typescript-eslint/typescript-estree').TSESTree.Nodeand code like this:if (node.type === "ObjectExpression") { return node.properties.every( p => ... ); }
Even though
node.propertiesare correctly inferred to be of typeObjectLiteralElementLike[] | Property[](because bothestreeand@typescript-eslint/typescript-estreedefineObjectExpression, not exactly compatible though),pin theeverycallback is of typeany. 😢I think it's inherently bad idea to rely on compiler to figure out the output of the
transformCoordArray()meanwhilecoords: ...argument varies.
I think much better way either to create overloads (checkout playground):... 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
coords:(<any> coords).map(c => transformCoordArray(c, fn)) as Position | Position[] | Position[][];
Reacted by Dan VanderkamReacted by Georgii DolzhykovFixed by #42620 I think?
Reacted by Dan VanderkamYep! 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: