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

Poor autocomplete inside object literal as the return value of a getter #58484

Open
whzx5byb opened this issue May 9, 2024 · 1 comment
Open

Comments

@whzx5byb
Copy link

whzx5byb commented May 9, 2024

πŸ”Ž Search Terms

object literal getter completion

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-beta#code/JYOwLgpgTgZghgYwgAgILIN7IFDOTAewIAoBKALk2QAcoDrowBPAOTgFsJKQBXdgI2jIAvgG5cyfnCiUsteo1YcuyXgKFjsw7NgQEQAZzDI4ldAF5MEvIRKkqUCGB5QQVvB48B6L8gA8ALTIYFBMwUzUoADmyABE1LHIABbQENYewjiewgA0Op5RTpLSZO6eyI7OrlTpnj7+QSFhzJEgMfGJKY61IunawkA

πŸ’» Code

interface A { 
  foo(): { propertyName: number };
  bar: { propertyName: number };
}

const a: A = {
    foo() { return {
        // <- try typing "p" here
      } 
    },

    get bar() {
      return { 
        // <- try typing "p" here
      }
    }
}

πŸ™ Actual behavior

😊 Good completions inside the function body:

1

😒 Poor completions inside the getter body:

2

πŸ™‚ Expected behavior

It should provide the same completions inside a getter, as it does inside a function.

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Andarist commented May 9, 2024

Those completions are computed based on the contextual type and it seems that return types of getters are not contextually-typed here:

interface Test1 {
  foo: (arg: number) => void;
}

const t1: Test1 = {
  get foo() {
    // Parameter 'arg' implicitly has an 'any' type.(7006)
    return (arg) => {};
  },
};

interface Test2 {
  bar: [string]
}

const t2: Test2 = {
  get bar() {
    // Type 'string[]' is not assignable to type '[string]'.
    //   Target requires 1 element(s) but source may have fewer.(2322)
    return ['']
  },
};

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