-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add undefined to tuple index signature, or reject keys that are not "keyof tuple" #38779
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
If I understand your issue correctly, you want
This is not true. declare const two: [string, string];
const oneOfthem = two[Math.floor(Math.random()*2)];
oneOfThem.charAt(0); Before your proposed change, |
@thw0rted good points
yes, either that or
tuples were release much later and much more limited in use.. so unlike #13778 this would not
you are right, but it would be a minor breaking change, And honestly because the tuple
Unfortuanly TS does not know
// quick fix?
const oneOfthem = two[Math.floor(Math.random()*2) as 1 | 2 ];
// general case
const oneOfthem = two[Math.floor(Math.random() * two.length) as keyof two ]; |
I still don't see this as a "minor" breaking change, but I'm not sure it's a big enough deal to merit a lot of debate. I make a point of not explicitly indexing arrays unless I absolutely have to -- I use for/of or forEach whenever possible -- so really, tuples should be no different. The large majority of existing tuple usage should be indexed with a literal number, right? But those cases already work, because the system is smart enough to treat |
All I am saying is this const t0: [string][0] = 'hello' // ok
const t1: [string][1] = 'hello' // Type '"hello"' is not assignable to type 'undefined'.
// therefore this is already true
const t2: [string][0|1] // string | undefined
// How can you justify this behavior?
const t3: [string][number] // string Yes, this was what brought me to the other thread:
So objectively this is a problem that does happen. |
I wonder if maybe what you need for your specific issue is actually #29317 ? If negation existed, you could declare your index as Obviously this does nothing for |
Search Terms
Tuple index signature
Suggestion
I am opening up this issue as Seperate from #13778 since I am ok with the current interpretation of the Array.
but this fails in conjunction with tuples
Better Options:
var a : strTuple[10 as number]
should either bestring | undefined
or it should give a type error similar to:Tuples are already aware of boundaries and properly return undefined for more specific number types:
why would type
number
act any differently?Use Cases
// Easy fix for for loops
You could use
keyof typeof strTuple
instead of0 | 1
when you fix this #27995Given that
strictNullChecks
still allow typed arrays to do this:It would be nice to have a mechanism that could allow you to declare arrays with better type safety.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: