Skip to content

Suggestion: Shorthand Syntax for Fixed-Length Single-Type Tuple (e.g. [number: 4])Β #62481

Closed

Description

@hillin

πŸ” Search Terms

fixed length single type tuple shorthand

βœ… Viability Checklist

⭐ Suggestion

Introduce a concise syntax for declaring fixed-length tuples consisting of a single repeated type. For example, instead of writing [number, number, number, number], allow [number: 4] to represent a tuple of four numbers.

πŸ“ƒ Motivating Example

Current syntax for fixed-length tuples requires repetition. This can be a bit awkward if the tuple has many elements:

type Matrix3x3 = [number, number, number, number, number, number, number, number, number];

With the proposed shorthand:

type Matrix3x3 = [number: 9];

This is especially useful for mathematical vectors, matrices, and APIs expecting tuples of a fixed size and type.

πŸ’» Use Cases

  • Declaring fixed-size vectors or matrices in math/graphics code
  • APIs in other domains with similar semantical requirement

Activity

  1. IllusionMH commented on Sep 24, 2025

    @IllusionMH
    Contributor

    Isn't proposed syntax already used for named tuples?
    Example should create length 1 tuple with element named number and type (literal) 9.

  2. MartinJohns commented on Sep 24, 2025

    @MartinJohns
    Contributor

    Duplicate of #47874.

  3. hillin commented on Sep 24, 2025

    @hillin
    Author

    Isn't proposed syntax already used for named tuples? Example should create length 1 tuple with element named number and type (literal) 9.

    Yeah you are definitely right, I didn't pay too much attention to the syntax, it's just a placeholder. I'd be totally happy with a better syntax for the same semantics (say, [number; 9] like rust's fixed-length array declaration).

  4. hillin commented on Sep 24, 2025

    @hillin
    Author

    Duplicate of #47874.

    I knew there would be an existing issue!

    Unfortunately the author of that issue did not come up with a good real world example. In the maths/CG area this could be much more a real QoL improvement.

  5. snarbles2 commented on Sep 24, 2025

    @snarbles2

    The linked issue points out this is something you can create yourself with code example included.

    type TupLen<N extends number, T = any, A extends T[] = []> =
    	N extends A['length'] ? A : TupLen<N, T, [T, ...A]>;
    
    
    declare const x: TupLen<5, number>;
    // same as [number, number, number, number, number]
    
    type Matrix3x3 = TupLen<9, number>;
  6. locked as resolved and limited conversation to collaborators on Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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