错误 TS2304:找不到名称“承诺”

Posted

技术标签:

【中文标题】错误 TS2304:找不到名称“承诺”【英文标题】:error TS2304: Cannot find name 'Promise' 【发布时间】:2016-12-31 04:45:12 【问题描述】:

大家好,我浏览了 *** 上所有可用的解决方案。但对我没有任何工作。因此发布问题。 tsconfig.json


  "version":"2.13.0",
  "compilerOptions": 
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": false    
  ,
  "exclude": [
        "node_modules"
    ]

package.json


  "name": "anguapp",
  "version": "1.0.0",
  "description": "Angular2 Gulp Typescript and Express",
  "main": "dist/server.js",
  "scripts": 
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "gulp build"
  ,
  "keywords": [],
  "author": "UK",
  "license": "MIT",
  "dependencies": 
    "angular2": "2.0.0-beta.17",
     "es6-promise": "3.2.1",
    "es6-shim": "0.35.1",
    "body-parser":"1.15.2",
    "express": "4.13.3",
    "gulp-concat": "2.6.0",
    "reflect-metadata": "0.1.8",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.37",
    "zone.js": "0.6.17",
    "mongodb" :"2.2.8",
     "ejs" : "~0.8.5"          
    ,
  "devDependencies": 
    "del": "2.2.2",
    "gulp": "3.9.1",
    "gulp-concat": "2.6.0",
    "gulp-sourcemaps": "1.6.0",
    "gulp-typescript": "2.13.0",
    "run-sequence": "1.1.5"
  

在执行 gulp 时出现以下错误。

[12:39:43] Using gulpfile D:\Repository\documents\personal\abha-du\gulpfile.js
[12:39:43] Starting 'build'...
[12:39:43] Starting 'clean'...
[12:39:43] Finished 'clean' after 66 ms
[12:39:43] Starting 'build:server'...
[12:39:45] Finished 'build:server' after 2.18 s
[12:39:45] Starting 'build:index'...
[12:39:46] Finished 'build:index' after 21 ms
[12:39:46] Starting 'build:app'...
D:/Repository/documents/personal/abha-du/node_modules/angular2/platform/browser.
d.ts(78,90): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/common/direct
ives/ng_class.d.ts(72,35): error TS2304: Cannot find name 'Set'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/common/pipes/
async_pipe.d.ts(25,38): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/applicat
ion_ref.d.ts(38,88): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/applicat
ion_ref.d.ts(92,42): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/applicat
ion_ref.d.ts(151,33): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/change_d
etection/differs/default_keyvalue_differ.d.ts(23,15): error TS2304: Cannot find
name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/change_d
etection/differs/default_keyvalue_differ.d.ts(25,16): error TS2304: Cannot find
name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/di/refle
ctive_provider.d.ts(103,123): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/di/refle
ctive_provider.d.ts(103,165): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/c
omponent_resolver.d.ts(8,53): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/c
omponent_resolver.d.ts(12,44): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/d
ynamic_component_loader.d.ts(59,148): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/d
ynamic_component_loader.d.ts(100,144): error TS2304: Cannot find name 'Promise'.

D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/d
ynamic_component_loader.d.ts(105,139): error TS2304: Cannot find name 'Promise'.

D:/Repository/documents/personal/abha-du/node_modules/angular2/src/core/linker/d
ynamic_component_loader.d.ts(106,135): error TS2304: Cannot find name 'Promise'.

