-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
It should suggest using |
@ExE-Boss I'm not sure about that, at least with code like my example above, because 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 A more universal fix could be "just write |
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 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. |
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
🙁 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?The text was updated successfully, but these errors were encountered: