如何扩展 return 类型?

How to extend the return type?

这是我想要完成的伪语法。

type Potatoizer<T> = (input:T) => (T extends {potato: number})

当您发现自己想要扩展无法在其上使用 extends 的类型时,请考虑使用 intersection types。在你的情况下,你有一个通用的 T 所以你不能做 interface WithPotato extends T,你也不能做 T extends {potato: number} 因为传入的类型 T 实际上并没有扩展那。你可以做的是:

type Potatoizer<T> = (input:T) => T & {potato: int}

(什么是 int?你是说 number 吗?)

然后 Potatoizer<T> 是一个函数的类型,它接受某种类型的参数和 returns 一个具有额外 potato 属性 的相同类型的值在它的类型 int (或任何你的意思)上。


希望对您有所帮助;祝你好运!