Skip to content

Commit 3b12b32

Browse files
committed
Adds typescript typings tests
1 parent de33200 commit 3b12b32

File tree

8 files changed

+128
-17
lines changed

8 files changed

+128
-17
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
commonjs/
33
coverage/
44
umd/
5+
src/__tests__/typescript/

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"react-dom": "^0.14.0 || ^15.0.0-0 || ^16.0.0"
4444
},
4545
"devDependencies": {
46+
"@types/react": "^16.8.8",
47+
"@types/react-dom": "^16.8.2",
4648
"app-root-dir": "1.0.2",
4749
"babel-cli": "^6.26.0",
4850
"babel-core": "^6.26.3",
@@ -84,7 +86,9 @@
8486
"rimraf": "^2.6.2",
8587
"rollup": "^0.60.7",
8688
"rollup-plugin-babel": "^3.0.4",
87-
"rollup-plugin-uglify": "^4.0.0"
89+
"rollup-plugin-uglify": "^4.0.0",
90+
"typescript": "^3.4.5",
91+
"typings-tester": "^0.3.2"
8892
},
8993
"jest": {
9094
"collectCoverageFrom": [

react-sizeme.d.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { Component, ComponentType } from 'react'
1+
import { Component, ComponentType, ReactNode } from 'react'
22

3-
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
3+
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
44

55
interface SizeMeProps {
66
size: {
7-
width: number | null;
8-
height: number | null;
9-
};
7+
width: number | null
8+
height: number | null
9+
}
1010
}
1111

1212
export interface SizeMeOptions {
13-
monitorWidth?: boolean;
14-
monitorHeight?: boolean;
15-
monitorPosition?: boolean;
16-
refreshRate?: number;
17-
refreshMode?: 'throttle' | 'debounce';
18-
noPlaceholder?: boolean;
19-
children(props: SizeMeProps): JSX.Element;
13+
monitorWidth?: boolean
14+
monitorHeight?: boolean
15+
monitorPosition?: boolean
16+
refreshRate?: number
17+
refreshMode?: 'throttle' | 'debounce'
18+
noPlaceholder?: boolean
19+
children(props: SizeMeProps): ReactNode
2020
}
2121

22-
export class SizeMe extends Component<SizeMeOptions> {
23-
}
22+
export class SizeMe extends Component<SizeMeOptions> {}
2423

25-
export const withSize: (options?: SizeMeOptions) => <P extends SizeMeProps>(component: ComponentType<P>) => ComponentType<Omit<P, 'size'>>;
24+
export const withSize: (
25+
options?: SizeMeOptions,
26+
) => <P extends SizeMeProps>(
27+
component: ComponentType<P>,
28+
) => ComponentType<Omit<P, 'size'>>

src/__tests__/typescript.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { checkDirectory } from 'typings-tester'
2+
3+
describe('TypeScript definitions', () => {
4+
it('should compile against easy-peasy.d.ts', () => {
5+
checkDirectory(`${__dirname}/typescript`)
6+
})
7+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react'
2+
import { SizeMe } from 'react-sizeme'
3+
4+
function MyApp() {
5+
return (
6+
<SizeMe>
7+
{({ size }) => {
8+
const { width, height } = size
9+
if (width) {
10+
const foo = width + 1
11+
}
12+
if (height) {
13+
const foo = height + 1
14+
}
15+
// typings:expect-error
16+
const h1 = height + 1
17+
// typings:expect-error
18+
const w1 = width + 1
19+
return <div />
20+
}}
21+
</SizeMe>
22+
)
23+
}

src/__tests__/typescript/hoc.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as React from 'react'
2+
import { withSize, SizeMeProps } from 'react-sizeme'
3+
4+
interface MyComponentProps extends SizeMeProps {
5+
id: number
6+
}
7+
8+
function MyComponent({ id, size }: MyComponentProps) {
9+
const { width, height } = size
10+
if (width) {
11+
const foo = width + 1
12+
}
13+
if (height) {
14+
const foo = height + 1
15+
}
16+
// typings:expect-error
17+
const h1 = height + 1
18+
// typings:expect-error
19+
const w1 = width + 1
20+
return <div>My width is {size.width}px</div>
21+
}
22+
23+
const SizedMyComponent = withSize()(MyComponent)
24+
25+
const foo = <SizedMyComponent id={1} />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["esnext", "dom"],
4+
"jsx": "preserve",
5+
"strict": true,
6+
"baseUrl": "../../..",
7+
"paths": {
8+
"react-sizeme": ["react-sizeme.d.ts"]
9+
}
10+
}
11+
}

yarn.lock

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@
124124
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.8.tgz#e5d71173c95533be9842b2c798978f095f912aab"
125125
integrity sha512-szA3x/3miL90ZJxUCzx9haNbK5/zmPieGraZEe4WI+3srN0eGLiT22NXeMHmyhNEopn+IrxqMc7wdVwvPl8meg==
126126

127+
"@types/prop-types@*":
128+
version "15.7.1"
129+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.1.tgz#f1a11e7babb0c3cad68100be381d1e064c68f1f6"
130+
integrity sha512-CFzn9idOEpHrgdw8JsoTkaDDyRWk1jrzIV8djzcgpq0y9tG4B4lFT+Nxh52DVpDXV+n4+NPNv7M1Dj5uMp6XFg==
131+
132+
"@types/react-dom@^16.8.2":
133+
version "16.8.4"
134+
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.8.4.tgz#7fb7ba368857c7aa0f4e4511c4710ca2c5a12a88"
135+
integrity sha512-eIRpEW73DCzPIMaNBDP5pPIpK1KXyZwNgfxiVagb5iGiz6da+9A5hslSX6GAQKdO7SayVCS/Fr2kjqprgAvkfA==
136+
dependencies:
137+
"@types/react" "*"
138+
139+
"@types/react@*", "@types/react@^16.8.8":
140+
version "16.8.14"
141+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.8.14.tgz#b561bfabeb8f60d12e6d4766367e7a9ae927aa18"
142+
integrity sha512-26tFVJ1omGmzIdFTFmnC5zhz1GTaqCjxgUxV4KzWvsybF42P7/j4RBn6UeO3KbHPXqKWZszMXMoI65xIWm954A==
143+
dependencies:
144+
"@types/prop-types" "*"
145+
csstype "^2.2.0"
146+
127147
abab@^2.0.0:
128148
version "2.0.0"
129149
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.0.tgz#aba0ab4c5eee2d4c79d3487d85450fb2376ebb0f"
@@ -1535,7 +1555,7 @@ combined-stream@^1.0.5, combined-stream@^1.0.6, combined-stream@~1.0.5, combined
15351555
dependencies:
15361556
delayed-stream "~1.0.0"
15371557

1538-
commander@^2.11.0, commander@^2.14.1, commander@^2.19.0, commander@^2.9.0, commander@~2.20.0:
1558+
commander@^2.11.0, commander@^2.12.2, commander@^2.14.1, commander@^2.19.0, commander@^2.9.0, commander@~2.20.0:
15391559
version "2.20.0"
15401560
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
15411561
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==
@@ -1680,6 +1700,11 @@ cssstyle@^1.0.0:
16801700
dependencies:
16811701
cssom "0.3.x"
16821702

1703+
csstype@^2.2.0:
1704+
version "2.6.4"
1705+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.4.tgz#d585a6062096e324e7187f80e04f92bd0f00e37f"
1706+
integrity sha512-lAJUJP3M6HxFXbqtGRc0iZrdyeN+WzOWeY0q/VnFzI+kqVrYIzC7bWlKqCW7oCIdzoPkvfp82EVvrTlQ8zsWQg==
1707+
16831708
damerau-levenshtein@^1.0.4:
16841709
version "1.0.4"
16851710
resolved "https://registry.yarnpkg.com/damerau-levenshtein/-/damerau-levenshtein-1.0.4.tgz#03191c432cb6eea168bb77f3a55ffdccb8978514"
@@ -6072,6 +6097,18 @@ typedarray@^0.0.6:
60726097
version "0.0.6"
60736098
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
60746099

6100+
typescript@^3.4.5:
6101+
version "3.4.5"
6102+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
6103+
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==
6104+
6105+
typings-tester@^0.3.2:
6106+
version "0.3.2"
6107+
resolved "https://registry.yarnpkg.com/typings-tester/-/typings-tester-0.3.2.tgz#04cc499d15ab1d8b2d14dd48415a13d01333bc5b"
6108+
integrity sha512-HjGoAM2UoGhmSKKy23TYEKkxlphdJFdix5VvqWFLzH1BJVnnwG38tpC6SXPgqhfFGfHY77RlN1K8ts0dbWBQ7A==
6109+
dependencies:
6110+
commander "^2.12.2"
6111+
60756112
uglify-js@^3.1.4:
60766113
version "3.5.9"
60776114
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.9.tgz#372fbf95939555b1f460b1777d33a67d4a994ac9"

0 commit comments

Comments
 (0)