Skip to content

Incorrect suggestion: "Cannot find name 'global'. Did you mean 'global'?" #42209

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
mkantor opened this issue Jan 4, 2021 · 3 comments · Fixed by #42262
Closed

Incorrect suggestion: "Cannot find name 'global'. Did you mean 'global'?" #42209

mkantor opened this issue Jan 4, 2021 · 3 comments · Fixed by #42262
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@mkantor
Copy link
Contributor

mkantor commented Jan 4, 2021

Bug Report

🔎 Search Terms

"declare global", "error suggestion", "cannot find name", "did you mean"

🕗 Version & Regression Information

This is the behavior in every version I tried (3.3.3-4.1.3 and 4.2.0-dev.20210104), and I reviewed the FAQ for entries about error messages.

⏯ Playground Link

Playground link with relevant code

💻 Code

export {}
declare global { const x: any }
global.x

🙁 Actual behavior

The error message is Cannot find name 'global'. Did you mean 'global'? ts(2552). This is a legit error, but the suggestion is wrong.

🙂 Expected behavior

An error message that doesn't have an incorrect suggestion. Perhaps just Cannot find name 'global'. with no suggestion at all?

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 4, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 4, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jan 4, 2021
@ExE-Boss
Copy link
Contributor

ExE-Boss commented Jan 5, 2021

It should suggest using globalThis instead.

@mkantor
Copy link
Contributor Author

mkantor commented Jan 5, 2021

@ExE-Boss I'm not sure about that, at least with code like my example above, because globalThis also produces an error:

export {}
declare global { const x: any }
globalThis.x // Property 'x' does not exist on type 'typeof globalThis'.

It's the same deal as what you pointed out in #39504. You need to use var to attach x to globalThis.

A more universal fix could be "just write x", but it might not be worth adding a new type of suggestion for this presumably-rare situation.

@mkantor
Copy link
Contributor Author

mkantor commented Jan 5, 2021

I started playing around with checker.ts. If the desired behavior is to have no suggestion, I think something like this could do the trick:

--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -2028,6 +2028,10 @@ namespace ts {
                         let suggestion: Symbol | undefined;
                         if (suggestedNameNotFoundMessage && suggestionCount < maximumSuggestionCount) {
                             suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning);
+                            // Avoid suggesting "global" if it refers to a global scope augmentation declaration.
+                            if (suggestion && isAmbientModule(suggestion.valueDeclaration) && isGlobalScopeAugmentation(suggestion.valueDeclaration)) {
+                                suggestion = undefined;
+                            }
                             if (suggestion) {
                                 const suggestionName = symbolToString(suggestion);
                                 const diagnostic = error(errorLocation, suggestedNameNotFoundMessage, diagnosticName(nameArg!), suggestionName);

However this is my first foray into the compiler internals and I don't have my bearings yet, so I'm not sure this is the best approach. This check could also happen inside getSuggestedSymbolForNonexistentSymbol, for example, or maybe there's a more general notion of "things that shouldn't be suggested for value positions" that should be captured somewhere.

Anyway, if this is vaguely on the right track let me know and I'll draft a pull request. If not, any guidance would be appreciated!

EDIT: I opened #42262 which uses this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants