打字稿:错误 TS2693:“Promise"仅指一种类型,但在此处用作值 [英] typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here

查看:120
本文介绍了打字稿:错误 TS2693:“Promise"仅指一种类型,但在此处用作值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Typescript 用于我的 AWS Lambda,但在使用 Promise 时遇到以下错误.

<前>错误 TS2693:Promise"仅指一种类型,但在此处用作值.

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

使用 Promise 构造函数

responsePromise = new Promise((resolve, reject) => {返回拒绝(新错误(`缺少需要数据`))})

使用 Promise.reject

responsePromise = Promise.reject(new Error(`不支持的方法${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":真,编译器选项":{"module": "commonjs",//"typeRoots" : ["./typings", "./node_modules/@types"],目标":es5",//"类型" : [ "core-js" ],"noImplicitAny": 真,"strictNullChecks": 真,allowJs":真,noEmit":真,"alwaysStrict": 真,preserveConstEnums":真,源地图":真,"outDir": "dist","moduleResolution": "节点",声明":真实,库":[es6"]},包括": ["index.ts","lib/**/*.ts"],排除": [节点模块",**/*.spec.ts"]}

我正在使用具有以下配置的 grunt-ts 来运行 ts 任务.

ts: {应用程序: {tsconfig:{tsconfig: "./tsconfig.json",忽略设置:true}},...

我尝试了 我得到:[ts] 'Promise' 仅指一种类型,但在此处被用作值 但没有运气.

解决方案

我在使用 aws-sdk 时遇到了同样的问题,我使用 "target": "es2015" 解决了它.这是我的 tsconfig.json 文件.

<代码>{编译器选项":{"outDir": "./dist/",源地图":假,noImplicitAny":假,"module": "commonjs",目标":es2015"},包括": [源代码/**/*"],排除": [节点模块",**/*.spec.ts"]}

I am trying to use Typescript for my AWS Lambda and i am getting the following errors where ever I use promises.

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

responsePromise = new Promise((resolve, reject) => {
                    return reject(new Error(`missing is needed data`))
                })

using 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

{
    "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.

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 here but no luck.

解决方案

I had the same issue with the aws-sdk and I solved it by using "target": "es2015". This is my tsconfig.json file.

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

这篇关于打字稿:错误 TS2693:“Promise"仅指一种类型,但在此处用作值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