从node_modules文件夹中编译Typescript时出现错误

30

我正在运行typescript构建并在node_modules中遇到错误。为什么它没有忽略这个文件夹呢?我已经在tsconfig.json的排除部分中设置了它。真正奇怪的是,我有另一个项目,我已经对比了文件,即使gulpfile.js、tsconfig.json和node_modules文件夹都相同,它也不会抛出这些错误。我还能检查什么?

错误:

c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(3,14): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(4,42): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(14,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(24,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(25,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,103): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,135): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(13,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(14,84): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(4,27): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(4,39): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(7,9): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(8,30): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(11,43): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(12,27): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(14,23): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(15,25): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(95,41): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(96,22): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(97,25): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(13,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(14,17): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(78,59): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(1,10): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(3,14): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/http/headers.d.ts(43,59): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/http/url_search_params.d.ts(11,16): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/platform/browser/browser_adapter.d.ts(75,33): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/platform/dom/dom_adapter.d.ts(85,42): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(22,67): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(72,67): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(77,31): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(77,54): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(65,67): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(65,88): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(72,84): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(77,38): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(100,66): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(154,66): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(159,31): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(159,54): error TS2304: Cannot find name 'Promise'.

tsconfig.js

{
  "version": 3,
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]
}

gulpfile.js(我正在运行build-typescript任务 - 当在命令行中只键入tsc时,我会得到相同的错误)

/// <binding Build='default' />

var del = require('del'),
    gulp = require("gulp"),
    ts = require('gulp-typescript'),
    watch = require('gulp-watch');

var webproj = "./src/StreakMaker.Web/";
var webroot = webproj + "wwwroot/";
var appsource = webproj + "App/";
var appout = webroot + "app/";
var jspmsource = "./jspm_packages/";
var jspmout = webroot + "jspm_packages/";

var paths = {
    webroot: webroot,
    src: appsource,
    app: appout,
    jspm: jspmsource,
    jspm_out: jspmout
};

gulp.task('watch', ['watch-typescript', 'watch-html']);

gulp.task('watch-typescript', function(){
    gulp.watch(paths.src + '/**/*.ts', ['build-typescript']);
});

gulp.task('clean-typescript', function () {
    del([paths.app + '/**/*.ts']);
});

gulp.task('build-typescript', ['clean-typescript'], function () {
    var tsProject = ts.createProject('./tsconfig.json');

    gulp.src(paths.src + '/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(gulp.dest(paths.app));
});

gulp.task('watch-html', function () {
    gulp.watch(paths.src + '/**/*.html', ['copy-html']);
});

gulp.task('clean-html', function () {
    del([paths.app + '/**/*.html']);
});

gulp.task('copy-html', ['clean-html'], function () {
    gulp.src(paths.src + '/**/*.html')
        .pipe(gulp.dest(paths.app));
});

gulp.task('copy-jspm', ['clean-jspm', 'copy-config'], function() {
    gulp.src(paths.jspm + "**/*.{js,css,map}")
        .pipe(gulp.dest(paths.jspm_out));
});

gulp.task('clean-jspm', function(){
    del([paths.jspm_out + "**/*.*"]); 
});

gulp.task('copy-config', ['clean-config'], function(){
    gulp.src("./config.js")
        .pipe(gulp.dest(paths.webroot));
});

gulp.task('clean-config', function(){
    del(paths.webroot + 'config.js'); 
});

gulp.task('default', ['build-typescript', 'copy-html', 'copy-jspm']);
6个回答

29

我也曾经遇到过这个问题。

我在包含 "import {bootstrap}" 行的 TS 文件顶部添加了以下内容:

///<reference path="../node_modules/angular2/typings/browser.d.ts"/> 

当然,你的路径可能与那个文件不同。


2
这非常有用(+1),如果按照angular.io上的快速入门指南,直接引用顶级类型定义也可以:/// <reference path="../typings/browser.d.ts" /> - Chris Ballard
2
有没有人找到解决方案...我已经尝试了所有的方法,但是得到的都是无法找到地图、设置、期望等错误。 - Pratik Kelwalkar
3
考虑到<reference>标签是一种不良做法。这里有一篇文章,Minko Gechev写了关于这个确切错误的内容。这是另一篇文章,解释了整个打包过程。 - Cornel Stefanache

19

如果你的目标是ES5,那么请将"node_modules/typescript/lib/lib.es6.d.ts"添加到tsconfig.json文件中:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "outDir": "built",
        "rootDir": ".",
        "sourceMap": false
    },
    "files": [
      "helloworld.ts",
      "node_modules/typescript/lib/lib.es6.d.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

3
新手评论。在Visual Studio 2015中这样做导致了43,000个新的TypeScript构建错误。 - bbsimonbb
当一些模块无法找到它们的依赖项时,这对我很有效。 - Camron_Godbout
使用这行代码可以阻止TypeScript检查其他文件的类型。 - undefined

17

您可以在tsconfig.json文件中添加skipLibCheck属性,并将其值设置为true

{
  "compilerOptions": {
    [...]
    "skipLibCheck": true
  },
  "include": ["./src/**/*.*"],
  "exclude": [
    "node_modules"
  ]
}

使用该属性将跳过声明文件(*.d.ts)中的类型检查。

感谢来源


谢谢!这个回答对我来说是正确的。 我正在尝试在工作中使用VSCode为JavaScript项目编写类型,但目前还不想完全选择TypeScript。这解决了我的问题。 - 4uroraskye

1

当这个解决方案

/// <reference path="../typings/browser.d.ts" />
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app/main';

bootstrap(AppComponent);

使用gulpjs时,它能很好地工作。但是我更喜欢直接在相应的gulpjs任务源代码中添加类型文件,而不是在源文件中添加注释,例如:

gulp.task('typescript', function () {
  return gulp
    .src([
      'typings/browser.d.ts',
      tsSrc + '**/*.ts'
    ])
    .pipe(sourcemaps.init())
    .pipe(typescript(tscConfig.compilerOptions))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(appSrc + 'js/'));
});

0
在RC1演示中,shim由core-js模块提供,类型定义由typings工具管理。将typings/index.d.ts添加到gulp.src应该可以解决这个问题:
function buildTypeScriptFn(files) {
  files = files || ['app/**/*.ts','typings/index.d.ts'];

  return function () {
    var tsResult = gulp.src(files)
      .pipe(changed(paths.dirs.build, { extension: '.js' }))
      .pipe(ts(tscConfig.compilerOptions));

    return merge(tsResult.dts, tsResult.js)
      .pipe(gulp.dest(paths.dirs.build));
  }
}

gulp.task('ts', buildTypeScriptFn());

-3
我曾经遇到过同样的问题,只需尝试使用"npm run lite"而不是"npm start"来运行程序,这样更加方便。

这对我有用。将来会有什么问题吗? - Shourya Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接