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

Function with this type can be assigned to a type without #22289

Closed
hesselink opened this issue Mar 2, 2018 · 7 comments
Closed

Function with this type can be assigned to a type without #22289

hesselink opened this issue Mar 2, 2018 · 7 comments
Labels
Bug A bug in TypeScript

Comments

@hesselink
Copy link

TypeScript Version: Version 2.8.0-dev.20180302

Search Terms: strict this assigned property

Code

The following code, compiled with --strict, gives a type error:

class C {
  f(this: this) : void {}
}
const c : C = new C();
const f = c.f;
f();

This correctly prints "error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'C'."

However, I can assign a type to the variable f without the this context, and things will typecheck (but fail if the function actually uses this internally):

class C {
  f(this: this) : void {}
}
const c : C = new C();
const f : () => void = c.f;
f();

Expected behavior:

The code gives a type error like "Type '(this: C) => void' is not assignable to type '() => void'.

Actual behavior:

The code compiles without errors.

Playground Link: here

Related Issues:

@ericanderson
Copy link
Contributor

Also fails on 2.7.2

@sebastiaanvisser
Copy link

sebastiaanvisser commented Mar 2, 2018

Note that this issue has slightly broader consequences than the situation sketched above.

Because by default class methods don't auto-infer a this type on their signature you can pass them directly (unbound to this) as a callback to another function take the signature without this. This clearly causes runtime errors:

function twice(f: () => number) {
    alert(2 * f())
}

class C {
    constructor(private v: number) { }
    get() { return this.v }
}

const c: C = new C(2);

// Both typecheck:
twice(() => c.get()); // Will alert 4
twice(c.get); // Will alert NaN

@ghost ghost added the Bug A bug in TypeScript label Mar 2, 2018
@Conaclos
Copy link

Conaclos commented Mar 3, 2018

The error is not reported even if the option "strict" is enabled.

declare const method: (this: Date) => void
declare let f: () => void
f = method // no error in TS 2.8-dev

@mattmccutchen
Copy link
Contributor

Looks like a duplicate of #7968.

@hesselink
Copy link
Author

@mattmccutchen That seems to be about a flag to change the default this type for functions, to provide better call-site and assignability checking. This is about the assignability checking itself not catching a type error, so I think this is a different issue.

@mattmccutchen
Copy link
Contributor

@hesselink As #7968 suggests, the current behavior is that when the this type of a function signature is not specified, it defaults to any for the purpose of assignability checking. That's why the assignability check is not reporting an error in your example. So it's the same issue AFAICT.

@hesselink
Copy link
Author

Ah you're right. I guess I'll close this ticket and subscribe to that one.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants