Created
July 14, 2021 17:30
-
-
Save captain-yossarian/e2fc9c9e0b673ceb1012328dc5c4e192 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Base { | |
base = 'base' | |
} | |
class A extends Base { | |
a = 'a' | |
} | |
class B extends Base { | |
b = 'b' | |
} | |
class C { | |
c = 'c' | |
} | |
const fn = < | |
T extends typeof Base, | |
Instance extends InstanceType<T> | |
>(Cls: T & (new () => Instance), prop: keyof Instance, | |
) => new Cls()[prop]; | |
const result = fn(A, 'base') // ok | |
const result2 = fn(B, 'b') // ok | |
const result3 = fn(Base, 'base') // ok | |
const result4 = fn(A, 'b') // expected error | |
const result5 = fn(B, 'a') // expected error | |
const result6 = fn(C, 'x') // expected error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment