diff --git a/index.d.ts b/index.d.ts index 955403b..c505af4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/test-fixtures/types.cts b/test-fixtures/types.cts index 75cb77e..be5a81f 100644 --- a/test-fixtures/types.cts +++ b/test-fixtures/types.cts @@ -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'] @@ -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 diff --git a/test-fixtures/types.mts b/test-fixtures/types.mts index 2cf2d82..92ab37e 100644 --- a/test-fixtures/types.mts +++ b/test-fixtures/types.mts @@ -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'] @@ -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