【发布时间】:2020-10-11 04:58:40
【问题描述】:
我有一个具有某些参数类型的函数:
function func<T extends {field1: string}>(arg1: T) {
// function's code
}
我有一个熟悉的泛型接口,如下所示:
interface SomeInterface<T> {
field1: string;
}
现在我想推断接口的泛型参数并将其分配给函数的返回类型:
const value1: SomeInterface<number> = {field1: "Hello"};
const res1 = func(value1);
我需要 res1 的类型是 number。
我需要使用什么函数签名?
function func<T extends {field1: string}>(arg1: T) : T extends ???? { // What?
【问题讨论】:
标签: typescript typescript-generics