typescript是否支持参数多态性

typescript是否支持参数多态性,typescript,functional-programming,Typescript,Functional Programming,假设我有以下代码: export interface Semigroup<A> { append: (x: A, y: A) => A; } const arraySemigroup: Semigroup<Array<any>> = { append: (x, y) => x.concat(y) } 导出接口半群{ 附加:(x:A,y:A)=>A; } 常量数组半群:半群={ 附加:(x,y)=>x.concat(y) } 除了阵列的

假设我有以下代码:

export interface Semigroup<A> {
  append: (x: A, y: A) => A;
}

const arraySemigroup: Semigroup<Array<any>> = {
  append: (x, y) => x.concat(y)
}
导出接口半群{
附加:(x:A,y:A)=>A;
}
常量数组半群:半群={
附加:(x,y)=>x.concat(y)
}
除了阵列的
any
之外,这是非常棒的


有没有一种方法,我可以为数组指定泛型类型参数?

似乎可以使用TypeScript 2.8的条件类型来实现这一点。基于他们在发行说明中使用的一个示例,我提出了以下解决方案:

interface BoxedValue<T> {
  append: (x: T, y: T) => T;
};
interface BoxedArray<T> {
  append: (x: T, y: T) => T;
};
type Boxed<T> = T extends (infer U)[] ? BoxedArray<U> : BoxedValue<T>;

const boxedNumber: Boxed<number> = {
  append: (x: number, y: number): number => {
    return x + y
  }
}

const boxedNumberArray: Boxed<number[]> = {
  append: (x: number, y: number): number => {
    return x + y
  }
}
接口盒值{
附加:(x:T,y:T)=>T;
};
Darray接口{
附加:(x:T,y:T)=>T;
};
装箱类型=T扩展(推断U)[]?BoxedArray:BoxedValue;
常量boxedNumber:已装箱={
附加:(x:number,y:number):number=>{
返回x+y
}
}
常量boxedNumber数组:已装箱={
附加:(x:number,y:number):number=>{
返回x+y
}
}
这里可以看到发行说明:它们显示了装箱的
示例,但在该示例中没有使用
推断
功能