Griffen
Griffen

Reputation: 427

How can I declare the type for a function with properties in TypeScript?

I read some documentation on the TypeScript website, but I'm not sure if I'm understanding correctly. In this scenario, I'm using a package that does not have TypeScript types available (prismic-reactjs) and so I am trying to declare the types myself so that I can get rid of the errors riddling my project.

So, as a basic example, how would I write the declaration for such a function:

const foo = ({ bar }) =>  {
    return bar;
};

foo.baz = () => "string";

I can get this far:

declare module "my-module" {
  interface fooParams {
    bar: string;
  }

  export function foo({ bar }: fooParams): string;
}

But how can I include the baz property?

Upvotes: 2

Views: 1403

Answers (1)

Wong Jia Hau
Wong Jia Hau

Reputation: 3069

You can achieve this using an intersection type.

declare const foo: {
  baz: () => string
} & (({ bar }: {bar: string}) =>  string)

Upvotes: 2

Related Questions