Incorrect type inference for array rest assignment and inability to add annotation
See original GitHub issueTypeScript Version: 2.7.1-insiders.20180127
Search Terms: array destructuring rest assignment
Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
const [stringA, ...numbers] = ['string', 1, 2];
// const [stringA, ...numbers] = <[string, number, number]>['string', 1, 2];
// const [stringA, ...numbers]: [string, number[]] = ['string', 1, 2];
// const [stringA, ...numbers]: [string, [number, number]] = ['string', 1, 2];
Expected behavior: Expect numbers
to be of type number[]
rather than (string | number)[]
.
TS also does not allow annotating as a tuple
Actual behavior: const numbers: (string | number)[]
Related Issues:
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
suggestion: explicit "tuple" syntax · Issue #16656 - GitHub
Problem. Because array literals are (correctly) inferred to be arrays, TS is limited in its ability to infer "tuple". This means there's an...
Read more >Asking for required annotations - Medium
The simplest solution here would be to use an explicit type argument to the OrderedMap() constructor, but any annotation between the exports and ......
Read more >Why can't TypeScript infer type from filtered arrays?
One way to deal with this lack of inference is annotate such boolean -returning functions as a user-defined type guard function.
Read more >Sound, Heuristic Type Annotation Inference for Ruby
Many researchers have explored retrofitting static type sys- tems to dynamic languages. This raises the question of how to add type annotations ......
Read more >Semantics
It could be that you don't care about the type at compile time or are relying on type inference (with Groovy's static nature)....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The right type should be
const [stringA, ...numbers]: [string, number, number]
. andnumbers
should be inferred correctly. that is a bug we should fix.The issue in the opening post is fixed in
typescript@next
, maybe even3.0.0-rc
.numbers
is now of typenumber[]
.