Type alias Except<ObjectType, KeysType, Options>

Except<ObjectType, KeysType, Options>: {
    [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType]
} & (Options["requireExactProps"] extends true
    ? Partial<Record<KeysType, never>>
    : {})

Create a type from an object type without certain keys.

We recommend setting the requireExactProps option to true.

This type is a stricter version of Omit. The Omit type does not restrict the omitted keys to be keys present on the given type, while Except does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.

This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types (microsoft/TypeScript#30825).

Type Parameters

  • ObjectType

  • KeysType extends keyof ObjectType

  • Options extends ExceptOptions = {
        requireExactProps: false;
    }

Example

import type {Except} from 'type-fest';

type Foo = {
a: number;
b: string;
};

type FooWithoutA = Except<Foo, 'a'>;
//=> {b: string}

const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
//=> errors: 'a' does not exist in type '{ b: string; }'

type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
//=> {a: number} & Partial<Record<"b", never>>

const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.

Generated using TypeDoc