Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@xl1

Description

@xl1
Issue body actions

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.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.