【问题标题】:''Promise' only refers to a type, but is being used as a value here''Promise' 仅指一种类型,但在这里用作值
【发布时间】:2018-01-14 22:17:40
【问题描述】:

我尝试在我的自定义验证器中以我在单独的打字稿文件之后创建的形式使用异步调用的承诺

usernameValidators.ts

import { Control } from 'angular2/common';

export class UsernameValidators {

    static shouldBeUnique(control: Control){
        return new Promise((resolve, reject) => {
            setTimeout(function(){
                if(control.value == "mosh")
                    resolve({ shouldBeUnique: true });

                else
                    resolve(null);


            }, 1000);
        });
    }
}

我正在使用 VS 代码开发这个,这里我在 Promise 下有红色波浪线,一旦我转到 PROBLEMS 选项卡,我可以看到

跟随错误

如何解决这个问题。

【问题讨论】:

标签: angular typescript angular-promise


【解决方案1】:

您的问题是您将 es5 指定为目标,但 es5 没有本机 Promise 实现,因此您还需要包含某种提供该实现的 shim。

最简单的解决方案应该是在tsconfig.json中包含“es2015.Promise”库:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "lib": ["es2015.promise", "dom"]
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

【讨论】:

  • 谢谢,这种方法错误消失了,但在tsconfig.json 得到一个交战like this
  • @kelumpriyadarshane es2015.promise - 小写
  • 奇怪的是,对我来说,它仅在 es 也被指定为 lib 时才有效,尽管它已经设置为目标:["es2015.promise", "es5","dom"]。此外,VS Code 似乎缺少重新加载文件。我必须重新启动 VS Code 才能使错误消失。
猜你喜欢
  • 2019-07-30
  • 2019-10-08
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2018-12-30
相关资源
最近更新 更多