Skip to content

Commit

Permalink
fix: improve replacer type definition
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BridgeAR committed Dec 28, 2022
1 parent db232a7 commit 978d49d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions esm/wrapper.d.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noEmit": true,
"downlevelIteration": false,
"experimentalDecorators": false,
"moduleResolution": "node",
"moduleResolution": "nodenext",
"importHelpers": false,
"target": "esnext",
"module": "CommonJS",
Expand Down

0 comments on commit 978d49d

Please sign in to comment.