2016-10-05 10 views
0

TSlint(v3.15.1)とtypescript(2.0.3)の最新バージョンを使用して、より古い.tsファイルを更新しています。私は私が解決しようとしているいくつかのエラーを取得しています。Typescriptで変数の有無を確認してください

export class myClass { 
    private pUserName: string; 
    private pUserPass: string; 
    private pHost: string; 
    private pPort: number; 

constructor($host: string, $port: number) { 
    this.pHost = $host; 
    this.pPort = $port; 
} 

public isReady(): boolean { 
     return Boolean(this.pUserName && this.pUserPass && this.pJiraHost && this.pJiraPort); 
} 

どちらか私は取得しています「[TS]は名前のブールを見つけることができない」、またはいくつかは、「ブール値への文字列を変換することはできません」...:

のコードでは、いくつかのクラスのプロパティのための単純な存在テストです

私は非常にタミル文字に慣れていますが、これを正しく書く方法を教えてください。

答えて

0

エラーを報告しないこの回避策が見つかりました。しかし、これは冗長で非常にエレガントではありません...良いアイデアですか? :)

public isReady(): boolean { 
    for (const property in ['pUserName', 'pUserPass', 'pHost', 'pPort']) { 
     if (typeof(property) !== 'string') { 
      return false; 
     } 
    } 
    return true; 
} 
+0

あなたは '復帰!!で行くことができます(この.pUserName && this.pUserPass && this.pJiraHost && this.pJiraPort);空の文字列とゼロも注意して 'false'として報告されます –

0

私はまた、ES6定義タイピングが含まれていなかったことが判明:今、私のtsconfig.jsonがどのように見える

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "target": "ES6", 
     "outDir": "out", 
     "noLib": true, 
     "sourceMap": true, 
     "removeComments": true 
    }, 
    "include" : [ 
     "node_modules/typescript/lib/lib.es6.d.ts", 
     "node_modules/@types/node/index.d.ts", 
     "node_modules/@types/q/index.d.ts", 
     "node_modules/@types/mocha/index.d.ts", 
     "node_modules/@types/moment/index.d.ts" 
    ], 
    "exclude": [ 
     "node_modules" 
    ] 
} 
関連する問題