简体   繁体   中英

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

I tried to use a promise to Async call in my custom validator in the form for that I created following separate typescript file

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);
        });
    }
}

I'm developing this using VS code, here I'm having red squiggly line under Promise and once I goto PROBLEMS tab, I can see

following error

在此处输入图片说明

how to solve this issue.

Your problem is that you specified es5 as the target, but es5 doesn't have a native Promise implementation so you also need to include some kind of shim that provides the implementation.

The simplest solution should be to include the "es2015.Promise" library in tsconfig.json :

{
  "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"
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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