Skip to content

Add type definition tests #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because of a rendering issue. We can remove it though. Eventually it will be fixed (probably). microsoft/TypeScript#15749

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is so weird 🤔

I was wondering why the @example didn't look good, makes sense now

* 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 () => {
Copy link
Contributor

@DenisCarriere DenisCarriere Aug 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamVerschueren This must be the most intense Typescript test I've ever seen 😮

Good job! 👍

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"
]
}
}