From 978d49d63606fbc7dec313ca77111dc3ead786a7 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 28 Dec 2022 11:41:24 +0100 Subject: [PATCH] fix: improve replacer type definition The replacer type may return an object while still guaranteeing that the overall return type is string. Change the tsconfig moduleResolution to nodenext to properly test the esm type definition. --- CHANGELOG.md | 5 +++++ esm/wrapper.d.ts | 6 +++--- index.d.ts | 2 +- package.json | 2 +- test.js | 3 ++- tsconfig.json | 2 +- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c4faf3..9801d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v2.4.2 + +- Improved ESM TypeScript types. +- More precise TypeScript replacer type. + ## v2.4.1 - More precise TypeScript types. The return type is now either `string`, `undefined` or `string | undefined` depending on the input. diff --git a/esm/wrapper.d.ts b/esm/wrapper.d.ts index a6e309f..2351eb1 100644 --- a/esm/wrapper.d.ts +++ b/esm/wrapper.d.ts @@ -1,4 +1,4 @@ -import { stringify } from '../index.js'; +import { stringify } from '../index.js' -export * from '../index.js'; -export default stringify; +export * from '../index.js' +export default stringify diff --git a/index.d.ts b/index.d.ts index e312909..442bda5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null) +export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null | object) export function stringify(value: undefined | symbol | ((...args: unknown[]) => unknown), replacer?: Replacer, space?: string | number): undefined export function stringify(value: string | number | unknown[] | null | boolean | object, replacer?: Replacer, space?: string | number): string diff --git a/package.json b/package.json index d46eeae..0a96ef4 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "typings": "index.d.ts", "devDependencies": { "@types/json-stable-stringify": "^1.0.34", - "@types/node": "^16.11.1", + "@types/node": "^18.11.18", "benchmark": "^2.1.4", "clone": "^2.1.2", "fast-json-stable-stringify": "^2.1.0", diff --git a/test.js b/test.js index a7073c7..f8e4f95 100644 --- a/test.js +++ b/test.js @@ -279,7 +279,8 @@ test('invalid replacer being ignored', function (assert) { // @ts-expect-error const actual = stringify(obj, 'invalidReplacer') - const expected = stringify(obj) + // @ts-expect-error + const expected = stringify(obj, 'invalidReplacer') assert.equal(actual, expected) assert.end() diff --git a/tsconfig.json b/tsconfig.json index 1a4ff3f..4904718 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "noEmit": true, "downlevelIteration": false, "experimentalDecorators": false, - "moduleResolution": "node", + "moduleResolution": "nodenext", "importHelpers": false, "target": "esnext", "module": "CommonJS",