-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Request: Easy way to remove Properties from Distributive Conditional Types #30295
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
Comments
EDIT: My mistake didn't see you had already covered the example using intermediate keys. If I understand your proposal correctly, would the type: type Foo<T> = { [ K in keyof T]: ! } resolve to To play devil's advocate: I don't usually associate filtering with mapping, so I think it would somewhat overload the term "mapped type". |
yes, I think your hypothetical I feel the real benefit is it's simplicity in use with conditional types. Without an easier way to actually remove a property, the developer needs to learn how to use an intermediate keys type. As I just spent the time learning how to do this fancy mixin stuff, I can tell you that figuring out the intermediate keys is aprox 2x the work to get the mixin type working correctly. Also, even though I got it working, I honestly can't say I really know how the intermediate keys really logically "works". So to summarize my justification: Adding explicit property removal would simplify the developer experience when making complex types. |
type PropsRemoveSimple<O, T> = Pick<O, { [ K in keyof O ]: O[K] extends T ? never : K }[keyof O]>; |
@fightingcat yes, my initial post shows an expanded form of that as the current solution /** helper that returns prop names except those to filter. This helper type is needed to actually remove the prop, as otherwise the prop still exists in the type just as "never". */
type PropsRemove_Name<TTarget, TPropToRemove> = { [ K in keyof TTarget ]: TTarget[ K ] extends TPropToRemove ? never : K }[ keyof TTarget ];
/** remove props of the given type. always removes ```never``` type props. if no ```TPropToRemove``` is provided, removes just ```never``` type props. */
type PropsRemove<TTarget,TPropToRemove=never> = Pick<TTarget, PropsRemove_Name<TTarget,TPropToRemove>>; The problem is that it requires understanding of intermediate key types. My own anecdote is it took me twice as long to figure intermediate keys out, and I still don't really get how the final So my suggestion hopefully offers a way for noob devs to make complex types while reducing the cognitive burden of doing so. |
You guys added the |
When manipulating types it is currently very difficult to actually remove properties from a type.
Consider an example where you want to remove properties of a certain type:
This works, however the property still exists with type
never
The only way to actually remove the property is to have an intermediate keys type:
SUGGESTION: use the
!
symbol to avoid emittingas in this mockup, the use of
!
instead ofnever
would cause the output to be skipped.USE CASES:
being able to remove properties on a first pass (without using an intermediate
[keyof]
type greatly simplifies the work and understanding needed to create advanced types.I came across this issue when creating a
mixin/union/default values
type that prioritizes the first input object's properties over the second.additionally, when doing type unions, removal of
never
properties is importantChecklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: