[英]How to specify a type in Typescript which represents the union of 2 functions
Let's suppose that I have 2 function 假设我有2个功能
export const functionA = () => {// do stuff}
export const functionB = () => {// do stuff}
and I want to create another function that accepts as input only either functionA
or functionB
, for instance 我想创建另一个仅接受
functionA
或functionB
作为输入的functionB
,例如
export const anotherFunction = functionAorB => {// do stuff }
Is there a way in Typescript to specify a type representing only either functionA
or functionB
? Typescript中是否可以指定仅表示
functionA
或functionB
的类型?
You can't make a type on a specific function. 您不能在特定的函数上进行输入。
functionA
is a value not a type. functionA
是值而不是类型。 However, you can do: 但是,您可以执行以下操作:
type FuncA = (x: number) => number;
type FuncB = (x: string) => string;
type FuncEither = FuncA | FuncB;
Functions combine in slightly unintuitive ways. 功能以稍微不直观的方式组合。
FuncEither
will be (x: number & string): number | string
FuncEither
将是(x: number & string): number | string
(x: number & string): number | string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.