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

前端 未结 22 2529
猫巷女王i
猫巷女王i 2020-11-29 19:35

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,          


        
相关标签:
22条回答
  • 2020-11-29 19:42

    Here is my tip. Tested with vscode 1.21.1 (on MAC)

    Put below config to tsconfig.json

    "lib": [
    "es2016",
    "dom"
    ]
    

    into compilerOptions

    Restart IDE (this action is required :D )

    0 讨论(0)
  • 2020-11-29 19:42

    I had the same issue until I added the following lib array in typeScript 3.0.1

    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"]
    }
    
    0 讨论(0)
  • 2020-11-29 19:45

    If you're using the DefinitelyTyped repository in your project you might be experiencing this recent issue.

    A decent workaround you might use (other than waiting for an updated build of the definitions file or refactoring your TS code) is to specify an explicit version+build for the core-js typings rather than let Visual Studio pick the latest/most recent one. I found one that seems to be unaffected by this problem (in my case at least), you can use it replacing the following line from your package.json file:

      "scripts": {
        "postinstall": "typings install dt~core-js --global"
      }
    

    With the following one:

      "scripts": {
        "postinstall": "typings install dt~core-js@0.9.7+20161130133742 --global"
      }
    

    This fixed my issue for good. However, is highly recommended to remove the explicit version+build reference as soon as the issue will be released.

    For further info regarding this issue, you can also read this blog post that I wrote on the topic.

    0 讨论(0)
  • 2020-11-29 19:46

    Had the same issue with typescript and the aws-sdk. The solve was to change the target to es6.

    My complete tsconfig.json file:

    {
            compilerOptions: {
                    outDir: ./dist/,
                    sourceMap: true,
                    noImplicitAny: true,
                    module: commonjs,
                    target: es6,
                    jsx: react,
                    allowJs: true
            },
            include: [
                    ./src/**/*
        ]
    }
    
    0 讨论(0)
  • 2020-11-29 19:46

    I had the same problem and this saved me from the problem in second:

    write in console this:

    npm i --save bluebird
    npm i --save-dev @types/bluebird @types/core-js@0.9.36
    

    in the file where the problem is copy paste this:

    import * as Promise from 'bluebird';
    
    0 讨论(0)
  • 2020-11-29 19:46

    Just change the target to "ES2017" in tsconfig.json file.

    this is my tsconfig.json file

    {
    "compilerOptions": {
    /* Basic Options */
        "target": "ES2017",   /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
        "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
        "declaration": true,  /* Generates corresponding '.d.ts' file. */
        "sourceMap": true,    /* Generates corresponding '.map' file. */
        "outDir": "./dist",   /* Redirect output structure to the directory. */
        "strict": true        /* Enable all strict type-checking options. */
      },
      "include": [
        "src/**/*"
      ],
      "exclude": [
        "node_modules"
      ]
    }
    
    0 讨论(0)
提交回复
热议问题