【发布时间】:2017-08-24 10:55:39
【问题描述】:
我正在尝试为我的 AWS Lambda 使用 Typescript,但我在使用 Promise 时遇到了以下错误。
错误 TS2693:“Promise”仅指一种类型,但在此处用作值。我尝试在代码中使用以下变体
使用 Promise 构造函数
responsePromise = new Promise((resolve, reject) => {
return reject(new Error(`missing is needed data`))
})
使用 Promise.reject
responsePromise = Promise.reject(new Error(`Unsupported method "${request.httpMethod}"`));
版本
以下是我的开发依赖项中的版本:
"typescript": "^2.2.2"
"@types/aws-lambda": "0.0.9",
"@types/core-js": "^0.9.40",
"@types/node": "^7.0.12",
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"
]
}
我正在使用具有以下配置的 grunt-ts 来运行 ts 任务。
ts: {
app: {
tsconfig: {
tsconfig: "./tsconfig.json",
ignoreSettings: true
}
},
...
我尝试了I get: [ts] 'Promise' only refers to a type, but is being used as a value here 中提到的解决方案,但没有成功。
【问题讨论】:
-
传入Promise构造函数的回调函数不需要返回值。摆脱
return。 -
你是这个意思吗?
responsePromise = new Promise((resolve, reject) => { reject(new Error("missing is needed data"))})我试过了。但它并没有解决这个问题。 -
是的。 JavaScript 不关心你是否返回一个值,但它不会关注它。然而,TypeScript 确实在乎。
-
知道了。但是为什么 tsc 无法编译任何风格的 Promose.resolve 或 Promise.reject?
-
那个,我不知道。
responsePromise究竟是如何声明的?
标签: javascript typescript promise