Search code examples
typescript

Strange typescript error:" the name g does not exist in the current scope"


All of a sudden, my code doesn't compile and tsc complains that "the name g does not exist in the current scope". This is a really funny error:

  • Nowhere in my code is a variable, class or interface named "g"
  • I'm not importing a module named "g"
  • The file itself is not named "g"

Here's some code:

export class SpawnMessage implements Message {
    tag: UID;
    id: number;
}

VisualStudio underlines the "e" of "export" in red and complains that "there is no name g". The same error message is sprinkled all across my code at the most peculiar places.

Well, I thought if g is so important, I'll just create it. I added a variable named g to the top of my code. The error messages didn't disappear, they just moved. For example, now one of my comments is apparently wrong. Really, I wouldn't believe this if someone told me about it, so I made a screenshot: ![g does not exist][1]

It's not just VisualStudio but tsc, too. I restarted the IDE and the shell, but the error persists. Naturally it changed places again.

What went wrong ?

UPDATE: I fixed the export import statement but that didn't affect the error. Here's the entire file on jsfiddle. The code is rather ugly, please bear in mind that this is work in progress. As a matter of fact I was just refactoring as the error appeared and haven't touched the file since.

http://jsfiddle.net/JT36W/

Right now the error is found in these three places:

  1. the closing curly brace of the Message interface
  2. the equals sign in the last line of the constructor of SpawnMessage
  3. some whitespace in front of the return statement of the method requestSpawn in the class Game

UPDATE 2: I restarted both VisualStudio and the command prompt. The error persisted. Then I restarted my computer, the error was still there. You can see that I was desperate. That's why I pushed it all to github, deleted my local copy and cloned the repo again. The error is gone. It's the exact same code, it's the exact same compiler. It's even the same path on my drive but tsc compiles the code.

I won't post this as an answer to my own question, since it's really unsatisfying. If one of you can explain this odd behaviour, maybe it's a known bug, i will gladly accept his answer.


Solution

  • I believe I've encountered this error a few times before. When it talks about "g" or "b" it's talking about the internal prototype inheritance chain it maintains for your classes. For me, this error or something similar would occur when the base class Message was defined AFTER the child SpawnMessage, in the code flow. Simply define the base class first.

    Hope it helps.