type-fest/test-d/asyncify.ts
2022-12-08 20:19:23 +01:00

20 lines
783 B
TypeScript

import {expectType, expectError} from 'tsd';
import type {Asyncify} from '../index';
declare function getFooSync(name: string): RegExp;
declare function getFooWithThisArgumentSync(this: Date, name: string): RegExp;
// Basic usage.
declare const getFooAsync1: Asyncify<typeof getFooSync>;
expectType<(name: string) => Promise<RegExp>>(getFooAsync1);
// Noops with async functions.
declare const getFooAsync2: Asyncify<typeof getFooAsync1>;
expectType<typeof getFooAsync1>(getFooAsync2);
// Respects `thisArg`.
declare const getFooWithThisArgumentAsync1: Asyncify<typeof getFooWithThisArgumentSync>;
const callResult = getFooWithThisArgumentAsync1.call(new Date(), 'foo');
expectType<Promise<RegExp>>(callResult);
expectError(getFooWithThisArgumentAsync1.call('not-date', 'foo'));