2017-01-02 166 views
1

我有下面的函數返回另一個function,其中getFirstPhoneNo()將返回string返回函數的函數的返回類型

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

下面是我對interfacepatientData

export interface IPatient { 
    getFirstPhoneNo: Function 
} 

應該是什麼我的返回類型的手機呢?如果它是一個類型IpatientFunctionFunction which returns string

+1

函數返回另一個'函數'在哪裏? – Satpal

+0

@Satpal這是'getFirstPhoneNo()'我想,它返回一個'string' – echonax

+0

你會看到返回getFirstPhoneNo – Shane

回答

1

IPatient被定義爲這樣

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

這意味着getFirstPhoneNo是返回其返回字符串的功能的功能。 因此,get phones返回一個布爾值或返回字符串的函數。這可以轉換爲返回類型boolean |() => string。此返回類型不是非常有用,因爲它只具有boolean() => string類型共享的屬性。

一種可能性是改變你這樣的代碼:

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

這改變的get phones的接口() =>() => string和,但也允許如果電話號碼設置(因爲一個空字符串來評估正在做檢查爲false)

另一種更簡單的方法將已經做好的方法調用中get phone功能,只返回電話號碼

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