Skip to content

Function typechecking doesn't work for methods. #35930

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

Closed
kwasimensah opened this issue Dec 31, 2019 · 2 comments
Closed

Function typechecking doesn't work for methods. #35930

kwasimensah opened this issue Dec 31, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kwasimensah
Copy link

Search Terms:
method typechecking

Code

interface DoSomething {
    (): number;
}

function doubleNumber(generator: DoSomething) {
    return generator() * 2;
}

function return42() {
    return 42;
}

console.log(doubleNumber(return42));

class ArbitraryNumber {
    constructor(private readonly _seed: number) { }

    seed(): number { return this._seed;}
}

const test = new ArbitraryNumber(5);
// this line will cause an error because *this* won't be defined inside of seed()
console.log(doubleNumber(test.seed));

Expected behavior:

The typechecker complains because the implicit this isn't supplied when calling test.seed/i.e. you're forced to bind with an actual object.

Actual behavior:

This is a runtime error.

Playground Link: ! A link to a Playground "Share" link which demonstrates behavior

https://www.typescriptlang.org/play/index.html#code/JYOwLgpgTgZghgYwgAgCIHsDK6C2EwAWoA5sgN4CwAUMrcgBQCUAXMiAK44BG0A3NQF9q1GOxAIwwdCGQATdOy4AbCADlOPKPWIQQ0OGHRRWGbHkIlG5anWRR87KDJ16oBo02QAqZACZ+VEJUImISUjL2YI4gACy+npQ0dJHRyHEBQdQI0gDO6CoAdEroxPTyiirq3ND0KU5xjIwBWUpwOTnIAIJQXMBgblAAnlWa1km02SA5-ewSHgAOUMAAbgYo9nDyIEqDyAD6ORAQsqwc1VBWZMiZ48iHx0ynGtDkdg5OyBY5BQdHsrxBG6TaafCAggC8bAgAHcuj0+gNhs8tABWJpZXL5CBFEplBTKNTI+iQaYFe6yRrNYJUIA

Related Issues: ! Did find other bugs that looked similar?

@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #7968.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 6, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants