Skip to content

Commit 978d49d

Browse files
committed
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.
1 parent db232a7 commit 978d49d

File tree

6 files changed

+13
-7
lines changed

6 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v2.4.2
4+
5+
- Improved ESM TypeScript types.
6+
- More precise TypeScript replacer type.
7+
38
## v2.4.1
49

510
- More precise TypeScript types. The return type is now either `string`, `undefined` or `string | undefined` depending on the input.

esm/wrapper.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringify } from '../index.js';
1+
import { stringify } from '../index.js'
22

3-
export * from '../index.js';
4-
export default stringify;
3+
export * from '../index.js'
4+
export default stringify

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null)
1+
export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null | object)
22

33
export function stringify(value: undefined | symbol | ((...args: unknown[]) => unknown), replacer?: Replacer, space?: string | number): undefined
44
export function stringify(value: string | number | unknown[] | null | boolean | object, replacer?: Replacer, space?: string | number): string

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"typings": "index.d.ts",
4040
"devDependencies": {
4141
"@types/json-stable-stringify": "^1.0.34",
42-
"@types/node": "^16.11.1",
42+
"@types/node": "^18.11.18",
4343
"benchmark": "^2.1.4",
4444
"clone": "^2.1.2",
4545
"fast-json-stable-stringify": "^2.1.0",

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ test('invalid replacer being ignored', function (assert) {
279279

280280
// @ts-expect-error
281281
const actual = stringify(obj, 'invalidReplacer')
282-
const expected = stringify(obj)
282+
// @ts-expect-error
283+
const expected = stringify(obj, 'invalidReplacer')
283284
assert.equal(actual, expected)
284285

285286
assert.end()

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"noEmit": true,
99
"downlevelIteration": false,
1010
"experimentalDecorators": false,
11-
"moduleResolution": "node",
11+
"moduleResolution": "nodenext",
1212
"importHelpers": false,
1313
"target": "esnext",
1414
"module": "CommonJS",

0 commit comments

Comments
 (0)