返回函数的函数的返回类型

问题描述:

我有下面的函数返回另一个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

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; 
    }