Skip to content

[JavaScript] Type checking for function property behaves differently from method declaration syntax #42984

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
xl1 opened this issue Feb 26, 2021 · 3 comments

Comments

@xl1
Copy link
Contributor

xl1 commented Feb 26, 2021

Bug Report

On JavaScript type checking (checkJs option), object initializer with function property ({ foo: function () {} }) emits ts2339 error unexpectedly, if that function gets and sets other properties of this.

🔎 Search Terms

checkJs function method definitions

🕗 Version & Regression Information

  • v3 (at least v3.9.7 on playground) does not emit an error. v4 emits an error,

⏯ Playground Link

https://www.typescriptlang.org/play?declaration=false&useJavaScript=true#code/MYewdgzgLgBAHjAvDA3gKBpmBDAXDABgBoMsAjfY0zAMwFcxgoBLcAZQE8wps597GLcDAAUASlTUsmKAAtmEAHTYkMAIwBuKdNCQQAGwCmi-SADmIuQsVkxGmAHoHMAAoAnEAAdDbqBxgA5GQBMAAmIIYQMGAgsIZwCrDCft6BAkysYJzcvAGKIgBMAMxFAJxiUgC+JNIAtoZyIKHZPHDiktLSVkoqyJraWLoQBsamFt02dlU1WOlCWVytAOrMjXRQAIIQEMxmYPXc-AwZwu3onYPgw0Ym5pbySrZa0pVolRpAA

💻 Code

// @ts-check
const x = {
    a: 0,
    b: 0,
    functionSyntax: function () {
        this.a = 1;
        console.log(this.b); // Property 'b' does not exist on type 'functionSyntax'.(2339)
    },
    methodSyntax() {
        this.a = 1;
        console.log(this.b); // no error
    },
    functionSyntaxWithoutAssignment: function () {
        console.log(this.b); // no error
    }
};

🙁 Actual behavior

ts(2339) error Property '{name}' does not exist on type '{function name}'. is reported

🙂 Expected behavior

No error

The method declaration syntax ({ foo() {} }) does not emit an error. I think that function syntax should have the same behavior for this checking.

@MartinJohns
Copy link
Contributor

Intentional and mentioned in the FAQ. Check for bi-variance.

@xl1
Copy link
Contributor Author

xl1 commented Feb 26, 2021

I don't think it is related to variance..., but I found a change that seemed to be relevant: #39447 and understood its motivation.

With typescript v3, the code below generates an error. v4 is improved on this point.

// @ts-check
const constructors = {
    SomeClass: function () {
        this.a = 1; // Property 'a' does not exist on type '{ SomeClass: typeof SomeClass; }'.(2339)
    },
};
const x = new constructors.SomeClass();
console.log(x.a);

In this situation it is impossible to distinguish between constructors and non-constructor functions, so the current behavior is certainly convincing.

I have a lot of code that uses non-constructor function properties, so I would be happy if there was any workaround for this error.

@xl1
Copy link
Contributor Author

xl1 commented Mar 7, 2021

I will close this issue as I think this is an expected behavior.

@xl1 xl1 closed this as completed Mar 7, 2021
@xl1 xl1 changed the title [JavaScript] Type checking for function property behaves diferrently from method declaration syntax [JavaScript] Type checking for function property behaves differently from method declaration syntax Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants