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

Make Simplify works for deep nesting #810

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

linbudu599
Copy link
Contributor

This PR makes Simplify type works for computed types with deep nesting.

Example:

type Foo = {
  foo: string;
  bar: number;
  baz: boolean;
}

type Result = {
  [K in keyof Foo]: Pick<Foo, K>
}

// {
//     foo: Pick<Foo, "foo">;
//     bar: Pick<Foo, "bar">;
//     baz: Pick<Foo, "baz">;
// }
type SimplifiedResult = Simplify<Result>;

With deep version:

type DeepSimplify<T> = { [KeyType in keyof T]: T[KeyType] extends object ? DeepSimplify<T[KeyType]> : T[KeyType] } & {};

type Result = {
  [K in keyof Foo]: Pick<Foo, K>
}

// {
//     foo: {
//         foo: string;
//     };
//     bar: {
//         bar: number;
//     };
//     baz: {
//         baz: boolean;
//     };
// }
type DeepSimplifiedResult = DeepSimplify<Result>;

@linbudu599
Copy link
Contributor Author

Seems like this change breaks the other type behaviour in test cases, like PartialOnUndefinedDeep, maybe we should bring a new type called SimplifyDeep ? @sindresorhus

@Emiyaaaaa
Copy link
Collaborator

Seems like this change breaks the other type behaviour in test cases, like PartialOnUndefinedDeep, maybe we should bring a new type called SimplifyDeep ? @sindresorhus

SimplifyDeep is existing but it's in type-fest/source/merge-deep, make it to be a stable type will be better

@Emiyaaaaa Emiyaaaaa added help wanted Extra attention is needed type addition labels Feb 2, 2024
@sindresorhus
Copy link
Owner

Yes. It should be a separate type. That internal type should be properly exposed.

@sindresorhus
Copy link
Owner

Bump :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants