Visual Studio Code complains that it “Cannot find namespace” for types defined in *.d.ts files

前端 未结 4 1201
甜味超标
甜味超标 2021-01-19 10:06

I created a new project using the gulp-angular Yeoman generator with language set to TypeScript. Then ran the Gulp build process and also opened the page in a web browser, w

相关标签:
4条回答
  • 2021-01-19 10:08

    I had the same problem; it appears typings was stripping the original definition file referencing the "ng" import.

    Add this back to your original angular/index.d.ts file:

    // Collapse angular into ng import ng = angular;

    Or better yet, place it in a ng.d.ts file so it's not overwritten by typings again.

    0 讨论(0)
  • 2021-01-19 10:10

    Cannot find namespace 'ng'"

    Make sure the project contains no reference to declare module ng. Same for moment.

    Update

    based on edit in the question:

    Explicitely referencing the type definition files by adding the following to the top of navbar.directive.ts removes the problem

    Please use a tsconfig.json : https://basarat.gitbooks.io/typescript/content/docs/project/compilation-context.html

    0 讨论(0)
  • 2021-01-19 10:24

    Adding a tsconfig.json to the root of my VS Code project is what fixed the issue for me. Also, had to restart VS code. Here is what I put inside that file:

    {
        "compilerOptions": {
            "target": "es5",
            "module": "commonjs",
            "sourceMap": true
        }
    }
    

    For more info on the tsconfig.json file, check out:

    https://code.visualstudio.com/docs/languages/typescript

    0 讨论(0)
  • 2021-01-19 10:33

    I found the following in a discreet location in the code. Likely put there to avoid type errors. Seems like vscode encountered this and redefined the global 'angular' variable as type of 'any'.

    var angular = window['angular'];
    
    0 讨论(0)
提交回复
热议问题