Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions source/except.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {ApplyDefaultOptions} from './internal/index.d.ts';
import type {IsEqual} from './is-equal.d.ts';
import type {LiteralUnion} from './literal-union.d.ts';

/**
Filter out keys from an object.
Expand Down Expand Up @@ -100,10 +101,10 @@ type PostPayloadFixed = Except<UserData, 'email'>;

@category Object
*/
export type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
export type Except<ObjectType, KeysType extends LiteralUnion<keyof ObjectType, PropertyKey>, Options extends ExceptOptions = {}> =
_Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;

type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
type _Except<ObjectType, KeysType extends PropertyKey, Options extends Required<ExceptOptions>> = {
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
} & (Options['requireExactProps'] extends true
? Partial<Record<KeysType, never>>
Expand Down
11 changes: 11 additions & 0 deletions test-d/except.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ type Example = {
const test: Except<Example, 'bar', {requireExactProps: false}> = {foo: 123, bar: 'asdf'};
expectType<number>(test.foo);
expectType<unknown>(test['bar']);

// Test for generic type
type GenericType<T> = T extends string ? {
foo: T;
bar: string;
} : {
baz: T;
};
type GenericTypeExcept<T> = Except<GenericType<T>, 'foo'>;
expectType<GenericTypeExcept<string>>({} as {bar: string});
expectType<GenericTypeExcept<number>>({} as {baz: number});