Skip to content

as tuple type assertion modifierΒ #49414

Closed
@zsakowitz

Description

@zsakowitz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions