Skip to content

Commit

Permalink
Merge pull request #15 from dotcore64/chore/use-tsd
Browse files Browse the repository at this point in the history
Replace tsc with tsd for type testing
  • Loading branch information
perrin4869 committed Jul 4, 2022
2 parents 805d976 + e72cf26 commit 1bbd052
Show file tree
Hide file tree
Showing 5 changed files with 2,578 additions and 972 deletions.
15 changes: 13 additions & 2 deletions .eslintrc.json
Expand Up @@ -7,7 +7,8 @@
},
"rules": {
"unicorn/prevent-abbreviations": 0,
"eslint-comments/no-unused-disable": "error"
"eslint-comments/no-unused-disable": "error",
"import/no-extraneous-dependencies": ["error", { "devDependencies": ["index.test-d.ts", "test/*.js"] }]
},
"overrides": [
{
Expand All @@ -33,5 +34,15 @@
"@typescript-eslint/no-redeclare": ["error"]
}
}
]
],
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts"]
},
"import/resolver": {
"typescript": {
"alwaysTryTypes": true
}
}
}
}
35 changes: 18 additions & 17 deletions test/types.ts → index.test-d.ts
@@ -1,39 +1,40 @@
// eslint-disable-next-line import/no-unresolved,n/no-extraneous-import
// eslint-disable-next-line n/no-extraneous-import
import fromCallback from 'p-from-callback';
import { expectAssignable, expectType } from 'tsd';

fromCallback((cb) => cb(undefined, 'foo')) as Promise<'foo'>;
fromCallback((cb) => cb(undefined, 'foo')) as Promise<string>;
expectType<Promise<'foo'>>(fromCallback((cb) => cb(undefined, 'foo')));
expectAssignable<Promise<string>>(fromCallback((cb) => cb(undefined, 'foo')));
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo')) as Promise<number>;
fromCallback<'foo'>((cb) => cb(undefined, 'foo')) as Promise<'foo'>;
expectType<Promise<'foo'>>(fromCallback<'foo'>((cb) => cb(undefined, 'foo')));

fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<string>;
fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<'foo'>;
expectAssignable<Promise<string>>(fromCallback((cb) => cb(undefined, 'foo', 'bar')));
expectType<Promise<'foo'>>(fromCallback((cb) => cb(undefined, 'foo', 'bar')));
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<'bar'>;
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<number>;
fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<string>;
fromCallback((cb) => cb(undefined, 'foo'), true) as Promise<string[]>;
expectAssignable<string>(await fromCallback((cb) => cb(undefined, 'foo', 'bar')));
expectAssignable<string[]>(await fromCallback((cb) => cb(undefined, 'foo'), true));
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo'), true) as Promise<string>;
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo', 'bar')) as Promise<string[]>;
fromCallback((cb) => cb(undefined, 'foo', 'bar'), true) as Promise<string[]>;
expectAssignable<string[]>(await fromCallback((cb) => cb(undefined, 'foo', 'bar'), true));
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo', 'bar'), true) as Promise<number[]>;

fromCallback((cb) => cb()) as Promise<undefined>;
expectType<undefined>(await fromCallback((cb) => cb()));
// eslint-disable-next-line unicorn/no-useless-undefined
fromCallback((cb) => cb(undefined, undefined)) as Promise<undefined>;
fromCallback((cb) => cb(new Error('message'))) as Promise<undefined>;
expectType<undefined>(await fromCallback((cb) => cb(undefined, undefined)));
expectType<undefined>(await fromCallback((cb) => cb(new Error('message'))));
// @ts-expect-error
fromCallback((cb) => cb(new Error('message'), 'foo')) as Promise<undefined>;
// @ts-expect-error
fromCallback((cb) => cb(new Error('message'))) as Promise<number>;

fromCallback((cb) => cb(), true) as Promise<[]>;
fromCallback((cb) => cb('foo'), true) as Promise<[]>;
expectType<[]>(await fromCallback((cb) => cb(), true));
expectType<[]>(await fromCallback((cb) => cb('foo'), true));
// @ts-expect-error
fromCallback((cb) => cb(), true) as Promise<undefined>;
// @ts-expect-error
Expand All @@ -45,9 +46,9 @@ fromCallback((cb) => cb('error', 'foo'), true) as Promise<undefined>;
fromCallback((cb) => cb === 'foo');

let foo: boolean;
fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo) as Promise<string>;
fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo) as Promise<string[]>;
fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo) as Promise<string | string[]>;
expectAssignable<string>(await fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo));
expectAssignable<string[]>(await fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo));
expectAssignable<string | string[]>(await fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo));
// @ts-expect-error
fromCallback((cb) => cb(undefined, 'foo', 'bar'), foo) as Promise<number>;
// @ts-expect-error
Expand Down

0 comments on commit 1bbd052

Please sign in to comment.