You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm requesting to add an as tuple type assertion, similar to as const. I'm not sure about the syntax yet, since tuple isn't a reserved word and could break existing code, so I'm hoping someone has a better idea for naming. Additionally, we could add a property to tsconfig to allow as tuple assertions instead of enabling it by default.
as const doesn't work because that specifies too many declarations, makes arrays readonly, and locks the types of literal elements such as 23.
π Motivating Example
Sometimes I store several variables in an array, but TypeScript infers an array type whereas I want a tuple type. To fix this, we could use the as tuple modifier.
letmousePosition=[23,56];// The type here is `number[]`, not a tuple.letmousePosition: [number,number]=[23,56];// Now we have the correct type, but too much code.letmousePosition=[23,56]astuple;// Beautiful!
Another example might be storing user data in an array because you don't want to use an object. In these cases, as tuple syntax could save time spent writing complex type declarations.
letuser=["Steve",45,"[email protected]"];// No TypeScript, I don't want an (string | number)[].letuser=["Steve",45,"[email protected]"]astuple;// Amazing!
A readonly keyword could also be added to allow easier syntax for readonly tuples.
letuser: readonly[string,number,string]=["Steve",45,"[email protected]"];// There isn't even a shortcut for this; you have to type everything.letuser=["Steve",45,"[email protected]"]asreadonlytuple;// Amazing!
This is also out of scope a bit, but named tuples would be nice, but I'm not sure how the syntax would work.
letuser=["Steve",45,"[email protected]"]astupleof[name,age,email];// The type of `user` is [name: string, age: number, email: string].letuser: [name: string,age: number,email: string]=["Steve",45,"[email protected]"];// Too verbose!
π» Use Cases
Currently, I manually specify types for these tuples, but it feels like it could be shorter at times. Here's an example from my zSnout project where I need to store the last two points the user touched.
While writing this, I felt that there should be an easier way to write these tuples, but the current workarounds feel to verbose when I only need them for simple tasks.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
tuple casting assertion as modifier
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I'm requesting to add an
as tuple
type assertion, similar toas const
. I'm not sure about the syntax yet, sincetuple
isn't a reserved word and could break existing code, so I'm hoping someone has a better idea for naming. Additionally, we could add a property totsconfig
to allowas tuple
assertions instead of enabling it by default.as const
doesn't work because that specifies too many declarations, makes arrays readonly, and locks the types of literal elements such as23
.π Motivating Example
Sometimes I store several variables in an array, but TypeScript infers an array type whereas I want a tuple type. To fix this, we could use the
as tuple
modifier.Another example might be storing user data in an array because you don't want to use an object. In these cases,
as tuple
syntax could save time spent writing complex type declarations.A
readonly
keyword could also be added to allow easier syntax for readonly tuples.This is also out of scope a bit, but named tuples would be nice, but I'm not sure how the syntax would work.
π» Use Cases
Currently, I manually specify types for these tuples, but it feels like it could be shorter at times. Here's an example from my zSnout project where I need to store the last two points the user touched.
While writing this, I felt that there should be an easier way to write these tuples, but the current workarounds feel to verbose when I only need them for simple tasks.
The text was updated successfully, but these errors were encountered: