-
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
Typescript inconsistently fails to keep track of required string literal types through type conversions. #30798
Comments
The issue I think of as possibly bugworthy here is primarily the unhelpful error message: declare const syntheticAtLeastOneTuple: string[] & { "0": string };
const realAtLeastOneTuple: [string, ...string[]] = syntheticAtLeastOneTuple; // error!
// ^^^^^^^^^^^^^^^^^^^
// Type 'string[] & { "0": string; }' is not assignable to type '[string, ...string[]]'. This just raises the question of why it is not assignable. I assume it's because the compiler knows that the real tuple type has a length of "at least 1", which is currently not something you can synthesize in the type system as of TS3.4 (maybe Other instances of "X is not assignable to Y" with no further info: #21253, #25896, #29049 |
A lot going on here! Seems like you're mostly observing the lack of tuple-preservation in spread; see #27859
|
Ok lemme see if I can explain again more clearly. Neither Now I'll admit, I'm pretty new to Typescript. So maybe I've erred and the types/interfaces defined above don't match my intent. If that's the case then, yes, there absolutely needs to be a better error message. But the behavior in the example above is, as far as I can tell, really inconsistent regardless.
Everything in the example above is me trying to find someway to not have to write a for loop to dis-assemble and reassemble the path every time I want to make a change to it. I can't even abstract such a for-loop to a function because I can't reliably assign the results from that function or assign to the parameters of that function. |
TypeScript Version: 3.5.0-dev.20190406
Search Terms:
spread
array literal
string literal type
ts(2741)
ts(2322)
Code
Expected behavior:
If I can assign to an
absPath
using an array literal constructor which has''
as its first element... then I should also be able to take anabsPath
assigned with such an array literal constructor and use it with Array.prototype.concat() or via the spread operator as the first element of another array literal constructor to assign to a differentabsPath
.Actual behavior:
Actual behavior is highly inconsistent as described in the commented examples above.
An especially odd example is that array literals constructed with
''
as the first element of the constructor and using an spread operator on anabsPath
as the second element of the constructor can be assigned toabsPath
s but, paradoxically, if there are any elements after the element containing the spread operator it fails again despite the first element explicitly being''
.Type 'string[]' is not assignable to type '["", ...string[]]'.ts(2322)
Playground Link:
Here
Related Issues:
Perhaps #26350 or #26350
Example Use Case
The basic idea should be pretty straight forward. Arrays of strings are paths. Arrays of strings beginning with
''
are absolute paths. Given a hierarchical data structure (like the filesystem on a hard drive... or the DOM) every node has a specific path that identifies it. By adding and removing elements from the array you can "navigate" the filetree/DOM to find other nodes relative to the current one. Not having to write a loop or function with a loop in it to perform this "navigation" is significantly more convenient and significantly improves code clarity.The text was updated successfully, but these errors were encountered: