Skip to content

Commit 9b12767

Browse files
authored
Add AbstractClass type (#559)
1 parent 9d00bac commit 9b12767

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ Click the type names for complete docs.
120120
- [`Primitive`](source/primitive.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
121121
- [`Class`](source/basic.d.ts) - Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
122122
- [`Constructor`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
123+
- [`AbstractClass`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
124+
- [`AbstractConstructor`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
123125
- [`TypedArray`](source/typed-array.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
124126
- [`ObservableLike`](source/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
125127

source/basic.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/Jav
1212
*/
1313
export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
1414

15+
/**
16+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
17+
18+
@category Class
19+
*/
20+
export type AbstractClass<T, Arguments extends unknown[] = any[]> = AbstractConstructor<T, Arguments> & {prototype: T};
21+
22+
/**
23+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
24+
25+
@category Class
26+
*/
27+
export type AbstractConstructor<T, Arguments extends unknown[] = any[]> = abstract new(...arguments_: Arguments) => T;
28+
1529
/**
1630
Matches a JSON object.
1731

test-d/abstract-class.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {expectAssignable, expectError} from 'tsd';
2+
import type {AbstractConstructor, AbstractClass} from '../index';
3+
4+
abstract class Foo {
5+
abstract fooMethod(): void;
6+
}
7+
8+
abstract class Bar {
9+
abstract barMethod(): void;
10+
}
11+
12+
function functionRecevingAbsClass<T>(cls: AbstractClass<T>) {
13+
return cls;
14+
}
15+
16+
function withBar<T extends AbstractConstructor<object>>(Ctor: T) {
17+
abstract class ExtendedBar extends Ctor {
18+
// eslint-disable-next-line @typescript-eslint/no-empty-function
19+
extendedBarMethod() {}
20+
}
21+
return ExtendedBar;
22+
}
23+
24+
function assertWithBar() {
25+
// This lacks `barMethod`.
26+
// @ts-expect-error
27+
class WrongConcreteExtendedBar extends withBar(Bar) {}
28+
29+
// This should be alright since `barMethod` is implemented.
30+
class CorrectConcreteExtendedBar extends withBar(Bar) {
31+
// eslint-disable-next-line @typescript-eslint/no-empty-function
32+
barMethod() {}
33+
}
34+
expectAssignable(functionRecevingAbsClass<Bar>(withBar(Bar)));
35+
expectAssignable(functionRecevingAbsClass<Bar>(CorrectConcreteExtendedBar));
36+
}
37+
38+
expectAssignable(functionRecevingAbsClass(Foo));
39+
expectError(functionRecevingAbsClass<Bar>(Foo));
40+
assertWithBar();

0 commit comments

Comments
 (0)