Javascript 打字稿:错误 TS2693:“Promise”仅指一种类型,但在此处用作值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43119163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here
提问by kalyanvgopal
I am trying to use Typescript for my AWS Lambda and i am getting the following errors where ever I use promises.
我正在尝试将 Typescript 用于我的 AWS Lambda,并且在我使用 promise 的地方遇到以下错误。
error TS2693: 'Promise' only refers to a type, but is being used as a value here.
I tried using the following variations in the code
我尝试在代码中使用以下变体
Using the Promise constructor
使用 Promise 构造函数
responsePromise = new Promise((resolve, reject) => {
return reject(new Error(`missing is needed data`))
})
using Promise.reject
使用 Promise.reject
responsePromise = Promise.reject(new Error(`Unsupported method "${request.httpMethod}"`));
Versions
版本
Following are the versions in my dev dependencies:
以下是我的开发依赖项中的版本:
"typescript": "^2.2.2"
"@types/aws-lambda": "0.0.9",
"@types/core-js": "^0.9.40",
"@types/node": "^7.0.12",
Contents of tsconfig.json
tsconfig.json 的内容
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
// "typeRoots" : ["./typings", "./node_modules/@types"],
"target": "es5",
// "types" : [ "core-js" ],
"noImplicitAny": true,
"strictNullChecks": true,
"allowJs": true,
"noEmit": true,
"alwaysStrict": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist",
"moduleResolution": "Node",
"declaration": true,
"lib": [
"es6"
]
},
"include": [
"index.ts",
"lib/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
I am using grunt-ts with the following configuration for running ts task.
我正在使用具有以下配置的 grunt-ts 来运行 ts 任务。
ts: {
app: {
tsconfig: {
tsconfig: "./tsconfig.json",
ignoreSettings: true
}
},
...
I tried with the solution mentioned in I get: [ts] 'Promise' only refers to a type, but is being used as a value herebut no luck.
我尝试使用I get 中提到的解决方案:[ts] 'Promise' 仅指一种类型,但在此处用作值但没有运气。
回答by Sandro Keil
I had the same issue with the aws-sdkand I solved it by using "target": "es2015". This is my tsconfig.jsonfile.
我遇到了同样的问题,aws-sdk我通过使用"target": "es2015". 这是我的tsconfig.json文件。
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": false,
"noImplicitAny": false,
"module": "commonjs",
"target": "es2015"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
回答by Kenshin
Encounter the same error today and solved it with:
今天遇到同样的错误并解决了它:
npm i --save-dev @types/es6-promise
Update:
更新:
add:
添加:
import {Promise} from 'es6-promise'
回答by Nilesh
I solved this by adding below code to tsconfig.json file.
我通过将以下代码添加到 tsconfig.json 文件解决了这个问题。
"lib": [
"ES5",
"ES2015",
"DOM",
"ScriptHost"]
回答by Mani S
Solved by changing the targetin compilerOptions.
通过更改compilerOptions 中的目标来解决。
{
"compilerOptions": {
"module": "es2015",
"target": "es2015",
"lib": [
"es2016",
"dom"
],
"moduleResolution": "node",
"noImplicitAny": false,
"sourceMap": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./public/js/app"
},
"exclude": [
"node_modules",
"public/js",
"assets/app/polyfills.ts"
],
"angularCompilerOptions": {
"skipMetadataEmit": true
}
}
回答by Nhan Cao
Here is my tip. Tested with vscode 1.21.1 (on MAC)
这是我的提示。使用 vscode 1.21.1 测试(在 MAC 上)
Put below config to tsconfig.json
将下面的配置放入tsconfig.json
"lib": [
"es2016",
"dom"
]
into compilerOptions
进入 编译器选项
Restart IDE (this action is required :D )
重新启动 IDE(此操作是必需的 :D)
回答by Hazem HASAN
I had this error but I resolved it by using this command, my ts file name is promises-fs.ts:
我有这个错误,但我使用这个命令解决了它,我的 ts 文件名是 promises-fs.ts:
tsc promises-fs.ts --target es6 && node promises-fs.js
and the error is gone
错误消失了
回答by Imamudin Naseem
Add below line to file where error is being thrown.This should fix the issue
将以下行添加到引发错误的文件中。这应该可以解决问题
declare var Promise: any;
P.S:This is definitely not the optimal solution
PS:这绝对不是最优解
回答by kalyanvgopal
Finally tsc started working without any errors. But multiple changes. Thanks to Sandro Keil, Pointy& unional
最后 tsc 开始工作,没有任何错误。但是多变。由于桑德罗的Keil,尖及unional
- Removed dt~aws-lambda
- Removed options like noEmit,declaration
- Modified Gruntfile and removed ignoreSettings
- 删除了 dt~aws-lambda
- 删除了 noEmit、declaration 等选项
- 修改了 Gruntfile 并删除了 ignoreSettings
tsconfig.json
配置文件
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"strictNullChecks": true,
"alwaysStrict": true,
"preserveConstEnums": true,
"sourceMap": false,
"moduleResolution": "Node",
"lib": [
"dom",
"es2015",
"es5",
"es6"
]
},
"include": [
"*",
"src/**/*"
],
"exclude": [
"./node_modules"
]
}
Gruntfile.js
Gruntfile.js
ts: {
app: {
tsconfig: {
tsconfig: "./tsconfig.json"
}
},
...
回答by Fanus du Toit
Had the same issue with typescript and the aws-sdk. The solve was to change the target to es6.
打字稿和aws-sdk. 解决方法是将目标更改为es6。
My complete tsconfig.jsonfile:
我的完整tsconfig.json文件:
{
compilerOptions: {
outDir: ./dist/,
sourceMap: true,
noImplicitAny: true,
module: commonjs,
target: es6,
jsx: react,
allowJs: true
},
include: [
./src/**/*
]
}
回答by Jon Gear
I had the same issue until I added the following lib array in typeScript 3.0.1
我遇到了同样的问题,直到我在 typeScript 3.0.1 中添加了以下 lib 数组
tsconfig.json
tsconfig.json
{
"compilerOptions": {
"outDir": "lib",
"module": "commonjs",
"allowJs": false,
"declaration": true,
"target": "es5",
"lib": ["dom", "es2015", "es5", "es6"],
"rootDir": "src"
},
"include": ["./**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

