【发布时间】:2020-07-27 07:44:26
【问题描述】:
我读过这篇文章https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints
我想知道为什么我们必须在这里使用extends 关键字?
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know it has a .length property, so no more error
return arg;
}
为什么不这样做呢?我使用extends关键字有什么区别?
interface Lengthwise {
length: number;
}
function loggingIdentity(arg: Lengthwise): Lengthwise {
console.log(arg.length); // Now we know it has a .length property, so no more error
return arg;
}
【问题讨论】:
标签: typescript typescript-generics