2017-08-07 73 views
0

我试过的形式使用承诺异步调用我的自定义验证了我创建以下单独的打字稿文件'承诺”只是指一个类型,而是被用作一个值这里

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代码,在这里我有下无极,一旦我转到问题选项卡上,我可以看到

红色波浪线

以下错误

enter image description here

如何解决这个问题。

+0

什么是你'tsconfig.json'文件?如果输出是'es5',那么在使用承诺之前,您需要包含承诺库或es6'的垫片。 – Duncan

+0

[typescript:error TS2693:'Promise'可能重复只是指一个类型,但在这里用作值](https://stackoverflow.com/questions/43119163/typescript-error-ts2693-promise-only -refers-to-a-type-but-being-being-as-as) –

+0

我也可以建议你不要在你的ts代码中使用function()吗? –

回答

3

你的问题是,你指定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" 
    ] 
} 
+0

谢谢,这种方法错误消失了,但在'tsconfig.json'这里得到了一个像这样的交战[http://i.imgur.com/8oausRv.png] –

+1

@kelumpriyadarshane'es2015.promise' - 小写 – Gildor

+0

@吉尔多,哎呀。更正,谢谢。 – Duncan

相关问题