0

I just needed to type an argument of one of my functions. Not a very uncommon task, but this time it could be two types, a string or a function. So I tried

function listen (event: string | () => void): void {}

Example

enter image description here

This gives an error. Typescript doesn't understand anymore whats going on.

However, one way to fix this is to swap them

 function listen (event: () => void | string): void {}

Well I guess it makes sense. Can someone explain to me whats going on here or point me to the right documentation

1
  • 1
    () => void | string means () => (void | string). You want (() => void) | string or string | (() => void). Commented Jul 25, 2019 at 21:05

1 Answer 1

2

It looks like, inside a type expression, the parser expects ( to open a group only.

Wrap the function type in parentheses and it will work.

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.