Skip to content

Commit aaf1990

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Use TypeScript by default for new applications (facebook#35165)
Summary: This change moves the default new application template in OSS from Flow to TypeScript. This better aligns with the communities usage, and aligns to the great work that has been happening for TS codegen and built-in types. This used [`react-native-community/react-native-template-typescript`](https://github.com/react-native-community/react-native-template-typescript) as a main reference, maintained by radko93. A few things are different: 1. Updated `types/*` devDependencies to match bumped libraries (e.g. Jest 26 to 20). 2. Removed `types/react-native` 3. Removed explicit `moduleFileExtensions` to Jest config in package.json (TS and TSX and added by default in current versions) 4. Removed overrides to eslint config to disable `no-shadow` and `no-undef`, since this was fixed in the underlying eslint config with facebook#32644 and facebook#32655 5. Re-translated `App.js` to be a direct translation (e.g. still a raw function instead of React.FC which is sometimes discouraged) 6. Aligns completely to `tsconfig/react-native` maintained configuration (We no longer have the opinionated override of `skipLibCheck`). The important settings are that `strict` is enabled for, but `allowJS` is also enabled to let users import JS modules without anything unexpected. `esModuleInterop` is enabled, which is needed for consistency with Babel import transformations used by Metro. Consistent with our [current documentation](https://reactnative.dev/docs/typescript) built against the community template, `tsc` will typecheck your code without emitting(building) anything. [Documentation](https://reactnative.dev/docs/typescript) will need to be updated as a followup. Changelog: [General][Changed] - Use TypeScript by default for new applications Pull Request resolved: facebook#35165 Test Plan: Added usage of `tsc` and `jest` when validating a newly built app. Passes in CircleCI `test_js` job. This also meant removing some cases of copying packages into the users app to pull in the root Metro config (we seem to be able to use the template Metro config consistent with what real apps would get). Reviewed By: cortinico Differential Revision: D40911951 Pulled By: NickGerleman fbshipit-source-id: 15994534235695e91cf994ad06ba2183dfc89a50
1 parent f207cfd commit aaf1990

File tree

8 files changed

+35
-98
lines changed

8 files changed

+35
-98
lines changed

scripts/run-ci-e2e-tests.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,9 @@ try {
8989
exec(`rsync -a ${ROOT}/template ${REACT_NATIVE_TEMP_DIR}`);
9090
cd(REACT_NATIVE_APP_DIR);
9191

92-
const METRO_CONFIG = path.join(ROOT, 'metro.config.js');
93-
const RN_GET_POLYFILLS = path.join(ROOT, 'rn-get-polyfills.js');
94-
const RN_POLYFILLS_PATH = 'packages/polyfills/';
95-
exec(`mkdir -p ${RN_POLYFILLS_PATH}`);
96-
97-
cp(METRO_CONFIG, '.');
98-
cp(RN_GET_POLYFILLS, '.');
99-
exec(
100-
`rsync -a ${ROOT}/${RN_POLYFILLS_PATH} ${REACT_NATIVE_APP_DIR}/${RN_POLYFILLS_PATH}`,
101-
);
102-
mv('_flowconfig', '.flowconfig');
103-
mv('_watchmanconfig', '.watchmanconfig');
10492
mv('_bundle', '.bundle');
93+
mv('_eslintrc.js', '.eslintrc.js');
94+
mv('_watchmanconfig', '.watchmanconfig');
10595

10696
describe('Install React Native package');
10797
exec(`npm install ${REACT_NATIVE_PACKAGE}`);
@@ -267,6 +257,7 @@ try {
267257
exitCode = 1;
268258
throw Error(exitCode);
269259
}
260+
270261
describe('Test: Verify packager can generate an iOS bundle');
271262
if (
272263
exec(
@@ -277,12 +268,17 @@ try {
277268
exitCode = 1;
278269
throw Error(exitCode);
279270
}
280-
describe('Test: Flow check');
281-
// The resolve package included a test for a malformed package.json (see https://github.com/browserify/resolve/issues/89)
282-
// that is failing the flow check. We're removing it.
283-
rm('-rf', './node_modules/resolve/test/resolver/malformed_package_json');
284-
if (exec(`${ROOT}/node_modules/.bin/flow check`).code) {
285-
echo('Flow check failed.');
271+
272+
describe('Test: TypeScript typechecking');
273+
if (exec('yarn tsc').code) {
274+
echo('Typechecking errors were found');
275+
exitCode = 1;
276+
throw Error(exitCode);
277+
}
278+
279+
describe('Test: Jest tests');
280+
if (exec('yarn test').code) {
281+
echo('Jest tests failed');
286282
exitCode = 1;
287283
throw Error(exitCode);
288284
}

template/App.js renamed to template/App.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* https://github.com/facebook/react-native
44
*
55
* @format
6-
* @flow strict-local
76
*/
87

98
import React from 'react';
10-
import type {Node} from 'react';
9+
import type {PropsWithChildren} from 'react';
1110
import {
1211
SafeAreaView,
1312
ScrollView,
@@ -26,12 +25,11 @@ import {
2625
ReloadInstructions,
2726
} from 'react-native/Libraries/NewAppScreen';
2827

29-
type SectionProps = {
30-
title: string,
31-
children: Node,
32-
};
28+
type SectionProps = PropsWithChildren<{
29+
title: string;
30+
}>;
3331

34-
function Section({children, title}: SectionProps): Node {
32+
function Section({children, title}: SectionProps): JSX.Element {
3533
const isDarkMode = useColorScheme() === 'dark';
3634
return (
3735
<View style={styles.sectionContainer}>
@@ -57,7 +55,7 @@ function Section({children, title}: SectionProps): Node {
5755
);
5856
}
5957

60-
function App(): Node {
58+
function App(): JSX.Element {
6159
const isDarkMode = useColorScheme() === 'dark';
6260

6361
const backgroundStyle = {
File renamed without changes.

template/_buckconfig

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

template/_eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = {
22
root: true,
33
extends: '@react-native-community',
4+
parser: '@typescript-eslint/parser',
5+
plugins: ['@typescript-eslint'],
46
};

template/_flowconfig

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

template/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"scripts": {
66
"android": "react-native run-android",
77
"ios": "react-native run-ios",
8+
"lint": "eslint .",
89
"start": "react-native start",
9-
"test": "jest",
10-
"lint": "eslint ."
10+
"test": "jest"
1111
},
1212
"dependencies": {
1313
"react": "18.2.0",
@@ -17,11 +17,18 @@
1717
"@babel/core": "^7.12.9",
1818
"@babel/runtime": "^7.12.5",
1919
"@react-native-community/eslint-config": "^3.0.0",
20+
"@tsconfig/react-native": "^2.0.2",
21+
"@types/jest": "^29.2.1",
22+
"@types/react": "^18.0.24",
23+
"@types/react-test-renderer": "^18.0.0",
24+
"@typescript-eslint/eslint-plugin": "^5.37.0",
25+
"@typescript-eslint/parser": "^5.37.0",
2026
"babel-jest": "^29.2.1",
2127
"eslint": "^8.19.0",
2228
"jest": "^29.2.1",
2329
"metro-react-native-babel-preset": "0.73.3",
24-
"react-test-renderer": "18.2.0"
30+
"react-test-renderer": "18.2.0",
31+
"typescript": "^4.8.3"
2532
},
2633
"jest": {
2734
"preset": "react-native"

template/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@tsconfig/react-native/tsconfig.json"
3+
}

0 commit comments

Comments
 (0)