I'm taking an Angular2 course and following along. Everything I have been doing thus far for all the other exercise has worked fine until I got to creating this custom validator and this Promise code. Makes no sense to me why this would occur now.

I get: [ts] 'Promise' only refers to a type, but is being used as a value here.
Can't seem to resolve this. I get my error even without adding in the setTimeout function.
Per a suggestion from someone elsewhere. I added to the tsconfig.json: "compilerOptions": { ... "types" : [ "core-js" ] }
But I still get the error.
Using Visual Studio code: Version 1.10.1. OS: Windows 10 Pro.
Here's my code:
import {Control} from 'angular2/common';
export class UsernameValidators
{
static shouldBeUnique(control: Control)
{
return new Promise((resolve, reject) =>
{
setTimeout(function()
{
if (control.value == "Dan")
resolve({ shouldBeUnique: true });
else
resolve(null);
}, 1000);
});
}
}
Here's the course that I am taking and a screen shot of what I am being instructed to do. The intelliSense is different from mine. It works fine and they do not get the error I get.

Here's the courses end result.

You need to target ES6, or else use a polyfill library like bluebird. If you are targeting ES5 in the compiler output, then it will fail like this because ES5 has no Promises.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With