返回函数的函数的返回类型
问题描述:
我有下面的函数返回另一个function
,其中getFirstPhoneNo()
将返回string
。返回函数的函数的返回类型
get phones() {
if (this._patientData && this._patientData.getPatientPrimaryAddress) {
return this._patientData.getFirstPhoneNo();
}
return false;
}
下面是我对interface
patientData
export interface IPatient {
getFirstPhoneNo: Function
}
应该是什么我的返回类型的手机呢?如果它是一个类型Ipatient
或Function
或Function which returns string
答
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;
}
函数返回另一个'函数'在哪里? – Satpal
@Satpal这是'getFirstPhoneNo()'我想,它返回一个'string' – echonax
你会看到返回getFirstPhoneNo – Shane