如何在Typescript中指定表示两个函数并集的类型

如何在Typescript中指定表示两个函数并集的类型,typescript,type-definition,Typescript,Type Definition,假设我有2个函数 export const functionA = () => {// do stuff} export const functionB = () => {// do stuff} 我想创建另一个只接受function或functionB作为输入的函数 export const anotherFunction = functionAorB => {// do stuff } Typescript中是否有一种方法可以指定仅表示functionA或functio

假设我有2个函数

export const functionA = () => {// do stuff}
export const functionB = () => {// do stuff}
我想创建另一个只接受
function
functionB
作为输入的函数

export const anotherFunction = functionAorB => {// do stuff }

Typescript中是否有一种方法可以指定仅表示
functionA
functionB
的类型?

您不能在特定函数上创建类型<代码>函数是一个值而不是一种类型。但是,您可以执行以下操作:

type FuncA = (x: number) => number;
type FuncB = (x: string) => string;
type FuncEither = FuncA | FuncB;

函数以稍微不直观的方式组合<代码>函数将是
(x:number&string):number | string

您不能在特定函数上创建类型<代码>函数是一个值而不是一种类型。但是,您可以执行以下操作:

type FuncA = (x: number) => number;
type FuncB = (x: string) => string;
type FuncEither = FuncA | FuncB;
函数以稍微不直观的方式组合
funcor
将是
(x:number&string):number | string

的可能重复