Issue
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'.
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.
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
...or by introducing a new type:
export type ArrayType<T> = T[] | readonly T[]
export type ElementType<T extends ArrayType<any>> =
T extends ArrayType<infer U> ?
U :
never
we can simplify the amount of inference and branching required for the conditional type
Answered By - spender
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.