我得到:[ts]'Promise'仅指类型,但在此处被用作值 [英] I get: [ts] 'Promise' only refers to a type, but is being used as a value here
问题描述
我正在学习Angular2课程并继续学习.到目前为止,我在所有其他练习中所做的所有工作都运转良好,直到我创建了此自定义验证器和此Promise代码为止.对我来说毫无意义,为什么现在会发生这种情况.
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.
我得到:[ts]'Promise'仅指一种类型,但是在这里被用作值.
I get: [ts] 'Promise' only refers to a type, but is being used as a value here.
似乎无法解决此问题.即使没有添加setTimeout函数,我也会收到错误消息.
Can't seem to resolve this. I get my error even without adding in the setTimeout function.
根据其他人的建议.我添加到tsconfig.json: "compilerOptions":{ ... "types":["core-js"] }
Per a suggestion from someone elsewhere. I added to the tsconfig.json: "compilerOptions": { ... "types" : [ "core-js" ] }
但是我仍然会收到错误消息.
But I still get the error.
使用Visual Studio代码:1.10.1版. 操作系统:Windows 10 Pro.
Using Visual Studio code: Version 1.10.1. OS: Windows 10 Pro.
这是我的代码:
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.
这是课程的最终结果.
推荐答案
您需要定位ES6,否则请使用诸如bluebird之类的polyfill库.如果要在编译器输出中定位ES5,则它将失败,因为ES5没有承诺.
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.
这篇关于我得到:[ts]'Promise'仅指类型,但在此处被用作值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!