-
-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
b11f017
commit e11ab80
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type {JsonPrimitive, JsonValue} from './basic'; | ||
|
||
type JsonifiableObject = {[Key in string]?: Jsonifiable} | {toJSON: () => Jsonifiable}; | ||
type JsonifiableArray = readonly Jsonifiable[]; | ||
|
||
/** | ||
Matches a value that can be losslessly converted to JSON. | ||
Can be used to type values that you expect to pass to `JSON.stringify`. | ||
`undefined` is allowed in object fields (for example, `{a?: number}`) as a special case even though `JSON.stringify({a: undefined})` is `{}` because it makes this class more widely useful and checking for undefined-but-present values is likely an anti-pattern. | ||
@example | ||
``` | ||
import type {Jsonifiable} from 'type-fest'; | ||
// @ts-expect-error | ||
const error: Jsonifiable = { | ||
map: new Map([['a', 1]]), | ||
}; | ||
JSON.stringify(error); | ||
//=> {"map": {}} | ||
const good: Jsonifiable = { | ||
number: 3, | ||
date: new Date(), | ||
missing: undefined, | ||
} | ||
JSON.stringify(good); | ||
//=> {"number": 3, "date": "2022-10-17T22:22:35.920Z"} | ||
``` | ||
@category JSON | ||
*/ | ||
export type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {expectAssignable, expectNotAssignable} from 'tsd'; | ||
import type {Jsonifiable} from '..'; | ||
|
||
expectAssignable<Jsonifiable>(1); | ||
expectAssignable<Jsonifiable>(''); | ||
expectAssignable<Jsonifiable>(null); | ||
expectAssignable<Jsonifiable>(new Date()); | ||
expectAssignable<Jsonifiable>({a: new Date()}); | ||
expectAssignable<Jsonifiable>([new Date()]); | ||
expectAssignable<Jsonifiable>({a: undefined}); | ||
expectAssignable<Jsonifiable>([1, 2, 3] as const); | ||
expectAssignable<Jsonifiable>({a: new Date()} as const); | ||
expectAssignable<Jsonifiable>({a: {deeply: {nested: {toJsonObject: new Date()}}}}); | ||
expectAssignable<Jsonifiable>({toJSON: () => new Date()}); | ||
expectAssignable<Jsonifiable>({ | ||
toJSON() { | ||
return { | ||
foo: { | ||
toJSON() { | ||
return {bar: 'bar'}; | ||
}, | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
expectNotAssignable<Jsonifiable>(undefined); | ||
expectNotAssignable<Jsonifiable>(new Map()); | ||
expectNotAssignable<Jsonifiable>({a: new Map()}); | ||
expectNotAssignable<Jsonifiable>([new Map()]); |