2
type A = [1, 2, 3]
type B = (1|2|3)[]

type IsTuple<T> = ???

type C = IsTuple<A> // true
type D = IsTuple<B> // false

I am looking for a way to tell tuple and array type apart

2 Answers 2

3

This is the method I always use:

type IsTuple<T> = T extends [any, ...any] ? true : false

We can basically check if there are individual elements inside the tuple.

Playground

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

Comments

2

update, I figured out the answer, by checking the length

type A = [1, 2, 3]
type B = (1|2|3)[]

type C = A['length'] //3
type D = B['length'] // number

playground

Comments

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.