-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
I figured out how to exclude a set of key names from every level of an object in typescript using a recursive type definition. This would be useful, for example, to restrict someone from calling proxify with an Observable of a type that overrides subscribe.
// deep-omit.d.ts
export type DeepOmit<Reserved extends string> =
| Boolean
| Number
| String
| Date
| Function
| Array<DeepOmit<Reserved>>
| ({ [key: string]: DeepOmit<Reserved> } & Partial<
{ [key in Reserved]: never }
>);Usage:
type S = DeepOmit<keyof Observable>;
// or
type S = DeepOmit<'subscribe' | 'pipe'>;
const test: S = 'a'; // OK
const test: S = false // OK
const test: S = { subscribe: false }; // Type 'boolean' is not assignable to type 'undefined'.
// Deep matching
const test: S = { a: { pipe: 'hello' } }; // Type 'string' is not assignable to type 'undefined'.
const test: S = { a: { piper: 'hi' } }; // OKBut I don't really know how to integrate with the existing typings file you have.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels