TypeScript
TypeScript copied to clipboard
Why doesn't an implementation use the types on its parameters from its interface?
interface Bank {
balance: number;
withdraw(x: number): number;
}
class PowellCountyBank implements Bank {
balance: number = 0;
withdraw(x) {
return (this.balance - x);
}
}
var b = new PowellCountyBank();
console.log(b.withdraw({}));
// ^ Expect type error here.
The implementation shows the return type of withdraw but not type for the parameter:

Duplicate of #3667. Fix available in #6118.
Actually, #6118 doesn't affect methods, only property initializers. Reopening as suggestion.
Commit 055ae8cd5ca9838ad03a37f2386802c7cebea1b3 from #6118 adds method contextual typing as well. Here's a cut-down sample from the test (it works for extending classes, too):
interface Ringer {
ring: (times: number) => void;
}
class Alarm implements Ringer {
str: string;
ring(times) {
this.str = times; // error
}
}