簡體   English   中英

如何在Typescript中指定表示2個函數的並集的類型

[英]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}

我想創建另一個僅接受functionAfunctionB作為輸入的functionB ,例如

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

Typescript中是否可以指定僅表示functionAfunctionB的類型?

您不能在特定的函數上進行輸入。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM