Repository navigation
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
TypeScript Version: 3.5.1
Search Terms: infer array destruction
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.
function formatValue(value: string): [string, boolean] {
const hasExpandString = /\%[^\%]*\%/i;
return [value.replace(/^[\'\"].*[\'\"]$/, ''), hasExpandString.test(value)];
}
function formatOptions(options: string[]): [string, string, string, boolean] {
let key, value, operator;
switch (options.length) {
case 2:
[key, value] = options;
return [key, '=', ...formatValue(value)];
// ^
// Type 'string | boolean' is not assignable to type 'string'.
// Type 'false' is not assignable to type 'string'.
default:
throw new Error('argument error');
}
}Expected behavior: after destruction of array ts can infer [key, '=', ...formatValue(value)] to be [string, string, string, boolean] with formatValue(string): [string, boolean]
Actual behavior: got error
Type 'string | boolean' is not assignable to type 'string'.
Type 'false' is not assignable to type 'string'.
Related Issues: #5296
Reacted by Michele Angioni, Nathan, btoo, Cam C, LennyLip, Gabriel Chittolina, Cedric Reichenbach, Ahmed Rafayat, Radian, Khafra and 1 more
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Activity
russelldavis commentedon Jul 26, 2019
To provide some clarification here, the term is "destructuring" (not "destruction"). And, to be even more precise, it's actually a spread operation. But I agree this is an issue. (DeepKolos (@deepkolos) it might help to update your title and description.)
RyanCavanaugh commentedon Jul 26, 2019
Simpler repro:
[-]typescript can't infer destruction of array properly [/-][+]typescript can't infer destructuring of array properly [/+]micheleangioni commentedon Dec 9, 2019
I ran into this problem as well, it would be nice to have it fixed :)
dianafa commentedon Dec 18, 2019
Same issue with the code:
Gives error:
The workaround was to use objects instead. Object destructuring works good 👍
but sometimes the code is not that flexible to change :)
tylervick commentedon Dec 30, 2019
+1 to this - it's especially annoying in my common use case of passing down the return value from
useStatein React.Proper treatment of splicing tuples in array literals
Proper treatment of splicing tuples in array literals
Proper treatment of splicing tuples in array literals
Proper treatment of splicing tuples in array literals
Proper treatment of splicing tuples in array literals
Proper treatment of splicing tuples in array literals
keithbro commentedon Apr 21, 2020
I think this is still an issue. With the following code:
I get an issue when testing against the nightly build on https://www.typescriptlang.org/play
elibarzilay commentedon Apr 22, 2020
Keith Broughton (@keithbro), the problem here is the inferred type of
f, which TS assumes to be an array and therefore results in the union that you see.