Skip to content

Improve compile-time checking for a case of passing instance's method as a callback #18527

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
Deilan opened this issue Sep 16, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Deilan
Copy link

Deilan commented Sep 16, 2017

TypeScript Version: 2.5.2

Repo with demo
Typescript Playground

Code

class Foo {
    private result: number = 42;

    public func(this: Foo): number {
        return this.result;
    }
}

function action(): void {
    console.log("Hello world!");
}

function bar(callbackFn: (this: void) => any, thisArg?: undefined): any;
function bar<T>(callbackFn: (this: T) => any, thisArg: T): any;
function bar<T, TResult>(callbackFn: (this: T) => TResult, thisArg: T): TResult {
    return callbackFn.call(thisArg);
}

const foo = new Foo();
const obj = new Object();

bar(action); // success - ok
bar(() => action()); // success - ok
bar(function() { return action(); }); // success - ok

bar(action, foo); // success - NOT ok, expected ERROR
bar(() => action(), foo); // success - NOT ok, expected ERROR
bar(function() { return action(); }, foo); // success - NOT ok, expected ERROR

bar(action, obj); // success - NOT ok, expected ERROR
bar(() => action(), obj); // success - NOT ok, expected ERROR
bar(function() { return action(); }, obj); // success - NOT ok, expected ERROR



bar(foo.func); // ERROR - ok
bar(() => foo.func()); // success - NOT ok, expected ERROR
bar(function() { return foo.func(); }); // success - NOT ok, expected ERROR

bar(foo.func, foo); // success - ok
bar(() => foo.func(), foo); // success - ok
bar(function() { return foo.func(); }, foo); // success - ok

bar(foo.func, obj); // success - ok
bar(() => foo.func(), obj); // success - ok
bar(function() { return foo.func(); }, obj); // success - ok



bar(obj.toString); // success - NOT ok, expected ERROR
bar(() => obj.toString()); // success - NOT ok, expected ERROR
bar(function() { return obj.toString(); }); // success - NOT ok, expected ERROR

bar(obj.toString, foo); // success - NOT ok, expected ERROR
bar(() => obj.toString(), foo); // success - NOT ok, expected ERROR
bar(function() { return obj.toString(); }, foo); // success - NOT ok, expected ERROR

bar(obj.toString, obj); // success - ok
bar(() => obj.toString(), obj); // success - ok
bar(function() { return obj.toString(); }, obj); // success - ok

Expected behavior:

bar(action, foo); // ERROR
bar(() => action(), foo); // ERROR
bar(function() { return action(); }, foo); // ERROR

bar(action, obj); // ERROR
bar(() => action(), obj); // ERROR
bar(function() { return action(); }, obj); // ERROR



bar(() => foo.func()); // ERROR
bar(function() { return foo.func(); }); // ERROR



bar(obj.toString); // ERROR
bar(() => obj.toString()); // ERROR
bar(function() { return obj.toString(); }); // ERROR

bar(obj.toString, foo); // ERROR
bar(() => obj.toString(), foo); // ERROR
bar(function() { return obj.toString(); }, foo); // ERROR

Actual behavior:

bar(action, foo); // success
bar(() => action(), foo); // success
bar(function() { return action(); }, foo); // success

bar(action, obj); // success
bar(() => action(), obj); // success
bar(function() { return action(); }, obj); // success



bar(() => foo.func()); // success
bar(function() { return foo.func(); }); // success



bar(obj.toString); // success
bar(() => obj.toString()); // success
bar(function() { return obj.toString(); }); // success

bar(obj.toString, foo); // success
bar(() => obj.toString(), foo); // success
bar(function() { return obj.toString(); }, foo); // success
@DanielRosenwasser
Copy link
Member

Hey @Deilan thanks for putting this together, but this looks like a duplicate of #7968.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Sep 16, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 2, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants