Given:
Type
: Any typeProvide:
A type that only includes properties from Type
that don't have a value of type never
.
type NonNeverKeys<Type> = {
[Key in keyof Type]: IsNever<Type[Key]> extends true ? never : Key;
}[keyof Type];
type PickNonNever<Type> = Pick<Type, NonNeverKeys<Type>>;
type Example = {
[Key in keyof string]: Key extends `${string}At` ? Key : never;
};
type StringSearchable = PickNonNever<Example>;
Removing a set of members from an object via multiple combined typings.
type MapMethodReturnTypes<Type> = {
[Key in keyof Type]: Type[Key] extends ((...args: any) => any) ? ReturnType<Type[Key]> : never;
};
type FilterConverters<Type> = {
[Key in keyof Type]: Key extends `to${string}` ? Type[Key] : never;
};
type Conversions<Type> = PickNonNever<MapMethodReturnTypes<FilterConverters<Type>>>;
interface Person {
name: string;
dob: Date;
toJSON(): object;
toString(): string;
toBinary(): Uint8Array;
}
type PersonConversions = Conversions<Person>;
// this type will be: {toJSON: object, toString: string, toBinary: Uint8Array}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too