Skip to content

Commit d5f6bc7

Browse files
chore: improve Typescript magic of (credit to @Lemonexe)
1 parent 9d14df0 commit d5f6bc7

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

packages/type-utils/src/utils.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export type FilterPropertiesByType<T, ValueFilter> = {
110110
};
111111

112112
/**
113-
* Removed the type from the union where `{ type: U }`.
113+
* Removed the type from the union where `{ KeyName: ValueToExclude }`.
114114
*
115115
* Example:
116116
* ```
@@ -120,11 +120,15 @@ export type FilterPropertiesByType<T, ValueFilter> = {
120120
* | { type: 'C' | 'D' | 'E'; cde: boolean };
121121
*
122122
* // { type: 'A', a: string } | { type: 'B', b: number } | { type: 'D' | 'E', cde: boolean };
123-
* type NotC = FilterOutFromUnionByTypeProperty<T1, 'C'>;
123+
* type NotC = FilterOutFromUnionByTypeProperty<T1, 'type', 'C'>;
124124
* ```
125125
*/
126-
export type FilterOutFromUnionByTypeProperty<T, U> = T extends { type: infer P }
127-
? P extends U
126+
export type FilterOutFromUnionByTypeProperty<
127+
Union,
128+
KeyName extends keyof Union,
129+
ValueToExclude extends Union[KeyName],
130+
> = Union extends { [K in KeyName]: infer ActualValue }
131+
? ActualValue extends ValueToExclude
128132
? never
129-
: { type: Exclude<P, U> } & Omit<T, 'type'>
130-
: T;
133+
: { [K in KeyName]: Exclude<ActualValue, ValueToExclude> } & Omit<Union, KeyName>
134+
: Union;

0 commit comments

Comments
 (0)