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

Proposal: add option with exclude/keep specified type on deep type #862

Open
Emiyaaaaa opened this issue Apr 12, 2024 · 5 comments
Open
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Emiyaaaaa
Copy link
Collaborator

Emiyaaaaa commented Apr 12, 2024

Look at this scene

I have a type Item that wrapped with some util type, and I want get a simplify Item, like:

import type { SimplifyDeep } from 'type-fest/source/merge-deep'

type SimplifyItem = SimplifyDeep<Item>
// => expect type SimplifyItem = { name: string, color: THREE.Color }

but actual rsult is:

import type { SimplifyDeep } from 'type-fest/source/merge-deep'

type SimplifyItem = SimplifyDeep<Item>
/**
type SimplifyItem = { 
    name: string,
    color: {
            readonly isColor: true;
            r: number;
            g: number;
            b: number;
            set: (color: string | number | Color) => Color;
            ... 31 more ...;
            toArray: {
                ...;
            };
        };
}
*/

so maybe we can add a exclude type option ( or named keep ) on Deep type like:

import type { SimplifyDeep } from 'type-fest/source/merge-deep'

type SimplifyItem = SimplifyDeep<Item, { exclude: THREE.Color }>
// => expect type SimplifyItem = { name: string, color: THREE.Color }

It can also resolve scene conflicts between users, such as one want recursive into HTMLElemet, anther one want keep HTMLElemet

Related comment: #651 (comment)
Related issue: #651

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@Emiyaaaaa Emiyaaaaa added enhancement New feature or request help wanted Extra attention is needed labels Apr 12, 2024
@voxpelli
Copy link
Collaborator

One could also do:

type SimplifyItem = SimplifyDeep<Omit<Item, 'color'>> & Pick<Item, 'color'>;

And if one wants a helper to keep it a bit shorter one could do something like this in ones types:

type SimplifyDeepExcept<A, B extends keyof A> = SimplifyDeep<Omit<A,B>> & Pick<A, B>;

Is it common that that needs to exclude something like THREE.Color at an unknown location in an object?

@voxpelli
Copy link
Collaborator

voxpelli commented Apr 12, 2024

Related: #719

Kind of requests the opposite: #860 (Kind of request an { only: string } rather than an { exclude: string })

@Emiyaaaaa
Copy link
Collaborator Author

Emiyaaaaa commented Apr 16, 2024

type SimplifyItem = SimplifyDeep<Omit<Item, 'color'>> & Pick<Item, 'color'>;

My case is more complicated. THREE.Color is deep in nested object, and must used type to exclude not key, because key could be 'lineColor', 'pointColor' or other in my scene

@sindresorhus
Copy link
Owner

This proposal makes sense to me.

@Emiyaaaaa
Copy link
Collaborator Author

Sorry, I found I could use ConditionalSimplifyDeep<Item, THREE.Color, object> instead, but other deep type also need this feature.

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

No branches or pull requests

3 participants