Skip to content

Commit c019942

Browse files
authored
Always type check TypeScript when being used (#5515)
* Always type check TypeScript when being used * Use alternate version
1 parent 07ff5a0 commit c019942

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

docusaurus/docs/adding-typescript.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ title: Adding TypeScript
55

66
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
77

8-
Recent versions of [TypeScript](https://www.typescriptlang.org/) work with Create React App projects out of the box thanks to Babel 7. Beware that Babel 7 TypeScript does not allow some features of TypeScript such as constant enum and namespaces.
8+
Recent versions of [TypeScript](https://www.typescriptlang.org/) work with Create React App projects out of the box thanks to Babel 7. Note that Babel 7 TypeScript does not allow some features of TypeScript such as constant enum and namespaces.
99

1010
To add TypeScript to a Create React App project, follow these steps:
1111

12-
1. Run `npm install --save typescript fork-ts-checker-webpack-plugin @types/react @types/react-dom @types/jest` (or `yarn add typescript fork-ts-checker-webpack-plugin @types/react @types/react-dom @types/jest`).
13-
2. Rename the `.js` files you want to convert. Use `.tsx` if they use JSX or `.ts` if not (e.g. `git mv src/index.js src/index.tsx`).
12+
1. Run `npm install --save typescript @types/react @types/react-dom @types/jest` (or `yarn add typescript @types/react @types/react-dom @types/jest`).
13+
2. Rename the `.js` files you want to convert: use `.tsx` if they use JSX or `.ts` if not (e.g. `git mv src/index.js src/index.tsx`).
1414

1515
3. Create a [`tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) at the root directory with the following content:
1616

@@ -38,8 +38,6 @@ To add TypeScript to a Create React App project, follow these steps:
3838

3939
Type errors will show up in the same console as the build one.
4040

41-
> Note: If you prefer to run type checking separately from the build process, you can run `npm uninstall fork-ts-checker-webpack-plugin` (or `yarn remove fork-ts-checker-webpack-plugin`) to remove the `fork-ts-checker-webpack-plugin` dependency and then `npx tsc -w` on a new terminal tab.
42-
4341
We recommend using [VSCode](https://code.visualstudio.com/) for a better integrated experience.
4442

4543
To learn more about TypeScript, check out [its documentation](https://www.typescriptlang.org/).

packages/react-scripts/config/webpack.config.dev.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const getClientEnvironment = require('./env');
2323
const paths = require('./paths');
2424
const ManifestPlugin = require('webpack-manifest-plugin');
2525
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
26+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
2627
// @remove-on-eject-begin
2728
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
2829
// @remove-on-eject-end
@@ -410,27 +411,15 @@ module.exports = {
410411
}),
411412
// TypeScript type checking
412413
fs.existsSync(paths.appTsConfig) &&
413-
(() => {
414-
let ForkTsCheckerWebpackPlugin;
415-
try {
416-
ForkTsCheckerWebpackPlugin = require(resolve.sync(
417-
'fork-ts-checker-webpack-plugin',
418-
{ basedir: paths.appNodeModules }
419-
));
420-
} catch (e) {
421-
// Fail silently.
422-
// Type checking using this plugin is optional.
423-
// The user may decide to install `fork-ts-checker-webpack-plugin` or use `tsc -w`.
424-
return null;
425-
}
426-
427-
return new ForkTsCheckerWebpackPlugin({
428-
async: false,
429-
checkSyntacticErrors: true,
430-
tsconfig: paths.appTsConfig,
431-
watch: paths.appSrc,
432-
});
433-
})(),
414+
new ForkTsCheckerWebpackPlugin({
415+
typescript: resolve.sync('typescript', {
416+
basedir: paths.appNodeModules,
417+
}),
418+
async: false,
419+
checkSyntacticErrors: true,
420+
tsconfig: paths.appTsConfig,
421+
watch: paths.appSrc,
422+
}),
434423
].filter(Boolean),
435424

436425
// Some libraries import Node modules but don't use them in the browser.

packages/react-scripts/config/webpack.config.prod.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent')
2727
const paths = require('./paths');
2828
const getClientEnvironment = require('./env');
2929
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
30+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
3031
// @remove-on-eject-begin
3132
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
3233
// @remove-on-eject-end
@@ -530,27 +531,15 @@ module.exports = {
530531
}),
531532
// TypeScript type checking
532533
fs.existsSync(paths.appTsConfig) &&
533-
(() => {
534-
let ForkTsCheckerWebpackPlugin;
535-
try {
536-
ForkTsCheckerWebpackPlugin = require(resolve.sync(
537-
'fork-ts-checker-webpack-plugin',
538-
{ basedir: paths.appNodeModules }
539-
));
540-
} catch (e) {
541-
// Fail silently.
542-
// Type checking using this plugin is optional.
543-
// The user may decide to install `fork-ts-checker-webpack-plugin` or use `tsc -w`.
544-
return null;
545-
}
546-
547-
return new ForkTsCheckerWebpackPlugin({
548-
async: false,
549-
checkSyntacticErrors: true,
550-
tsconfig: paths.appTsConfig,
551-
watch: paths.appSrc,
552-
});
553-
})(),
534+
new ForkTsCheckerWebpackPlugin({
535+
typescript: resolve.sync('typescript', {
536+
basedir: paths.appNodeModules,
537+
}),
538+
async: false,
539+
checkSyntacticErrors: true,
540+
tsconfig: paths.appTsConfig,
541+
watch: paths.appSrc,
542+
}),
554543
].filter(Boolean),
555544
// Some libraries import Node modules but don't use them in the browser.
556545
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"eslint-plugin-jsx-a11y": "6.1.2",
4444
"eslint-plugin-react": "7.11.1",
4545
"file-loader": "2.0.0",
46+
"fork-ts-checker-webpack-plugin-alt": "0.4.10",
4647
"fs-extra": "7.0.0",
4748
"html-webpack-plugin": "4.0.0-alpha.2",
4849
"identity-obj-proxy": "3.0.0",

0 commit comments

Comments
 (0)