diff --git a/index.d.ts b/index.d.ts index 4735eba..0dfdf09 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ -export type Replacer = (key: string, value: any) => void; +export type Replacer = (key: string, value: any) => number | string | boolean | object | null | undefined; export type SortKeys = (a: string, b: string) => number; -export type JSONStringifyable = object | number | string; +export type JSONStringifyable = object | number | string | boolean; export interface Options { /** @@ -39,6 +39,7 @@ export interface Options { * Creates directories for you as needed. * * @example + * * import * as writeJsonFile from 'write-json-file'; * * writeJsonFile.sync('foo.json', {foo: true}); @@ -51,6 +52,7 @@ export function sync(filepath: string, data: JSONStringifyable, options?: Option * Creates directories for you as needed. * * @example + * * import writeJsonFile from 'write-json-file'; * * (async () => { diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..697bd43 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,52 @@ +import {expectType} from 'tsd-check'; +import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.'; + +(async () => { + expectType('🦄'); + expectType(1); + expectType(true); + expectType(new Date()); + expectType(['hello', 'world']); + expectType({unicorn: '🦄'}); + + expectType(() => 1); + expectType((a: string) => a.length); + expectType((a: string, b: string) => a.length - b.length); + + expectType(() => 1); + expectType(() => 'unicorn'); + expectType(() => true); + expectType(() => null); + expectType(() => undefined); + expectType(() => ({unicorn: '🦄'})); + expectType(() => ['unicorn', 1]); + expectType(() => () => 'foo'); + expectType((key: string) => key.toUpperCase()); + expectType((key: string, value: string) => (key + value).toUpperCase()); + + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'})); + expectType(await writeJsonFile('unicorn.json', '🦄')); + expectType(await writeJsonFile('date.json', new Date())); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length})); + expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); + + expectType(sync('unicorn.json', {unicorn: '🦄'})); + expectType(sync('unicorn.json', '🦄')); + expectType(sync('date.json', new Date())); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {indent: 4})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length})); + expectType(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); +})(); diff --git a/package.json b/package.json index 4b22bb3..927a891 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd-check" }, "files": [ "index.js", @@ -44,11 +44,12 @@ "devDependencies": { "ava": "*", "tempfile": "^2.0.0", + "tsd-check": "^0.1.0", "xo": "*" }, "xo": { "ignores": [ - "index.d.ts" + "*.ts" ] } }