0

I am trying to declare the type of a function that can take function or object as a parameter, but the result is Cannot call `data` because a callable signature is missing in `TestObject` [1].

This is basic code example what I trying to achieve.

type TestObject = {
  name: string
}

const dummyFunction = (data: TestObject | (props:Object)=> TestObject) => {
  typeof data === 'function' ? props => data(props): data;

  return {} // empty object just for the test purpose
} 

or link to flow-type

What I am missed?

10
  • Shouldn't your ternary operator be typeof data === 'function' ? data : () => data if you're trying to normalize it to a function? data(props) is the issue, because in that part of the ternary, data is an object and not a function. Commented Oct 31, 2018 at 19:28
  • @PatrickRoberts thank you, I fixed it but the issue is still there.
    – Grund
    Commented Oct 31, 2018 at 19:50
  • Before I answer the question, what exactly is the return type supposed to be? Commented Oct 31, 2018 at 19:52
  • 1
    But props => data(props) doesn't do anything. You create a function that is never executed or returned. And since that bit is exactly what giving you the error, it'll help to know what you're trying to do there.
    – Alex Wayne
    Commented Oct 31, 2018 at 20:11
  • 1
    By the way, the issue is that your TestObject type isn't mutually exclusive with a function, so the data === 'function' isn't enough to determine that you're not dealing with a TestObject. Commented Oct 31, 2018 at 20:22

0

Your Answer

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

Browse other questions tagged or ask your own question.