Skip to content

Commit cce2ca7

Browse files
committed
add typescript support
1 parent af1c127 commit cce2ca7

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = {
2020
},
2121
],
2222
'@babel/preset-react',
23+
'@babel/preset-typescript',
2324
],
2425
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"@babel/plugin-transform-runtime": "7.10.3",
1818
"@babel/preset-env": "7.10.3",
1919
"@babel/preset-react": "7.10.1",
20+
"@babel/preset-typescript": "^7.12.7",
21+
"@types/react": "^17.0.0",
22+
"@types/react-dom": "^17.0.0",
2023
"babel-loader": "8.1.0",
21-
"lerna": "^3.22.1"
24+
"lerna": "^3.22.1",
25+
"typescript": "^4.1.3"
2226
}
2327
}

packages/devtool-module/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
"description": "Devtool for React Context and useReducer hook",
55
"main": "dist/module/cjs/index.js",
66
"module": "dist/module/esm/index.js",
7+
"types": "dist/types/index.d.ts",
78
"files": [
89
"dist"
910
],
1011
"scripts": {
11-
"compile": "babel src/index.js --config-file ../../babel.config.js --extensions '.js'",
12+
"compile": "babel src/index.tsx --config-file ../../babel.config.js --extensions '.tsx'",
13+
"postcompile": "npm run tsc",
1214
"clean": "rimraf dist/module",
1315
"build:esm": "BABEL_ENV=esm npm run compile -- --out-dir dist/module/esm",
1416
"build:cjs": "BABEL_ENV=cjs npm run compile -- --out-dir dist/module/cjs",
1517
"build": "npm run clean && npm run build:esm && npm run build:cjs",
16-
"prepublishOnly": "npm run build"
18+
"prepublishOnly": "npm run build",
19+
"tsc": "tsc"
1720
},
1821
"repository": {
1922
"type": "git",
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
import React from "react";
22

3-
let debugOptions = {
3+
declare global {
4+
interface Window {
5+
__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK: any;
6+
_REACT_CONTEXT_DEVTOOL: any;
7+
}
8+
}
9+
10+
interface DebugOptions {
11+
debugReducer?: boolean;
12+
debugContext?: boolean;
13+
disable?: boolean;
14+
disableAutoMode?: boolean;
15+
}
16+
17+
interface ContextDevToolProps {
18+
id: string | number;
19+
context: any;
20+
displayName: string;
21+
}
22+
23+
let debugOptions: DebugOptions = {
424
debugReducer: true,
525
debugContext: true,
626
disable: false,
727
disableAutoMode: false,
828
};
929

10-
export const ContextDevTool = ({ id, context: Context, displayName }) => {
30+
export const ContextDevTool = ({
31+
id,
32+
context: Context,
33+
displayName,
34+
}: ContextDevToolProps) => {
1135
if (debugOptions.disable) {
1236
return null;
1337
}
1438

1539
return (
1640
<Context.Consumer>
17-
{(values) => {
41+
{(values: any): null => {
1842
if (typeof window !== "undefined" && window._REACT_CONTEXT_DEVTOOL) {
1943
window._REACT_CONTEXT_DEVTOOL({ id, displayName, values });
2044
}
@@ -24,7 +48,10 @@ export const ContextDevTool = ({ id, context: Context, displayName }) => {
2448
);
2549
};
2650

27-
export const debugContextDevtool = (container, options) => {
51+
export const debugContextDevtool = (
52+
container: HTMLElement | null,
53+
options?: DebugOptions
54+
) => {
2855
debugOptions = { ...debugOptions, ...options };
2956

3057
if (

tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"module": "commonjs",
5+
"noImplicitAny": true,
6+
"esModuleInterop": true,
7+
"outDir": "./packages/devtool-module/dist/types/",
8+
"declaration": true,
9+
"target": "es5"
10+
},
11+
"include": ["packages/devtool-module/src"]
12+
}

0 commit comments

Comments
 (0)