Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The const assertion seems that the type can't be assigned to the type any[] for the generic parameter

export const CHRONIC_CHANNEL_LEVELS = [
  { label: 'one', value: '1' },
  { label: 'two', value: '2' },
] as const;

export type ElementType<T extends any[]> = T extends (infer U)[] ? U : never;

type ChronicChannelLevel = ElementType<typeof CHRONIC_CHANNEL_LEVELS>['value']; // throw error

TSC throws an error:

Type 'readonly [{ readonly label: "one"; readonly value: "1"; }, { readonly label: "two"; readonly value: "2"; }]' does not satisfy the constraint 'any[]'.
  The type 'readonly [{ readonly label: "one"; readonly value: "1"; }, { readonly label: "two"; readonly value: "2"; }]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.(2344)

The const assertion seems that the type can’t be assigned to the type any[] for the generic parameter.

I want the type ChronicChannelLevel to be a union type '1' | '2'.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know I can use typeof CHRONIC_CHANNEL_LEVELS[number]['value'], but I want to create a utility type like ElementType to get the element type from an array.

TypeScript Playground

>Solution :

By accepting that tuple/array and readonly tuple/array are fundamentally different types, we can make progress:

export type ElementType<T extends (any[] | readonly any[])> =
  T extends (infer U)[] ?
  U :
  T extends readonly (infer U)[] ?
  U :
  never

Playground Link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading