diff --git a/source/set-non-nullable.d.ts b/source/set-non-nullable.d.ts index 8392d1e85..206441106 100644 --- a/source/set-non-nullable.d.ts +++ b/source/set-non-nullable.d.ts @@ -1,6 +1,3 @@ -import type {Except} from './except'; -import type {Simplify} from './simplify'; - /** Create a type that makes the given keys non-nullable, where the remaining keys are kept as is. @@ -35,10 +32,8 @@ type AllNonNullable = SetNonNullable; @category Object */ -export type SetNonNullable = - Simplify< - // Pick just the keys that are readonly from the base type. - Except & - // Pick the keys that should be non-nullable from the base type and make them non-nullable. - {[Key in Keys]: NonNullable} - >; +export type SetNonNullable = { + [Key in keyof BaseType]: Key extends Keys + ? NonNullable + : BaseType[Key]; +}; diff --git a/test-d/set-non-nullable.ts b/test-d/set-non-nullable.ts index 03398b2bf..d31d38463 100644 --- a/test-d/set-non-nullable.ts +++ b/test-d/set-non-nullable.ts @@ -16,3 +16,15 @@ expectType<{a: number; b?: string}>(variation3); // Fail if type changes even if non-nullable is right. declare const variation4: SetNonNullable<{a: number; b: string | undefined}, 'b'>; expectNotAssignable<{a: string; b: string}>(variation4); + +// Update all keys if `Keys` generic is not passed. +declare const variation5: SetNonNullable<{a: number; b: string | undefined; c: boolean | null}>; +expectType<{a: number; b: string; c: boolean}>(variation5); + +// Does not throw type error in type predicate contexts. +type Variation6Config = {a: boolean | null; b: boolean | null}; +const variant6Fn = ( + config: Variation6Config, + prop: TProp, +): config is SetNonNullable => Boolean(config[prop]); +expectNotAssignable(variant6Fn); // Just to prevent unused error.