When performing an action on a list
type PerformActionOn<L extends any[]> = L
The approach I am taking is as follows:
const keys = {
a: ['a', 'b', 'c'] as ['a', 'b', 'c'],
d: ['d', 'e', 'f'] as ['d', 'e', 'f'],
}
type Keys = typeof keys
type KeysWithAction = {
[K in keyof Keys]: PerformActionOn<Keys[K]>
}
However, to prevent redundancy (especially with potentially longer lists), I would like to simplify it like this:
const keys = {
a: ['a', 'b', 'c'] as const,
d: ['d', 'e', 'f'] as const,
}
type Keys = typeof keys
type PerformActionOn<L extends any[]> = L
type KeyTypes = {
[K in keyof Keys]: PerformActionOn<Keys[K]>
// ^^^^^^^: Type '{ a: readonly ["a", "b", "c"]; d: readonly ["d", "e", "f"]; }[K]' does not satisfy the constraint 'any[]'.
}
The error arises from attempting to pass a readonly type to PerformActionOn
, which expects a generic list type (any[]
). Is there a way to specify that PerformActionOn
should also accept readonly elements?