Skip to content

Commit 514b04c

Browse files
SamVerschuerensindresorhus
authored andcommitted
Add type definition tests (#18)
1 parent 58c5d57 commit 514b04c

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export type Replacer = (key: string, value: any) => void;
1+
export type Replacer = (key: string, value: any) => number | string | boolean | object | null | undefined;
22
export type SortKeys = (a: string, b: string) => number;
3-
export type JSONStringifyable = object | number | string;
3+
export type JSONStringifyable = object | number | string | boolean;
44

55
export interface Options {
66
/**
@@ -39,6 +39,7 @@ export interface Options {
3939
* Creates directories for you as needed.
4040
*
4141
* @example
42+
*
4243
* import * as writeJsonFile from 'write-json-file';
4344
*
4445
* writeJsonFile.sync('foo.json', {foo: true});
@@ -51,6 +52,7 @@ export function sync(filepath: string, data: JSONStringifyable, options?: Option
5152
* Creates directories for you as needed.
5253
*
5354
* @example
55+
*
5456
* import writeJsonFile from 'write-json-file';
5557
*
5658
* (async () => {

index.test-d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {expectType} from 'tsd-check';
2+
import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.';
3+
4+
(async () => {
5+
expectType<JSONStringifyable>('🦄');
6+
expectType<JSONStringifyable>(1);
7+
expectType<JSONStringifyable>(true);
8+
expectType<JSONStringifyable>(new Date());
9+
expectType<JSONStringifyable>(['hello', 'world']);
10+
expectType<JSONStringifyable>({unicorn: '🦄'});
11+
12+
expectType<SortKeys>(() => 1);
13+
expectType<SortKeys>((a: string) => a.length);
14+
expectType<SortKeys>((a: string, b: string) => a.length - b.length);
15+
16+
expectType<Replacer>(() => 1);
17+
expectType<Replacer>(() => 'unicorn');
18+
expectType<Replacer>(() => true);
19+
expectType<Replacer>(() => null);
20+
expectType<Replacer>(() => undefined);
21+
expectType<Replacer>(() => ({unicorn: '🦄'}));
22+
expectType<Replacer>(() => ['unicorn', 1]);
23+
expectType<Replacer>(() => () => 'foo');
24+
expectType<Replacer>((key: string) => key.toUpperCase());
25+
expectType<Replacer>((key: string, value: string) => (key + value).toUpperCase());
26+
27+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}));
28+
expectType<void>(await writeJsonFile('unicorn.json', '🦄'));
29+
expectType<void>(await writeJsonFile('date.json', new Date()));
30+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
31+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
32+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
33+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
34+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
35+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
36+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
37+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
38+
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));
39+
40+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}));
41+
expectType<void>(sync('unicorn.json', '🦄'));
42+
expectType<void>(sync('date.json', new Date()));
43+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
44+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
45+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
46+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
47+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
48+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
49+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
50+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
51+
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));
52+
})();

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd-check"
1717
},
1818
"files": [
1919
"index.js",
@@ -44,11 +44,12 @@
4444
"devDependencies": {
4545
"ava": "*",
4646
"tempfile": "^2.0.0",
47+
"tsd-check": "^0.1.0",
4748
"xo": "*"
4849
},
4950
"xo": {
5051
"ignores": [
51-
"index.d.ts"
52+
"*.ts"
5253
]
5354
}
5455
}

0 commit comments

Comments
 (0)