如何扩展返回类型?

Bir*_*sky 5 typescript typescript2.0

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

type Potatoizer<T> = (input:T) => (T extends {potato: number})
Run Code Online (Sandbox Code Playgroud)

jca*_*alz 8

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

type Potatoizer<T> = (input:T) => T & {potato: int}
Run Code Online (Sandbox Code Playgroud)

(什么是int?你的意思是number?)

然后Potatoizer<T>是函数的类型,它接受某种类型的参数并返回相同类型的值,并potato在其类型上有一个额外的属性int(或任何你想要的类型)。


希望有帮助;祝你好运!