Skip to content

Commit 058056b

Browse files
Add type definition tests
1 parent 58c5d57 commit 058056b

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

index.d.ts

Lines changed: 2 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
/**

index.test-d.ts

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

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 && tsd-check && ava"
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)