Aller au contenu principal

StrictOmit<T, K>

StrictOmit<T, K> = Omit<T, K>

Like Omit<T, K> but enforces that K must be a key of T.

TypeScript's built-in Omit accepts any string as K, which can hide typos. StrictOmit catches these errors at compile time.


Type Parameters

T: T

The object type

K: K extends keyof T

The keys to omit (must exist in T)


Since

2.0.0


Example

type User = { id: string; name: string; email: string };

type WithoutEmail = StrictOmit<User, "email">;
// = { id: string; name: string }

type Typo = StrictOmit<User, "emial">;
// ❌ Error: Type '"emial"' does not satisfy the constraint 'keyof User'