From 6317726538d6b9465cc5a5cb7a1388c7bf0d85ee Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Tue, 9 Jul 2019 12:56:58 -0700 Subject: [PATCH] add type definitions for react-sizes (#36058) * add type definitions for react-sizes * conform Definitions by: to expected format * test file extension is .tsx, not .ts * specify typescript version in header, remove old comment * "strictFunctionTypes": true, to satisfy the CI gods * make testProp foo optional * add "jsx": "react" compiler option * change TestInnerProps to an interface, update expectation for foo * add type definitions for react-sizes * conform Definitions by: to expected format * test file extension is .tsx, not .ts * specify typescript version in header, remove old comment * "strictFunctionTypes": true, to satisfy the CI gods * make testProp foo optional * add "jsx": "react" compiler option * change TestInnerProps to an interface, update expectation for foo * fix example in test * remove trailing white space. change TestProps from interface to Type * use interface instead * explicit exports * specify return type in test example * add type definitions for react-sizes * conform Definitions by: to expected format * test file extension is .tsx, not .ts * specify typescript version in header, remove old comment * "strictFunctionTypes": true, to satisfy the CI gods * make testProp foo optional * add "jsx": "react" compiler option * change TestInnerProps to an interface, update expectation for foo * add type definitions for react-sizes * fix example in test * remove trailing white space. change TestProps from interface to Type * use interface instead * explicit exports * specify return type in test example * use PascalCase for type names * PascalCase for MapSizesToProps * inline type for mapSizesToProps function * fix import * remove errant ; * remove lib compiler option in tsconfig * Revert "remove lib compiler option in tsconfig" This reverts commit 97d100788581e2bfacc563d676af4ad3020a68a1. * export WithSizes as default and import it as default in test. retain named export of WithSizes to match react-sizes library's export pattern --- types/react-sizes/index.d.ts | 16 ++++++++++++++ types/react-sizes/react-sizes-tests.tsx | 29 +++++++++++++++++++++++++ types/react-sizes/tsconfig.json | 24 ++++++++++++++++++++ types/react-sizes/tslint.json | 1 + 4 files changed, 70 insertions(+) create mode 100644 types/react-sizes/index.d.ts create mode 100644 types/react-sizes/react-sizes-tests.tsx create mode 100644 types/react-sizes/tsconfig.json create mode 100644 types/react-sizes/tslint.json diff --git a/types/react-sizes/index.d.ts b/types/react-sizes/index.d.ts new file mode 100644 index 0000000000..8b00c1bf65 --- /dev/null +++ b/types/react-sizes/index.d.ts @@ -0,0 +1,16 @@ +// Type definitions for react-sizes 2.0 +// Project: https://github.com/renatorib/react-sizes#readme +// Definitions by: janKir , Micah Stubbs +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +export interface Sizes { + width: number; + height: number; +} + +export function WithSizes( + mapSizesToProps: (sizes: Sizes) => SP +): (component: React.ComponentType

) => React.ComponentType

; + +export default WithSizes; diff --git a/types/react-sizes/react-sizes-tests.tsx b/types/react-sizes/react-sizes-tests.tsx new file mode 100644 index 0000000000..af77fe250b --- /dev/null +++ b/types/react-sizes/react-sizes-tests.tsx @@ -0,0 +1,29 @@ +import * as React from 'react'; +import WithSizes, { Sizes } from 'react-sizes'; + +interface TestProps { + foo: string; + width: number; + height: number; +} + +const mapSizesToProps = ({ width, height }: Sizes): TestProps => ({ + foo: 'foo', + width, + height, +}); + +const TestComponent: React.ComponentType = ({ foo, width, height }) => { + foo; // $ExpectType string + width; // $ExpectType number + height; // $ExpectType number + return ( +

+

Foo: {foo}

+

Window width: {width}

+

Window height: {height}

+
+ ); +}; + +WithSizes(mapSizesToProps)(TestComponent); // $ExpectType ComponentType diff --git a/types/react-sizes/tsconfig.json b/types/react-sizes/tsconfig.json new file mode 100644 index 0000000000..a44cbfb31c --- /dev/null +++ b/types/react-sizes/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react" + }, + "files": [ + "index.d.ts", + "react-sizes-tests.tsx" + ] +} diff --git a/types/react-sizes/tslint.json b/types/react-sizes/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-sizes/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }