17973 - DeepMutable
type DeepMutable<T extends Record<keyof any,any>> =
T extends (...args:any[])=>any?
T:
{
- readonly [K in keyof T]:DeepMutable<T[K]>
}
Why we can use DeepMutable<T[K]> directly? T[k] can be primitive, and why it doesn't tigger the generic constraint T extends Record<keyof any, any>?
Why we can use
DeepMutable<T[K]>directly?T[k]can be primitive, and why it doesn't tigger the generic constraintT extends Record<keyof any, any>?
because T[K] extends any ?
@BruceYuj question is valid.
T[k] can be primitive, and why it doesn't tigger the generic constraint T extends Record<keyof any, any>?
I've got the same question. How is this possible?
@sv-98-maxin T[K] extends any only inside Record. When T[K] is not a Record but a primitive value instead like string, then what's actually going on?
@BruceYuj Turns out the answer to ~your~ our question is this:
Mapped types already "skip" primitives by returning the input, and they automatically distribute over union, so you don't need to check for these yourself
Example:
type Look<T> = { [K in keyof T]: 123 };
type Y1 = Look<{ a: string }> // {a: 123}
type Y2 = Look<string> // string
type Y3 = Look<{ a: string } | { b: string }>
// Look<{ a: string; }> | Look<{ b: string; }>
For the full story have a look at this great SO post: https://stackoverflow.com/questions/68693054/what-is-extends-never-used-for/68693367