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

Add TupleOf type #161

Open
piotrwitek opened this issue Jan 6, 2021 · 2 comments · May be fixed by #162
Open

Add TupleOf type #161

piotrwitek opened this issue Jan 6, 2021 · 2 comments · May be fixed by #162

Comments

@piotrwitek
Copy link
Owner

Is your feature request related to a real problem or use-case?

Add a generic way to specify length of a tuple type

Describe a solution including usage in code example

// From @ahejlsberg PR https://github.com/microsoft/TypeScript/pull/40002
type TupleOf<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _TupleOf<T, N, [T, ...R]>;

type Vector<Length extends number> = TupleOf<number, Length>
type Matrix<Rows extends number, Columns extends number> = TupleOf<TupleOf<number, Columns>, Rows>

const v: Vector<2> = [1, 2]
const m: Matrix<2, 3> = [
  [1, 2, 3],
  [1, 2, 3],
]

Additional context (optional)

microsoft/TypeScript#40002

https://www.typescriptlang.org/play?ts=4.1.0-beta#code/C4TwDgpgBAKgrmANhA8gMwDwwDRQHJQQAewEAdgCYDOUZcAtgEYQBOAfFALy0PMuEly1fFAD8sANoBdKAC4oAfXhJUmHPlzS2AbgBQoSIuXJ0WXAWKlKNOk1a4ASgKvC4ZANZkA9gHcyWrigHCQByZDIAc2AACxCZSyEaAnEneSUEEzVzTXUAOnyHKR1dfXBoGABGQONVDCpgFgBLSNwAZh0oKAB6LqgJeqaWqAHmiNwRyKlSwxgAJmqM2ts+XAAGKAAfKHmtgBYO7t7pTb7l+x47FhktiTOWXDuH3nO7qYNy1oWVU0eLvgOen9WNJpuVdl9MhhfhVVqsAb0ACIQMAxQgsFheFhAA

@piotrwitek piotrwitek changed the title Add TupleOf Add TupleOf type Jan 6, 2021
@Mouvedia
Copy link

Mouvedia commented Jan 6, 2021

Shouldn't you use the logarithmic one instead?

@piotrwitek
Copy link
Owner Author

piotrwitek commented Jan 8, 2021

I agree, logarithmic could be better, but I haven't tried it yet. Looks a bit overwhelming :D

@DetachHead DetachHead linked a pull request Jan 14, 2021 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants