I want to make new object type from Original type should be filtered when property key starts with the Target String.
Origin type:
type Origin = {
a: string,
b: string,
_c: string,
_d: string,
}
Result type as I want.:
// type Result = SomethingWork<Origin, '_'>;
type Result = {
a: string,
b: string
};
Origin type has dynamic property keys.
so It's not correct, if using type as direct like '_c' | '_d'
This is now possible with template literal types
type FilterNotStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? never : Set
Example:
type Origin = {
a: string,
b: string,
_c: string,
_d: string,
}
type FilteredKeys = FilterNotStartingWith<keyof Origin, '_'>
type NewOrigin = Pick<Origin, FilteredKeys>
/*
type NewOrigin = {
a: string;
b: string;
}
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With