Skip to content

TS: cannot add imports for "Event" #28739

Closed

Description

This never gives me a code action to add the import, probably because "Event" is already defined in the DOM:

image

Activity

  1. removed their assignment
    on Nov 29, 2018
  2. mjbvz commented on Nov 29, 2018

    @mjbvz
    Contributor

    I believe the request is to understand that Event<T> does not match the non-generic global Event type from lib.dom.d.ts and may be defined somewhere else.

    Not sure if this makes sense since though since accidentally trying to use a non-generic type as a generic could also be a programmer error which should not result in an import quick fix

  3. added
    Suggestion
    In Discussion
    Domain: LS: Refactorings
    on Nov 30, 2018
  4. alexdima commented on Oct 31, 2019

    @alexdima
    Member

    I often encounter this issue with classes Position and Range. They have nothing to do with generics... It appears the lib.d.ts contains definitions for those too and the quick fix never realizes the global definition is not what I want (in order to suggest an import quick fix)

    Perhaps the import quick fix should always be presented on a symbol that is available from a global .d.ts, but which can be imported from somewhere else in the project?

  5. added
    VS Code Priority
    on Aug 12, 2020
  6. RyanCavanaugh commented on Aug 19, 2020

    @RyanCavanaugh
    Member

    The auto-imports for these symbols are already in the completion list, so that's the best way to import these if that's what you intend to do.

    Apart from the edge cases where the reference to the type is provably wrong (which is, AFAICT, only when their generic arities don't match, which I imagine is very rare for any two types of the same name), then there's not really anything we can do in terms of a "quick fix" -- once the reference to the type is legal, any further errors or unexpected behavior aren't distinguishable from normal typos or misunderstanding. More problematically, even offering something like "Overwrite the reference to Position with an auto-import at the place it was declared" might alter a file other than the one you're editing and cause downstream problems elsewhere. At the end of the day the editor can't be sitting around second-guessing code that is facially valid without creating even worse user friction, performance problems, or both.

  7. bpasero commented on Aug 20, 2020

    @bpasero
    Author

    Sorry I do not follow. In VSCode I cannot import our Event type because the one in lib.dom.d.ts is assumed:

    image

    Please let me know how I can clarify this better.

  8. bpasero commented on Aug 20, 2020

    @bpasero
    Author

    Just to give you an example of the pain I have to go through each time this happens:

    • find a file that has the import I need
    • copy it over

    recording

    //cc Daniel Rosenwasser (@DanielRosenwasser)

  9. DanielRosenwasser commented on Aug 20, 2020

    @DanielRosenwasser
    Member

    Benjamin Pasero (@bpasero) Yeah, I know, this is painful. We had a sort of mixed discussion on this.

    The problem is that there's no way to know until after you've used a type that it's wrong. In the case of Event, you can imagine a quick fix to suggest another type where the heuristic is "this one takes type arguments, this one doesn't". That's a potentially reasonable one, and I was personally not against it, but a few people in the editor sync felt like that could potentially be noisy if we suggested the wrong type.

    In the case of Alexandru Dima (@alexdima)'s example, there's no way until any arbitrary operation to know that the type is "wrong"; even if you can figure that out, figuring out which type is "right" could be expensive to do it accurately.

    Now you mentioned that there's no way to auto-import Event because the global overrides it. That shouldn't be the case, but when we experimented with it in the editor sync, we noticed that a lot of the time, auto-imports are significantly deprioritized to the point where they aren't noticed.

    Trying it out on your codebase, I'm definitely seeing Event come up (though unfortunately it looks like there are duplicates?)

    eventAutoImportsInVsCodeCodebase

  10. bpasero commented on Aug 20, 2020

    @bpasero
    Author

    Daniel Rosenwasser (@DanielRosenwasser) ah yeah good point about the import when you type the name, that helps. I think I often just copy this over from an interface and then miss the suggested auto imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: LS: RefactoringsIn DiscussionSuggestionVS Code Priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions