Friday 3 September 2021

Define class in Typescript module

I'm writing a Typescript module for an external library.

Here's a piece of my index.d.ts

declare module 'my-external-library' {
  export class MyComponent extends React.Component<MyComponentProps> {}

  export class CustomClass {
    constructor(
     firstParam: string,
     secondParam: string,
    )
  }

  interface MyComponentProps {
    config: string
    customClass: CustomClass
    text?: string
    customFn?: (input: string) => boolean
  }

}

This works fine except for the customClass prop. Custom Class is a class that has to be like this:

class customClass {
  constructor(firstParam, secondParam) {
    this.firstParam = firstParam
    this.secondParam = secondParam
  }

  ... all the methods you want
}

export default customClass

In a separate file I give the customClass props to MyComponent, like this:


import customClass from './customClass'

<MyComponent
    config: "test"
    customClass: customClass
/>

customClass actually is not receiving the "constructor types" declared in index.d.ts, but all other props of the component works well. So, the class definition I've made is correct? I have to export the class with another approach?

Thanks for any help, forgive my inexperience



from Define class in Typescript module

No comments:

Post a Comment