Skip to content

Could add Repeat type for array? #52257

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

Closed
5 tasks done
WolfgangKurz opened this issue Jan 16, 2023 · 2 comments
Closed
5 tasks done

Could add Repeat type for array? #52257

WolfgangKurz opened this issue Jan 16, 2023 · 2 comments

Comments

@WolfgangKurz
Copy link

Suggestion

πŸ” Search Terms

Repeat , Array , Repeat Type

βœ… 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'd like to shortnize repeated array types like this:

type Number32 = Repeat<number, 32>;

πŸ“ƒ Motivating Example

When i made my own Matrix class, there's some pretty uncomfortable code for matrix value type.

Code like this:

type Matrix4x4Value = [number, number, number, /* ... */ , number]; // 16 numbers!
type Matrix4x5Value = [number, number, number, /* ... */ , number]; // 20 numbers!!

Very tired and difficult to check about what times i wrote...

So i tried to use Repeat type for those, like this (with generated programatically code):

type Repeat1<T> = [T];
type Repeat2<T> = [T, T];
/* ... */
type Repeat16<T> = [T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T];
type Repeat17<T> = [T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T];
type Repeat18<T> = [T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T];
type Repeat19<T> = [T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T];
type Repeat20<T> = [T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T];
/* ... */
type Repeat100<T> = [T, T, T, T, T, T, T, T, /* ... */ , T, T, T, T]; // 100 T

export type Repeat<T, R extends Number> = R extends 1
	? Repeat1<T>
	: R extends 2
	? Repeat2<T>
	: R extends 3
	? Repeat3<T>
	: R extends 4
	? Repeat4<T>
	: R extends 5
	? Repeat5<T>
	...
	: R extends 15
	? Repeat15<T>
	: R extends 16
	? Repeat16<T>
	: R extends 17
	? Repeat17<T>
	: R extends 18
	? Repeat18<T>
	: R extends 19
	? Repeat19<T>
	: R extends 20
	? Repeat20<T>
	...
	: R extends 100
	? Repeat100<T>
	: T[]; // not defined, just use plain array

Looks very dirty code but works well.

And my Matrix code has changed to:

type Matrix4x4Value = Repeat<number, 16>; // Easy to use
type Matrix4x5Value = Repeat<number, 20>; // and Easy to know

Just for type-checking feature, as i think.

πŸ’» Use Cases

Fixed-length single type array like Matrix value type as in Example section, to use in WebGL or something.

@MartinJohns
Copy link
Contributor

Duplicate of #50838.

@WolfgangKurz
Copy link
Author

Ah i missed it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants