type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

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

Open Emiyaaaaa opened this issue 4 months ago • 5 comments

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: https://github.com/sindresorhus/type-fest/issues/651#issuecomment-1803324362 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 avatar Apr 12 '24 03:04 Emiyaaaaa

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 avatar Apr 12 '24 16:04 voxpelli

Related: https://github.com/sindresorhus/type-fest/issues/719

Kind of requests the opposite: https://github.com/sindresorhus/type-fest/issues/860 (Kind of request an { only: string } rather than an { exclude: string })

voxpelli avatar Apr 12 '24 21:04 voxpelli

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

Emiyaaaaa avatar Apr 16 '24 03:04 Emiyaaaaa

This proposal makes sense to me.

sindresorhus avatar Apr 16 '24 16:04 sindresorhus

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

Emiyaaaaa avatar Apr 22 '24 06:04 Emiyaaaaa