[英]How to specify a type in Typescript which represents the union of 2 functions
假設我有2個功能
export const functionA = () => {// do stuff}
export const functionB = () => {// do stuff}
我想創建另一個僅接受functionA
或functionB
作為輸入的functionB
,例如
export const anotherFunction = functionAorB => {// do stuff }
Typescript中是否可以指定僅表示functionA
或functionB
的類型?
您不能在特定的函數上進行輸入。 functionA
是值而不是類型。 但是,您可以執行以下操作:
type FuncA = (x: number) => number;
type FuncB = (x: string) => string;
type FuncEither = FuncA | FuncB;
功能以稍微不直觀的方式組合。 FuncEither
將是(x: number & string): number | string
(x: number & string): number | string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.