Last active
April 21, 2017 13:09
-
-
Save radiosilence/85878b976ba7c2ff7ff4502a690b08a2 to your computer and use it in GitHub Desktop.
I am having a confuse about generics
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
interface Z { | |
potato: string | |
} | |
abstract class A<T> { // I want to indicate that T will always at least implement Z | |
protected foo(bar: T) { | |
console.log(bar.potato); // ERROR: [ts] Property 'potato' does not exist on type 'T' | |
} | |
} | |
class B extends A<Z> { | |
public bork() { | |
const z: Z = { | |
potato: 'tomato', | |
} | |
this.foo(z) | |
} | |
} | |
const b = new B(); | |
b.bork() | |
// Should output: 'potato' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment