Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
interface StringifyFunction {
(value: undefined | symbol | ((...args: unknown[]) => unknown), replacer?: stringify.Replacer, space?: string | number): undefined
(value: string | number | unknown[] | null | boolean | object, replacer?: stringify.Replacer, space?: string | number): string
(value: unknown, replacer?: ((key: string, value: unknown) => unknown) | (number | string)[] | null | undefined, space?: string | number): string | undefined
}

declare const stringify: StringifyFunction & {
configure(options: stringify.StringifyOptions): StringifyFunction
declare const stringify: stringify.StringifyFunction & {
configure(options: stringify.StringifyOptions): stringify.StringifyFunction
stringify: typeof stringify
default: typeof stringify
}

declare namespace stringify {
export interface StringifyFunction {
(value: undefined | symbol | ((...args: unknown[]) => unknown), replacer?: Replacer, space?: string | number): undefined
(value: string | number | unknown[] | null | boolean | object, replacer?: Replacer, space?: string | number): string
(value: unknown, replacer?: ((key: string, value: unknown) => unknown) | (number | string)[] | null | undefined, space?: string | number): string | undefined
}

export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null | object)

export interface StringifyOptions {
Expand Down
6 changes: 5 additions & 1 deletion test-fixtures/types.cts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import stringify = require('safe-stable-stringify')
import type { Replacer, StringifyOptions } from 'safe-stable-stringify'
import type { Replacer, StringifyFunction, StringifyOptions } from 'safe-stable-stringify'

const options: StringifyOptions = { deterministic: false }
const replacer: Replacer = ['value']
Expand All @@ -17,6 +17,10 @@ const configuredResult: string = configured('value')
const omitted: undefined = configured(undefined)
configured({ configuredResult, omitted })

const typed: StringifyFunction = stringify.configure(options)
const namespacedTyped: stringify.StringifyFunction = typed
namespacedTyped({ value: 1 })

// @ts-expect-error
configured.stringify({ value: 1 })
// @ts-expect-error
Expand Down
5 changes: 4 additions & 1 deletion test-fixtures/types.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stringify, { configure, stringify as named, type Replacer, type StringifyOptions } from 'safe-stable-stringify'
import stringify, { configure, stringify as named, type Replacer, type StringifyFunction, type StringifyOptions } from 'safe-stable-stringify'

const options: StringifyOptions = { deterministic: false }
const replacer: Replacer = ['value']
Expand All @@ -11,6 +11,9 @@ const configuredResult: string = configured('value')
const omitted: undefined = configured(undefined)
configured({ configuredResult, omitted })

const typed: StringifyFunction = configure(options)
typed({ value: 1 })

// @ts-expect-error
configured.stringify({ value: 1 })
// @ts-expect-error
Expand Down