0

Let's suppose that I have 2 function

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

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

Is there a way in Typescript to specify a type representing only either functionA or functionB?

1

1 Answer 1

2

You can't make a type on a specific function. functionA is a value not a type. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.