繁体   English   中英

“ Promise”仅指类型,但在此处用作值

[英]''Promise' only refers to a type, but is being used as a value here

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

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下有红色的波浪线,一旦转到“ 问题”选项卡,就可以看到

跟随错误

在此处输入图片说明

如何解决这个问题。

您的问题是您将es5指定为目标,但是es5没有本地Promise实现,因此您还需要包括某种提供该实现的填充程序。

最简单的解决方案应该是在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"
  ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM