diff --git a/docs/modules/Json.ts.md b/docs/modules/Json.ts.md index 2ecbffff8..10014347e 100644 --- a/docs/modules/Json.ts.md +++ b/docs/modules/Json.ts.md @@ -62,7 +62,7 @@ Converts a JavaScript Object Notation (JSON) string into a `Json` type. **Signature** ```ts -export declare const parse: (s: string) => Either +export declare const parse: (s: string) => Either ``` **Example** @@ -85,7 +85,7 @@ Converts a JavaScript value to a JavaScript Object Notation (JSON) string. **Signature** ```ts -export declare const stringify: (a: A) => Either +export declare const stringify: (a: A) => Either ``` **Example** @@ -101,7 +101,7 @@ circular.ref = circular assert.deepStrictEqual( pipe( J.stringify(circular), - E.mapLeft((e) => e instanceof Error && e.message.includes('Converting circular structure to JSON')) + E.mapLeft((e) => e.message.includes('Converting circular structure to JSON')) ), E.left(true) ) diff --git a/dtslint/ts3.5/Json.ts b/dtslint/ts3.5/Json.ts index 8e568d1c8..b51e83284 100644 --- a/dtslint/ts3.5/Json.ts +++ b/dtslint/ts3.5/Json.ts @@ -2,6 +2,15 @@ import * as E from '../../src/Either' import { pipe } from '../../src/function' import * as _ from '../../src/Json' +declare const s: string + +// +// parse +// + +// $ExpectType Either +_.parse(s) + // // stringify // @@ -34,5 +43,5 @@ _.stringify([{ a: 'a', b: 1 }]) _.stringify(abs) _.stringify([...abs]) -// $ExpectType Either +// $ExpectType Either pipe(E.right('a'), E.chainFirst(_.stringify)) diff --git a/src/Json.ts b/src/Json.ts index 4a188af53..b84f61f9b 100644 --- a/src/Json.ts +++ b/src/Json.ts @@ -34,7 +34,7 @@ export interface JsonArray extends ReadonlyArray {} * * @since 2.10.0 */ -export const parse = (s: string): Either => tryCatch(() => JSON.parse(s), identity) +export const parse = (s: string): Either => tryCatch(() => JSON.parse(s), identity as any) /** * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. @@ -50,18 +50,18 @@ export const parse = (s: string): Either => tryCatch(() => JSON.p * assert.deepStrictEqual( * pipe( * J.stringify(circular), - * E.mapLeft(e => e instanceof Error && e.message.includes('Converting circular structure to JSON')) + * E.mapLeft(e => e.message.includes('Converting circular structure to JSON')) * ), * E.left(true) * ) * * @since 2.10.0 */ -export const stringify = (a: A): Either => +export const stringify = (a: A): Either => tryCatch(() => { const s = JSON.stringify(a) if (typeof s !== 'string') { - throw new Error('Converting unsupported structure to JSON') + throw new TypeError('Converting unsupported structure to JSON') } return s - }, identity) + }, identity as any) diff --git a/test/Json.ts b/test/Json.ts index 060baf731..457843e58 100644 --- a/test/Json.ts +++ b/test/Json.ts @@ -17,10 +17,11 @@ describe('Json', () => { pipe( circular, _.stringify, - E.mapLeft((e) => (e as Error).message.includes('Converting circular structure to JSON')) + E.mapLeft((e) => e.message.includes('Converting circular structure to JSON')) ), E.left(true) ) + U.deepStrictEqual(_.stringify({ a: BigInt(1) }), E.left(new TypeError('Do not know how to serialize a BigInt'))) type Person = { readonly name: string readonly age: number @@ -28,6 +29,6 @@ describe('Json', () => { const person: Person = { name: 'Giulio', age: 45 } U.deepStrictEqual(pipe(person, _.stringify), E.right('{"name":"Giulio","age":45}')) - U.deepStrictEqual(_.stringify(undefined as any), E.left(new Error('Converting unsupported structure to JSON'))) + U.deepStrictEqual(_.stringify(undefined), E.left(new TypeError('Converting unsupported structure to JSON'))) }) })