From 1b6488989b3812794c14fef464323f41e977cddc Mon Sep 17 00:00:00 2001 From: harris-miller Date: Sat, 20 Jan 2024 15:25:15 -0700 Subject: [PATCH] same for omit --- test/omit.test.ts | 7 ++++--- test/pick.test.ts | 3 ++- types/omit.d.ts | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/omit.test.ts b/test/omit.test.ts index 55ea9ec..343ca93 100644 --- a/test/omit.test.ts +++ b/test/omit.test.ts @@ -10,9 +10,9 @@ expectType<{ foo: number; bar: string; biz?: boolean; }>(omit([])(obj)); expectType<{ bar: string; biz?: boolean }>(omit(['foo'])(obj)); expectType<{ biz?: boolean }>(omit(['foo', 'bar'])(obj)); expectType<{}>(omit(['foo', 'bar', 'biz'])(obj)); -// as long as some of the array has valid keys, it will accept it, but will return `never` if there are any invalid keys -expectType(omit(['baz', 'bar', 'biz'])(obj)); -// if the array is only invalid keys, it will error +// errors with `never` if array contains some but not all keys on obj +expectError(omit(['baz', 'bar', 'biz'])(obj)); +// errors (with better message) with array of all unknown keys expectError(omit(['baz'])(obj)); // make sure typed array works expectType<{}>(omit([] as (keyof typeof obj)[])(obj)); @@ -26,4 +26,5 @@ expectError(omit(['baz', 'bar', 'biz'], obj)); expectType<{}>(omit([] as (keyof typeof obj)[], obj)); // Record +expectType>(omit(['foo', 'bar'])({} as Record)); expectType>(omit(['foo', 'bar'], {} as Record)); diff --git a/test/pick.test.ts b/test/pick.test.ts index 7a9f603..00a1e7b 100644 --- a/test/pick.test.ts +++ b/test/pick.test.ts @@ -12,7 +12,7 @@ expectType<{ foo: number; bar: string; }>(pick(['foo', 'bar'])(obj)); expectType<{ foo: number; bar: string; biz?: boolean }>(pick(['foo', 'bar', 'biz'])(obj)); // as long as some of the array has valid keys, it will accept it, but will return `never` if there are any invalid keys expectError(pick(['baz', 'bar', 'biz'])(obj)); -// if the array is only invalid keys, it will error +// errors (with better message) with array of all unknown keys expectError(pick(['baz'])(obj)); // make sure typed array works expectType(pick([] as (keyof typeof obj)[])(obj)); @@ -26,4 +26,5 @@ expectError(pick(['baz', 'bar', 'biz'], obj)); expectType(pick([] as (keyof typeof obj)[], obj)); // Record +expectType>(pick(['foo', 'bar'])({} as Record)); expectType>(pick(['foo', 'bar'], {} as Record)); diff --git a/types/omit.d.ts b/types/omit.d.ts index 8c695fa..ff8d2c9 100644 --- a/types/omit.d.ts +++ b/types/omit.d.ts @@ -1,4 +1,4 @@ import { ElementOf } from './util/tools'; -export function omit(names: Names): , any>>>(obj: U) => string extends keyof U ? Record : ElementOf extends keyof U ? Omit> : never; +export function omit(names: Keys): , any>>>(obj: ElementOf extends keyof U ? U : never) => ElementOf extends keyof U ? Omit> : never; export function omit(names: Names, obj: U): string extends keyof U ? Record : Omit>;