diff --git a/source/basic.d.ts b/source/basic.d.ts index ac74fdc9a..1c637e7a0 100644 --- a/source/basic.d.ts +++ b/source/basic.d.ts @@ -19,7 +19,7 @@ This type can be useful to enforce some input to be JSON-compatible or as a supe @category JSON */ -export type JsonObject = {[Key in string]?: JsonValue}; +export type JsonObject = {[Key in string]: JsonValue} & {[Key in string]?: JsonValue | undefined}; /** Matches a JSON array. diff --git a/test-d/package-json.ts b/test-d/package-json.ts index e43a46b4e..075d2044d 100644 --- a/test-d/package-json.ts +++ b/test-d/package-json.ts @@ -1,5 +1,5 @@ -import {expectType, expectAssignable, expectNotAssignable} from 'tsd'; -import type {PackageJson, LiteralUnion} from '../index'; +import {expectType, expectAssignable, expectNotAssignable, expectError} from 'tsd'; +import type {PackageJson, LiteralUnion, JsonObject} from '../index'; const packageJson: PackageJson = {}; @@ -81,4 +81,17 @@ expectAssignable({ '<4': undefined, }); -expectNotAssignable>(packageJson); +// Must reject an object that contains properties with `undefined` values. +// See https://github.com/sindresorhus/type-fest/issues/272 +declare function setConfig(config: JsonObject): void; + +expectError(setConfig({bugs: undefined})); +expectError(setConfig({bugs: {life: undefined}})); + +expectNotAssignable({bugs: undefined}); +expectNotAssignable({bugs: {life: undefined}}); + +expectAssignable({}); +expectAssignable({bugs: 42}); +expectAssignable({bugs: [42]}); +expectAssignable({bugs: {life: 42}});