Skip to content

Commit c88b6c7

Browse files
authored
♻️ Move library to TypeScript (#467)
* ➕ Install typescript dependencies * 📦️ Define `@types/react-native` as an optional peerDependency * Add 🔧 TypeScript configuration file * ♻️ Move `src/index` to TypeScript * ♻️ Move `src/ErrorBoundary/index` to TypeScript * ♻️ Move `src/ErrorBoundary/FallbackComponent` to TypeScript * ♻️ Update `eslint` configuration to TypeScript * ♻️ Move unit tests to TypeScript * ♻️ Replace `flow-bin` with `tsc` * ♻️ Update coverage file extensions * ✨ Add `build` script * 👷 Run `build` in `npm-publish` workflow * 🔧 Add `types` and `react-native` fields to package.json * 🔥 Remove manual declaration file * ✨ Export `ErrorBoundaryProps` and `FallbackComponentProps` * ➖ Remove `@babel/preset-react` dependency
1 parent 70a3775 commit c88b6c7

File tree

16 files changed

+414
-135
lines changed

16 files changed

+414
-135
lines changed

.flowconfig

Lines changed: 0 additions & 6 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
run: yarn
2121
- name: Lint 🎨
2222
run: yarn run lint
23-
- name: Flow types 🏷
24-
run: yarn run flow
23+
- name: Type check 🏷
24+
run: yarn run typecheck
2525
- name: Tests ✅
2626
run: yarn run test
2727
- name: Upload coverage 📈

.github/workflows/npm-publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
node-version: '16'
1414
- name: Install dependencies 📦
1515
run: yarn
16+
- name: Build 👷‍♂️
17+
run: yarn run build
1618
- name: Publish package to NPM 🚀
1719
env:
1820
NPM_TOKEN: ${{ secrets.NPM_AUTOMATION_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
*.log
33
coverage
4+
lib

babel.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
2+
env: {
3+
test: {
4+
presets: ['module:metro-react-native-babel-preset']
5+
}
6+
}
37
};

package.json

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
"name": "react-native-error-boundary",
33
"version": "1.1.17",
44
"description": "A simple and reusable React-Native error boundary component",
5-
"main": "src/index.js",
5+
"main": "lib/index.js",
66
"files": [
77
"src",
8+
"lib",
89
"README.md"
910
],
11+
"react-native": "src/index.tsx",
12+
"types": "lib/index.d.ts",
1013
"scripts": {
11-
"flow": "flow ./src",
14+
"build": "tsc",
15+
"typecheck": "tsc --noEmit",
1216
"lint": "eslint ./src",
1317
"test": "jest",
1418
"coverage:upload": "codecov"
@@ -17,7 +21,10 @@
1721
"preset": "react-native",
1822
"collectCoverage": true,
1923
"collectCoverageFrom": [
20-
"src/**/*.js"
24+
"src/**/*.(ts|tsx)"
25+
],
26+
"modulePathIgnorePatterns": [
27+
"<rootDir>/lib"
2128
]
2229
},
2330
"repository": {
@@ -42,46 +49,57 @@
4249
},
4350
"homepage": "https://github.com/carloscuesta/react-native-error-boundary",
4451
"peerDependencies": {
52+
"@types/react-native": ">=0.57.7",
4553
"react": ">=16.6.0",
4654
"react-native": ">=0.57.7"
4755
},
56+
"peerDependenciesMeta": {
57+
"@types/react-native": {
58+
"optional": true
59+
}
60+
},
4861
"devDependencies": {
4962
"@babel/core": "^7.16.0",
50-
"@babel/eslint-parser": "^7.16.3",
5163
"@babel/preset-env": "^7.18.2",
52-
"@babel/preset-react": "^7.16.0",
64+
"@types/jest": "^29.2.5",
65+
"@types/react-native": "^0.70.8",
66+
"@types/react-test-renderer": "^18.0.0",
67+
"@typescript-eslint/eslint-plugin": "^5.48.0",
68+
"@typescript-eslint/parser": "^5.48.0",
5369
"codecov": "^3.1.0",
5470
"eslint": "^8.2.0",
55-
"eslint-plugin-flowtype": "^8.0.3",
71+
"eslint-import-resolver-typescript": "^3.5.2",
5672
"eslint-plugin-import": "^2.25.3",
5773
"eslint-plugin-jest": "^27.0.1",
5874
"eslint-plugin-react": "^7.27.0",
5975
"eslint-plugin-react-native": "^4.0.0",
60-
"flow-bin": "^0.196.0",
6176
"jest": "^29.0.1",
6277
"metro-babel-register": "^0.73.1",
6378
"react": "^18.0.0",
6479
"react-native": "^0.70.0",
65-
"react-test-renderer": "^18.0.0"
80+
"react-test-renderer": "^18.0.0",
81+
"typescript": "^4.9.4"
6682
},
6783
"dependencies": {},
6884
"eslintConfig": {
69-
"parser": "@babel/eslint-parser",
85+
"parser": "@typescript-eslint/parser",
7086
"env": {
7187
"jest": true,
7288
"react-native/react-native": true
7389
},
7490
"plugins": [
7591
"react",
7692
"react-native",
77-
"flowtype"
93+
"import",
94+
"@typescript-eslint"
7895
],
7996
"extends": [
8097
"eslint:recommended",
81-
"plugin:flowtype/recommended",
98+
"plugin:@typescript-eslint/recommended",
8299
"plugin:react/recommended",
83100
"plugin:import/errors",
84-
"plugin:import/react-native"
101+
"plugin:import/react-native",
102+
"plugin:import/typescript"
85103
],
86104
"parserOptions": {
87105
"ecmaFeatures": {
@@ -93,8 +111,7 @@
93111
},
94112
"settings": {
95113
"react": {
96-
"version": "detect",
97-
"flowVersion": "detect"
114+
"version": "detect"
98115
},
99116
"import/ignore": [
100117
"react-native"

src/ErrorBoundary/FallbackComponent/index.js renamed to src/ErrorBoundary/FallbackComponent/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// @flow
2-
import React, { type Node } from 'react'
1+
import React from 'react'
32
import {
43
SafeAreaView,
54
Text,
@@ -9,9 +8,9 @@ import {
98

109
import styles from './styles'
1110

12-
export type Props = { error: Error, resetError: Function }
11+
export type Props = { error: Error, resetError: () => void }
1312

14-
const FallbackComponent = (props: Props): Node => (
13+
const FallbackComponent = (props: Props) => (
1514
<SafeAreaView style={styles.container}>
1615
<View style={styles.content}>
1716
<Text style={styles.title}>Oops!</Text>

src/ErrorBoundary/FallbackComponent/styles.js renamed to src/ErrorBoundary/FallbackComponent/styles.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// @flow
21
import { StyleSheet } from 'react-native'
32

4-
const styles: any = StyleSheet.create({
3+
const styles = StyleSheet.create({
54
container: {
65
backgroundColor: '#fafafa',
76
flex: 1,

src/ErrorBoundary/index.js renamed to src/ErrorBoundary/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// @flow
2-
import React, { type Node, type ComponentType } from 'react'
1+
import React, { type ComponentType } from 'react'
32

43
import FallbackComponent, {
54
type Props as FallbackComponentProps
65
} from './FallbackComponent'
76

8-
type Props = {
9-
children: Node,
7+
export type Props = {
8+
children: JSX.Element,
109
FallbackComponent: ComponentType<FallbackComponentProps>,
11-
onError?: Function
10+
onError?: (error: Error, stackTrace: string) => void
1211
}
1312

1413
type State = { error: Error | null }
@@ -26,15 +25,15 @@ class ErrorBoundary extends React.Component<Props, State> {
2625

2726
componentDidCatch (error: Error, info: { componentStack: string }) {
2827
if (typeof this.props.onError === 'function') {
29-
this.props.onError.call(this, error, info.componentStack)
28+
this.props.onError(error, info.componentStack)
3029
}
3130
}
3231

33-
resetError: Function = () => {
32+
resetError: () => void = () => {
3433
this.setState({ error: null })
3534
}
3635

37-
render (): Node {
36+
render () {
3837
const { FallbackComponent } = this.props
3938

4039
return this.state.error

0 commit comments

Comments
 (0)