Skip to content

Commit b6796da

Browse files
Timernate770
authored andcommitted
Always type check TypeScript when being used (facebook#5515)
* Always type check TypeScript when being used * Use alternate version
1 parent bf47086 commit b6796da

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
@@ -404,27 +405,15 @@ module.exports = {
404405
}),
405406
// TypeScript type checking
406407
fs.existsSync(paths.appTsConfig) &&
407-
(() => {
408-
let ForkTsCheckerWebpackPlugin;
409-
try {
410-
ForkTsCheckerWebpackPlugin = require(resolve.sync(
411-
'fork-ts-checker-webpack-plugin',
412-
{ basedir: paths.appNodeModules }
413-
));
414-
} catch (e) {
415-
// Fail silently.
416-
// Type checking using this plugin is optional.
417-
// The user may decide to install `fork-ts-checker-webpack-plugin` or use `tsc -w`.
418-
return null;
419-
}
420-
421-
return new ForkTsCheckerWebpackPlugin({
422-
async: false,
423-
checkSyntacticErrors: true,
424-
tsconfig: paths.appTsConfig,
425-
watch: paths.appSrc,
426-
});
427-
})(),
408+
new ForkTsCheckerWebpackPlugin({
409+
typescript: resolve.sync('typescript', {
410+
basedir: paths.appNodeModules,
411+
}),
412+
async: false,
413+
checkSyntacticErrors: true,
414+
tsconfig: paths.appTsConfig,
415+
watch: paths.appSrc,
416+
}),
428417
].filter(Boolean),
429418

430419
// 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
@@ -524,27 +525,15 @@ module.exports = {
524525
}),
525526
// TypeScript type checking
526527
fs.existsSync(paths.appTsConfig) &&
527-
(() => {
528-
let ForkTsCheckerWebpackPlugin;
529-
try {
530-
ForkTsCheckerWebpackPlugin = require(resolve.sync(
531-
'fork-ts-checker-webpack-plugin',
532-
{ basedir: paths.appNodeModules }
533-
));
534-
} catch (e) {
535-
// Fail silently.
536-
// Type checking using this plugin is optional.
537-
// The user may decide to install `fork-ts-checker-webpack-plugin` or use `tsc -w`.
538-
return null;
539-
}
540-
541-
return new ForkTsCheckerWebpackPlugin({
542-
async: false,
543-
checkSyntacticErrors: true,
544-
tsconfig: paths.appTsConfig,
545-
watch: paths.appSrc,
546-
});
547-
})(),
528+
new ForkTsCheckerWebpackPlugin({
529+
typescript: resolve.sync('typescript', {
530+
basedir: paths.appNodeModules,
531+
}),
532+
async: false,
533+
checkSyntacticErrors: true,
534+
tsconfig: paths.appTsConfig,
535+
watch: paths.appSrc,
536+
}),
548537
].filter(Boolean),
549538
// Some libraries import Node modules but don't use them in the browser.
550539
// 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)