''Promise' 仅指类型,但在这里用作值
''Promise' only refers to a type, but is being used as a value here
我尝试在我的自定义验证器中以我在单独的打字稿文件之后创建的形式使用异步调用承诺
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 下有红色波浪线,一旦我转到 PROBLEMS 选项卡, 我可以看到
跟随错误
如何解决这个问题。
您的问题是您将 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"
]
}
我尝试在我的自定义验证器中以我在单独的打字稿文件之后创建的形式使用异步调用承诺
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 下有红色波浪线,一旦我转到 PROBLEMS 选项卡, 我可以看到
跟随错误
如何解决这个问题。
您的问题是您将 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"
]
}