-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assign tuple to object: Number is not assignable to type literal #24680
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
Comments
Right now an array is only considered a tuple if it's contextually typed by a tuple-like type, which is really any type with a |
@DanielRosenwasser thanks for your answer. If I understand correctly, you are saying that I can assign tuples only to tuple-like objects (= must have I do not have a real use-case. I stumbled upon this feature while I was playing around with fixed size tuples in an attempt to define generic arrays of fixed size, such as,
|
What I do for tuples, https://github.com/AnyhowStep/typed-orm/blob/master/src/main/declaration-v2/tuple.ts If you need a tuple of length 2, you need to check that it had the indices "0" and "1" If you need a tuple of length 3, "0", "1", "2" Just "0" and "length" are not really "enough" to guarantee those indices exist, after all. I personally have no use for fixed length tuples. So, my type declarations make no reference to forcing it. But I imagine, without "native" support for tuples of fixed length, you can implement it with copy pasting code (or writing a for-loop) and conditional types. But using { length : number } isn't enough to guarantee the indices |
I think tuple-typing anything contextually typed by a |
TypeScript Version: 3.0.0-dev.20180602
Search Terms:
number is not assignable with type literal, tuple assigned to object, fixed size tuples
Code
Expected behavior:
After that,
[number, number]
type should be equivalent to:Hence, I would expect
let p0: { length: 2 } = [1, 2];
to be correct.Please not that
let p1: { length: 2, 0: number } = [1, 2];
is allowed.Actual behavior:
let p0: { length: 2 } = [1, 2];
is not correct and gives errorNumber is not assignable to type 2
.Playground Link:
Playground snippet
The text was updated successfully, but these errors were encountered: