2
如何將一個未參數化的泛型函數分配給一個變量?如何將未指定的通用函數分配給TypeScript中的變量?
在下面的例子考慮我試圖通過使通用功能的成分和成分的結果賦值給一個變量來創建areArraysShallowlyEqual
功能:
export function areNullablesEqualOver<a>(areEqual: (one: a, another: a) => boolean)
:(one: a, another: a) => boolean {
return function(one, another) {
if (one == null) {
if (another == null) {
return true;
} else {
return false;
}
} else {
if (another == null) {
return false;
} else {
return areEqual(one, another);
}
}
};
};
export function areArraysEqualOver<a>(areEqual: (one: a, another: a) => boolean)
: (one: a[], another: a[]) => boolean {
return function (ones, anothers) {
var length = ones.length;
if (length !== anothers.length) return false;
for (var index = 0; index < length; index++) {
if (areEqual(ones[index], anothers[index])) continue;
return false;
}
return true;
}
}
export function areSame<a>(one: a, another: a): boolean {
return one === another;
}
// PROBLEM:
export var areArraysShallowlyEqual<a> = areNullablesEqualOver<a>(areArraysEqualOver(areSame));