Skip to content
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

as tuple type assertion modifier #49414

Closed
5 tasks done
zsakowitz opened this issue Jun 7, 2022 · 2 comments
Closed
5 tasks done

as tuple type assertion modifier #49414

zsakowitz opened this issue Jun 7, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@zsakowitz
Copy link

Suggestion

πŸ” Search Terms

tuple casting assertion as modifier

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

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.

let mousePosition = [23, 56];
// The type here is `number[]`, not a tuple.

let mousePosition: [number, number] = [23, 56];
// Now we have the correct type, but too much code.

let mousePosition = [23, 56] as tuple;
// 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.

let user = ["Steve", 45, "[email protected]"];
// No TypeScript, I don't want an (string | number)[].

let user = ["Steve", 45, "[email protected]"] as tuple;
// Amazing!

A readonly keyword could also be added to allow easier syntax for readonly tuples.

let user: readonly [string, number, string] = ["Steve", 45, "[email protected]"];
// There isn't even a shortcut for this; you have to type everything.

let user = ["Steve", 45, "[email protected]"] as readonly tuple;
// 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.

let user = ["Steve", 45, "[email protected]"] as tuple of [name, age, email];
// The type of `user` is [name: string, age: number, email: string].

let user: [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.

let lastTouchPointA: [number, number] = [NaN, NaN];
let lastTouchPointB: [number, number] = [NaN, NaN];

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.

@xiBread
Copy link

xiBread commented Jun 7, 2022

Duplicate of #48052

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 9, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants