Skip to content

Instantly share code, notes, and snippets.

@qsona
Created September 26, 2019 16:38
Show Gist options
  • Save qsona/64f52487a981b77a1c552a32e10403eb to your computer and use it in GitHub Desktop.
Save qsona/64f52487a981b77a1c552a32e10403eb to your computer and use it in GitHub Desktop.
class inheritance and type
abstract class A<T> {
a() { return new Map<string, T>(); }
static b(): number { return 1; }
}
class X<T> extends A<T> {}
class Y extends A<number> {}
new X<number>().a(); // OK
new Y().a(); // OK
X.b(); // OK
Y.b(); // OK
// やりたいこと
const arr /* : 型 */ = [X, Y];
arr[0].b(); // OK
const arr1: (typeof A)[] = [X, Y]; // error: Type 'typeof Y' is not assignable to type 'typeof A'.
const arr2: { new(): A<any> }[] = [X, Y];
arr2[0].b(); // error: Property 'b' does not exist on type 'new () => A<any>'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment