1

I just installed fresh Visual Studio Code 1.23.1 on my MAC and opened a directory with one simple file main.ts (no tsconfig.json there):

function getFinalPrice(price: number, discount: number) {
  return price - price/discount;
}

console.log(getFinalPrice(100, 10));

The word console is underlined with a squiggly red line, and the error reads:

[ts] Cannot file the name console".

I checked the installation directory of VS Code - it has the file .../extensions/node_modules/typescript/lib/lib.es2016.full.d.ts, which has this line:

declare var console: Console;

Why my VS Code doesn't pick it up?

My colleague tried the same code and he doesn't see this error. The only difference is that I have a freshly installed VS Code. What can be the problem?

Update: after adding a simple tsconfig.json file, the error is gone.

{
    "compilerOptions": { 
        "noEmitOnError": true, 
        "target": "es5"
    }
  }

Is this a must to have tsconfig.json in VS Code projects?

2 Answers 2

1

Actually, the reason for this error is described in the VS Code docs at https://code.visualstudio.com/docs/languages/typescript#_typescript-files-and-projects

Without the tsconfig.json CS Code works in a File Scope mode and you can only use the code defined within the file, which was not the case with the console.

Sign up to request clarification or add additional context in comments.

Comments

0

You need a tsconfig.json file as this will specify the lib definitions to include - one of which will contain a definition for console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.