2017-01-02 16 views
1

stringを返す、functionを返す次の関数を持っています。以下は関数を返す関数の戻り値タイプ

get phones() { 
    if (this._patientData && this._patientData.getPatientPrimaryAddress) { 
     return this._patientData.getFirstPhoneNo(); 
    } 
    return false; 
} 

電話のための私の戻り値の型がどうあるべきかpatientData

export interface IPatient { 
    getFirstPhoneNo: Function 
} 

のための私のinterfaceのですか?それはIpatient又はFunctionの種類やIPatientがgetFirstPhoneNoが文字列を返す関数を返す関数であることを意味するよう

export interface IPatient { 
    getFirstPhoneNo:() =>() => string 
} 

として定義されるFunction which returns string

+1

他の 'function'を返す' function'はどこにありますか? – Satpal

+0

'getFirstPhoneNo()' '文字列'を返すと思います – echonax

+0

getFirstPhoneNoを返すphones()を表示します – Shane

答えて

1

であるべきです。 したがって、get phonesは、ブール値または文字列を返す関数を返します。これは返品タイプboolean |() => stringに変換することができます。この戻り値の型は、boolean() => stringの両方の型を共有するプロパティしか持たないため、あまり役に立ちません。

一つの可能​​性は、このようにコードを変更するには、次のようになります。

get phones() { 
    if (this._patientData && this._patientData.getPatientPrimaryAddress) { 
    return this._patientData.getFirstPhoneNo(); 
    } 
    return() => ''; 
} 

これは() =>() => stringからget phonesのインターフェースを変更しても、空の文字列が評価されるため、電話番号は(設定されている場合は、チェックを行うことができます)falseに

別のより容易なアプローチはget phone機能ですでにメソッド呼び出しを行うと、電話番号のみ

get phones() { 
     if (this._patientData && this._patientData.getPatientPrimaryAddress) { 
     return this._patientData.getFirstPhoneNo()(); 
     } 
     return null; 
    } 
を返すようになり