D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/async.
d.ts(27,33): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/async.
d.ts(28,45): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(4,27): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(4,39): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(7,9): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(8,30): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(11,43): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(12,27): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(14,23): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(15,25): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(100,41): error TS2304: Cannot find name 'Set'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(101,22): error TS2304: Cannot find name 'Set'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/collec
tion.d.ts(102,25): error TS2304: Cannot find name 'Set'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/lang.d
.ts(4,17): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/lang.d
.ts(5,17): error TS2304: Cannot find name 'Set'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/lang.d
.ts(71,59): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(2,14): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(11,50): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/facade/promis
e.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/http/headers.
d.ts(43,59): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/http/url_sear
ch_params.d.ts(11,16): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/platform/brow
ser/browser_adapter.d.ts(75,33): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/platform/dom/
dom_adapter.d.ts(85,42): error TS2304: Cannot find name 'Map'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/direct
ives/router_outlet.d.ts(27,54): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/direct
ives/router_outlet.d.ts(33,51): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/direct
ives/router_outlet.d.ts(38,56): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/direct
ives/router_outlet.d.ts(47,65): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/direct
ives/router_outlet.d.ts(58,60): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/instru
ction.d.ts(124,34): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/instru
ction.d.ts(148,25): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/instru
ction.d.ts(164,34): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/instru
ction.d.ts(167,25): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/interf
aces.d.ts(21,107): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/interf
aces.d.ts(39,104): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/interf
aces.d.ts(57,109): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/interf
aces.d.ts(80,109): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/interf
aces.d.ts(102,114): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/lifecy
cle/lifecycle_annotations.d.ts(29,100): error TS2304: Cannot find name 'Promise'
.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/route_
config/route_config_impl.d.ts(101,19): error TS2304: Cannot find name 'Promise'.

D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/route_
definition.d.ts(20,20): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/route_
definition.d.ts(35,20): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/route_
registry.d.ts(52,66): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(56,50): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(68,46): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(86,45): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(99,34): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(107,64): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(112,85): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(120,70): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(128,43): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(132,29): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(138,19): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/angular2/src/router/router
.d.ts(146,70): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/Observable.d.ts(10,66
): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/Observable.d.ts(66,60
): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/Observable.d.ts(66,70
): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/operator/toPromise.d.
ts(7,59): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/operator/toPromise.d.
ts(7,69): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/operator/toPromise.d.
ts(9,9): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/operator/toPromise.d.
ts(10,26): error TS2304: Cannot find name 'Promise'.
D:/Repository/documents/personal/abha-du/node_modules/rxjs/operator/toPromise.d.
ts(10,36): error TS2304: Cannot find name 'Promise'.
[12:39:47] TypeScript: 86 semantic errors
[12:39:47] TypeScript: emit succeeded (with errors)
[12:39:47] Finished 'build:app' after 1.93 s
[12:39:47] Starting 'build:home'...
[12:39:47] Finished 'build:home' after 7.17 ms
[12:39:47] Finished 'build' after 4.23 s
[12:39:47] Starting 'default'...
[12:39:47] Finished 'default' after 12 µs

请帮我解决这些 TS2304: Cannot find name 'Promise' 错误

【问题讨论】:

看起来类似于this question 或者只是将目标更改为"target": "es6" 应该可以解决它。 你简直太棒了。它正在工作。"target": "es6" 正在工作。非常感谢您。我从前两天被卡住了。 【参考方案1】:

既然是“TS 错误”,我建议你应该安装 es6-promise 类型。你可以在你的项目根目录使用typings install dt~es6-promise --global --save

【讨论】:

试过了,但似乎没有帮助,我错过了什么?【参考方案2】:

您只需将target 中的es6 更改为tsconfig.json 即可解决此问题。


  "version":"2.13.0",
  "compilerOptions": 
    "target": "es6", //<- change here
    ......
  ,
  "exclude": [
        "node_modules"
    ]


您可以做的其他方式是,您需要确保在页面上已将es6-shim.js 推荐到那里,然后按照不需要更改target 选项的this answer

【讨论】:

【参考方案3】:

"es6" 添加到compilerOptions.lib


  "compilerOptions": 
     "target": "ES5", /* You can still target ES5 if you want */
     "lib": ["es6"],
     ..
  
  ..

【讨论】:

以上是关于错误 TS2304:找不到名称“承诺”的主要内容,如果未能解决你的问题,请参考以下文章

嚎叫:角度错误 TS2304:找不到名称“嚎叫”[重复]

打字稿错误 TS2304:即使使用 esnext 也找不到名称 AsyncIterator

TS2304:找不到名称“要求”

从命令行读取参数 - 错误 TS2304:找不到名称“进程”

TypeScript 和 ng2 rc.1 出现错误:(20、15)TS2304:找不到名称“模块”

ts2304 找不到名称“OnInit”