Skip to content

Commit

Permalink
Add an optional “Argument” type to the “Class” type (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiospampinato authored and sindresorhus committed Jul 26, 2019
1 parent 51f9a3b commit 8b8dd4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/basic.d.ts
Expand Up @@ -17,7 +17,7 @@ export type Primitive =
/**
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
*/
export type Class<T = unknown> = new(...arguments_: any[]) => T;
export type Class<T = unknown, Arguments extends any[] = any[]> = new(...arguments_: Arguments) => T;

/**
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
Expand Down
22 changes: 22 additions & 0 deletions test-d/class.ts
@@ -0,0 +1,22 @@
import {expectError} from 'tsd';
import {Class} from '..';

class Foo {
constructor(x: number, y: any) {
console.log(x, y);
}

method(): void {}
}

function fn(Cls: Class<Foo>): Foo {
return new Cls(1, '', 123);
}

function fn2(Cls: Class<Foo, [number, number]>): Foo {
expectError(new Cls(1, ''));
return new Cls(1, 2);
}

fn(Foo);
fn2(Foo);

0 comments on commit 8b8dd4d

Please sign in to comment.