Skip to content

Commit 80eff96

Browse files
committed
refactor: add typescript support
1 parent 64846b8 commit 80eff96

39 files changed

+356
-13
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"exclude": ["@babel/plugin-transform-regenerator"]
99
}
1010
],
11+
"@babel/preset-typescript",
1112
"@babel/react"
1213
],
1314
"plugins": ["babel-plugin-transform-async-to-promises"],

.eslintrc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
{
2-
"parser": "babel-eslint",
3-
"extends": ["react-app", "prettier"],
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint"],
4+
"extends": [
5+
"plugin:@typescript-eslint/eslint-recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"react-app",
8+
"prettier"
9+
],
410
"env": {
511
"es6": true
612
},
713
"parserOptions": {
814
"sourceType": "module"
15+
},
16+
"rules": {
17+
"@typescript-eslint/explicit-module-boundary-types": "off"
918
}
1019
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"test:dev": "jest --watch",
2020
"test:ci": "jest && yarn dtslint",
2121
"test:coverage": "yarn test:ci; open coverage/lcov-report/index.html",
22+
"test:types": "tsc",
23+
"test:eslint": "eslint --ext .ts,.tsx ./src",
24+
"gen:types": "tsc --project ./tsconfig.types.json",
2225
"build": "NODE_ENV=production rollup -c",
2326
"now-build": "yarn && cd www && yarn && yarn build",
2427
"start": "rollup -c -w",
@@ -56,10 +59,14 @@
5659
"@babel/core": "^7.10.2",
5760
"@babel/preset-env": "^7.10.2",
5861
"@babel/preset-react": "^7.10.1",
62+
"@babel/preset-typescript": "^7.10.4",
5963
"@rollup/plugin-replace": "^2.3.3",
6064
"@svgr/rollup": "^5.4.0",
6165
"@testing-library/react": "^10.2.1",
66+
"@types/jest": "^26.0.4",
6267
"@types/react": "^16.9.41",
68+
"@typescript-eslint/eslint-plugin": "^3.6.1",
69+
"@typescript-eslint/parser": "^3.6.1",
6370
"babel-eslint": "^10.1.0",
6471
"babel-jest": "^26.0.1",
6572
"babel-plugin-transform-async-to-promises": "^0.8.15",

rollup.config.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const globals = {
1313
react: 'React',
1414
}
1515

16-
const inputSrc = 'src/react/index.js'
16+
const inputSrc = 'src/index.ts'
17+
18+
const extensions = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']
19+
const babelConfig = { extensions }
20+
const resolveConfig = { extensions }
1721

1822
export default [
1923
{
@@ -24,7 +28,12 @@ export default [
2428
sourcemap: true,
2529
},
2630
external,
27-
plugins: [resolve(), babel(), commonJS(), externalDeps()],
31+
plugins: [
32+
resolve(resolveConfig),
33+
babel(babelConfig),
34+
commonJS(),
35+
externalDeps(),
36+
],
2837
},
2938
{
3039
input: inputSrc,
@@ -34,7 +43,13 @@ export default [
3443
sourcemap: true,
3544
},
3645
external,
37-
plugins: [resolve(), babel(), commonJS(), externalDeps(), terser()],
46+
plugins: [
47+
resolve(resolveConfig),
48+
babel(babelConfig),
49+
commonJS(),
50+
externalDeps(),
51+
terser(),
52+
],
3853
},
3954
{
4055
input: inputSrc,
@@ -46,7 +61,12 @@ export default [
4661
globals,
4762
},
4863
external,
49-
plugins: [resolve(), babel(), commonJS(), externalDeps()],
64+
plugins: [
65+
resolve(resolveConfig),
66+
babel(babelConfig),
67+
commonJS(),
68+
externalDeps(),
69+
],
5070
},
5171
{
5272
input: inputSrc,
@@ -60,8 +80,8 @@ export default [
6080
external,
6181
plugins: [
6282
replace({ 'process.env.NODE_ENV': `"production"`, delimiters: ['', ''] }),
63-
resolve(),
64-
babel(),
83+
resolve(resolveConfig),
84+
babel(babelConfig),
6585
commonJS(),
6686
externalDeps(),
6787
terser(),
File renamed without changes.
File renamed without changes.

src/core/query.js renamed to src/core/query.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ import {
1313
} from './utils'
1414
import { makeQueryInstance } from './queryInstance'
1515

16+
export interface QueryState<T, TError = Error> {
17+
data?: T
18+
error?: TError | null
19+
failureCount: number
20+
isFetching: boolean
21+
canFetchMore?: boolean
22+
isStale: boolean
23+
status: 'idle' | 'loading' | 'error' | 'success'
24+
updatedAt: number
25+
markedForGarbageCollection: boolean
26+
}
27+
1628
const actionInit = 'Init'
1729
const actionFailed = 'Failed'
1830
const actionMarkStale = 'MarkStale'
@@ -405,13 +417,13 @@ export function makeQuery({
405417
return query
406418
}
407419

408-
export function queryReducer(state, action) {
420+
export function queryReducer<T>(state: QueryState<T>, action) {
409421
const newState = switchActions(state, action)
410422

411423
return Object.assign(newState, getStatusBools(newState.status))
412424
}
413425

414-
function switchActions(state, action) {
426+
function switchActions<T>(state: QueryState<T>, action): QueryState<T> {
415427
switch (action.type) {
416428
case actionInit:
417429
return {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './react/index'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true,
4+
"jsx": "react",
5+
"lib": ["es6", "dom"],
6+
"module": "es6",
7+
"noEmit": true,
8+
"noImplicitAny": true,
9+
"noImplicitReturns": true,
10+
"noImplicitThis": true,
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true,
13+
"strict": true
14+
},
15+
"include": ["./src/**/*"]
16+
}

tsconfig.types.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"declarationDir": "./types",
6+
"emitDeclarationOnly": true,
7+
"noEmit": false
8+
},
9+
"files": ["./src/index.ts"],
10+
"exclude": ["./src/**/*"]
11+
}

0 commit comments

Comments
 (0)