Created
September 26, 2019 16:38
-
-
Save qsona/64f52487a981b77a1c552a32e10403eb to your computer and use it in GitHub Desktop.
class inheritance and type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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