Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why doesn't an implementation use the types on its parameters from its interface? #6854

Closed
HomenSimpsor opened this issue Feb 2, 2016 · 3 comments
Labels
Suggestion An idea for TypeScript

Comments

@HomenSimpsor
Copy link

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.

(Playground)

The implementation shows the return type of withdraw but not type for the parameter:

image

@ahejlsberg
Copy link
Member

Duplicate of #3667. Fix available in #6118.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Feb 2, 2016
@ahejlsberg
Copy link
Member

Actually, #6118 doesn't affect methods, only property initializers. Reopening as suggestion.

@ahejlsberg ahejlsberg reopened this Feb 2, 2016
@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript and removed Duplicate An existing issue was already created labels Feb 2, 2016
@sandersn
Copy link
Member

sandersn commented Feb 2, 2016

Commit 055ae8c 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
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants