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

Object literals use contextual type for this when available and ignore type of literal itself. #15157

Closed
sandersn opened this issue Apr 12, 2017 · 1 comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@sandersn
Copy link
Member

Object literals with no contextual type use the type of the literal itself for this. However, when there is a contextual type, that gets used for this to the exclusion of the literal's own type. The type for this should actually be the intersection of the contextual type and the literal's own type.

class B { b: any }
class D extends B { d: any }
type Ctx = { m(): B, m2(): any }
let o: Ctx = {
    m() {
        return new D()
    },
    m2() {
        this.m()/**/
    }
}

this.m() should be of type D but is actually of type B.

I don't know how important this is; the current behaviour is pretty good but it did break a couple of times in our real-world code base. See comments on the original PR #14141 for details. MockJax and an Angular test broke.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Apr 19, 2017
@RyanCavanaugh
Copy link
Member

This doesn't seem to have affected anyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants