react-sizes icon indicating copy to clipboard operation
react-sizes copied to clipboard

Add typescript typings

Open renatorib opened this issue 8 years ago • 5 comments

renatorib avatar Aug 29 '17 21:08 renatorib

any news? )

yoyurec avatar Mar 25 '19 15:03 yoyurec

Unfortunately not, but we are open to pull-requests if anyone is willing to help!

renatorib avatar Mar 26 '19 21:03 renatorib

The following piece in the declarations.d.ts works for me with the withSizes function:

declare module 'react-sizes' {
  export interface Sizes {
    width: number | false;
    height: number | false;
  }
  type mapSizesToProps<SP extends object> = (sizes: Sizes) => SP;

  export function withSizes<SP extends object, P extends SP>(
    mapSizesToProps: mapSizesToProps<SP>,
  ): (component: React.ComponentType<P>) => React.ComponentType<P>;

  export default withSizes;
}

Note: This way, the output component expects the same props as the input component. Since withSizes already sets some props, this might raise issues with mandatory props. Therefore, you could e.g. use the Subtract<T, T1> type of the utility-types package. But I wanted to keep it simple for my use case.

janKir avatar Apr 16 '19 16:04 janKir

thanks for contributing these types @janKir! I just created a PR to DefinitelyTyped with the types from your comment https://github.com/DefinitelyTyped/DefinitelyTyped/pull/36058

the only modification that I made as to remove the | false from the Sizes interface. In my usage at least, I didn't find a case where either width or height was false or boolean. Otherwise, these type definitions work great in my project.

micahstubbs avatar May 31 '19 20:05 micahstubbs

meant to post this here earlier, but my PR made it into DefinitelyTyped 🎉

you can see the types for react-sizes here: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-sizes

you can use the types in your TypeScript project with this command:

yarn add -D @types/react-sizes

micahstubbs avatar Aug 29 '19 23:08 micahstubbs