0

I know how to define an interface for a function (callable object) with an additional property:

interface namedFunction {
    (x: number): number;
    name: string
}

However, I can't figure out how to create an object of this type, without running into Typescript type errors. How can I instantiate an object that satisfies this interface?

andypea
  • 1,343
  • 11
  • 22
  • 1
    A function's [`.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) is always a string by default. Why would you want to do this? – InSync Jul 23 '23 at 08:19
  • I'm really trying to define generic React components with attached propTypes. This is just the simplest example I could come up with that illustrates the problem I'm running into. – andypea Jul 23 '23 at 08:22
  • 1
    [Ask the real question](https://xyproblem.info) then. – InSync Jul 23 '23 at 08:23
  • Thanks for the tip; it's a good idea in general. However, my main aim is to improve my understanding of Typescript and this technique is useful in many applications irrespective of React. – andypea Jul 23 '23 at 08:29

0 Answers0