Skip to content

Commit

Permalink
Add type definition tests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Sep 3, 2018
1 parent 58c5d57 commit 514b04c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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});
Expand All @@ -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 () => {
Expand Down
52 changes: 52 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {expectType} from 'tsd-check';
import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.';

(async () => {
expectType<JSONStringifyable>('πŸ¦„');
expectType<JSONStringifyable>(1);
expectType<JSONStringifyable>(true);
expectType<JSONStringifyable>(new Date());
expectType<JSONStringifyable>(['hello', 'world']);
expectType<JSONStringifyable>({unicorn: 'πŸ¦„'});

expectType<SortKeys>(() => 1);
expectType<SortKeys>((a: string) => a.length);
expectType<SortKeys>((a: string, b: string) => a.length - b.length);

expectType<Replacer>(() => 1);
expectType<Replacer>(() => 'unicorn');
expectType<Replacer>(() => true);
expectType<Replacer>(() => null);
expectType<Replacer>(() => undefined);
expectType<Replacer>(() => ({unicorn: 'πŸ¦„'}));
expectType<Replacer>(() => ['unicorn', 1]);
expectType<Replacer>(() => () => 'foo');
expectType<Replacer>((key: string) => key.toUpperCase());
expectType<Replacer>((key: string, value: string) => (key + value).toUpperCase());

expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}));
expectType<void>(await writeJsonFile('unicorn.json', 'πŸ¦„'));
expectType<void>(await writeJsonFile('date.json', new Date()));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {detectIndent: true}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {indent: ' '}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {indent: 4}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {mode: 0o666}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {replacer: ['unicorn', 1]}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {replacer: () => 'unicorn'}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: () => -1}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: true}));

expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}));
expectType<void>(sync('unicorn.json', 'πŸ¦„'));
expectType<void>(sync('date.json', new Date()));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {detectIndent: true}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {indent: ' '}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {indent: 4}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {mode: 0o666}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {replacer: ['unicorn', 1]}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {replacer: () => 'unicorn'}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: () => -1}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
expectType<void>(sync('unicorn.json', {unicorn: 'πŸ¦„'}, {sortKeys: true}));
})();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
Expand Down Expand Up @@ -44,11 +44,12 @@
"devDependencies": {
"ava": "*",
"tempfile": "^2.0.0",
"tsd-check": "^0.1.0",
"xo": "*"
},
"xo": {
"ignores": [
"index.d.ts"
"*.ts"
]
}
}

0 comments on commit 514b04c

Please sign in to comment.