question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect type inference for array rest assignment and inability to add annotation

See original GitHub issue

TypeScript 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)[]

Playground Link: http://www.typescriptlang.org/play/index.html#src=const [stringA%2C ...numbers] %3D [‘string’%2C 1%2C 2]%3B %2F%2F const [stringA%2C ...numbers] %3D <[string%2C number%2C number]>[‘string’%2C 1%2C 2]%3B %2F%2F const [stringA%2C ...numbers]%3A [string%2C number[]] %3D [‘string’%2C 1%2C 2]%3B %2F%2F const [stringA%2C ...numbers]%3A [string%2C [number%2C number]] %3D [‘string’%2C 1%2C 2]%3B

Related Issues:

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mhegazycommented, Feb 1, 2018

The right type should be const [stringA, ...numbers]: [string, number, number]. and numbers should be inferred correctly. that is a bug we should fix.

0reactions
ajafffcommented, Jul 27, 2018

The issue in the opening post is fixed in typescript@next, maybe even 3.0.0-rc. numbers is now of type number[].

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found