Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17973 - DeepMutable #18259

Open
jiangshanmeta opened this issue Oct 29, 2022 · 4 comments
Open

17973 - DeepMutable #18259

jiangshanmeta opened this issue Oct 29, 2022 · 4 comments
Labels
17973 answer Share answers/solutions to a question en in English

Comments

@jiangshanmeta
Copy link
Member

type DeepMutable<T extends Record<keyof any,any>> =
  T extends (...args:any[])=>any?
    T:
    {
      - readonly [K in keyof T]:DeepMutable<T[K]>
    }
@jiangshanmeta jiangshanmeta added answer Share answers/solutions to a question en in English labels Oct 29, 2022
@github-actions github-actions bot added the 17973 label Oct 29, 2022
@BruceYuj
Copy link

BruceYuj commented Aug 2, 2023

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>?

@sv-98-maxin
Copy link

sv-98-maxin commented Sep 20, 2023

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>?

because T[K] extends any ?

@kstratis
Copy link

kstratis commented May 2, 2024

@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?

@kstratis
Copy link

kstratis commented May 2, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
17973 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

4 participants