Length<T>
EasyP2Coding
Implement Length<T>, the length of a tuple T as a number literal type. It must accept readonly tuples such as those produced by as const, and reject anything that is not an array type.
Tests your type must pass
const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const const spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] as const type cases = [ Expect<Equal<Length<typeof tesla>, 4>>, Expect<Equal<Length<typeof spaceX>, 5>>, Expect<Equal<Length<[]>, 0>>, Expect<Equal<Length<[1, 2]>, 2>>, ] // @ts-expect-error a number is not an array type error1 = Length<5> // @ts-expect-error a string is not an array type error2 = Length<'hello world'>
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.