1

What I want is simple: I want to define a tuple type whose second argument is T[] if the first argument is determined to be T.

I can do this.

type TandTArray<T> = [T, T[]]

But, i had to specify every T enter image description here

But i want it to be determined by the first argument automatically.
I tried this But dosen't work

type TandTArray = <T>[T, T[]]

1 Answer 1

3

You'll need a function for generic type to be inferred:

const tandTArray = <T>(x: [T, T[]]) => x;

const foo = tandTArray([1, [1]])

Playground

Sign up to request clarification or add additional context in comments.

2 Comments

Ummm... I would be sad if there is no way to define it without function But in second thought, I notice that this might impossible.
Using generic type without generic parameter is only possible with default type parameter, but this won't help here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.