Skip to content

feat(49385): Inlay hint for type of literal-initialized constant is unnecessary #49412

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

Merged
merged 1 commit into from
Jun 15, 2022

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #49385

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jun 7, 2022
@a-tarasyuk
Copy link
Contributor Author

@DanielRosenwasser Is this the expected behavior for this feature? Or do we need to omit variables with type declarations completely?

@DanielRosenwasser
Copy link
Member

Or do we need to omit variables with type declarations completely?

Can you give an example?

@a-tarasyuk
Copy link
Contributor Author

@DanielRosenwasser I meant the following case

interface Foo {
  a: number;
}
const a /* : Foo */ = { a: 1 }
        ^^^^^^^^^ - is it acceptable to have a hint here?

@DanielRosenwasser
Copy link
Member

Hah, now we've got some more subtlety.

I would say no - first we shouldn't provide try to provide hints using structurally identical types. But for basic object types, my experience would be that that sort of hint stops being useful pretty quickly.

I guess this comes down to a few heuristics on "obvious" forms you should omit on for variables and parameter declarations

  • A const initialized to a primitive literal.

    const foo = "...";
  • A binding initialized with a type assertion

    const foo = { /*...*/} as SomeType;
    const bar = <SomeType>{ /*...*/};
    const baz = {/*...*/} as const;
  • A binding initialized with a new expression where the instance type name and the constructor name are identical.

    class Foo {}
    namespace asd { export class Foo {} }
    const foo = new Foo();
    const foo = new asd.Foo();
  • An object literal

    const foo = { a: 123, b: 456 };

@andrewbranch, @gabritto, and @jakebailey might have some thoughts.

@jakebailey
Copy link
Member

I think those exceptions are all reasonable to my eye.

@andrewbranch andrewbranch merged commit 180bc4c into microsoft:main Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Inlay hint for type of literal-initialized constant is unnecessary
5 participants