当前位置: 首页 > 知识库问答 >
问题:

Typescript:错误TS2693:“promise”仅引用类型,但此处用作值

越嘉石
2023-03-14

我试图为我的AWS Lambda使用Typescript,我在使用promise时遇到了以下错误。

error TS2693: 'Promise' only refers to a type, but is being used as a value here.

我尝试在代码中使用以下变体

responsePromise = new Promise((resolve, reject) => {
                    return reject(new Error(`missing is needed data`))
                })
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",
{
    "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"
    ]
}
ts: {
            app: {
                tsconfig: {
                    tsconfig: "./tsconfig.json",
                    ignoreSettings: true
                }
            },
...

我尝试了I Get中提到的解决方案:[ts]'Promise'只引用类型,但在这里被用作值,但没有运气。

共有1个答案

朱宏爽
2023-03-14

我在AWS-SDK中遇到了同样的问题,并通过使用“target”:“ES2015”解决了这个问题。这是我的tsconfig.json文件。

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": false,
        "noImplicitAny": false,
        "module": "commonjs",
        "target": "es2015"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}
 类似资料: