Kelum
Kelum

Reputation: 1775

''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

enter image description here

how to solve this issue.

Upvotes: 1

Views: 4421

Answers (1)

Duncan
Duncan

Reputation: 95622

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

Upvotes: 4

Related Questions