Skip to content

Commit 71946b1

Browse files
kylebebakmrmckeb
authored andcommitted
Add TSC_COMPILE_ON_ERROR setting (#6931)
1 parent 6f7b371 commit 71946b1

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

docusaurus/docs/adding-typescript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ yarn add typescript @types/node @types/react @types/react-dom @types/jest
3131

3232
Next, rename any file to be a TypeScript file (e.g. `src/index.js` to `src/index.tsx`) and **restart your development server**!
3333

34-
Type errors will show up in the same console as the build one.
34+
Type errors will show up in the same console as the build one. You'll have to fix these type errors before you continue development or build your project. For advanced configuration, [see here](advanced-configuration.md).
3535

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

docusaurus/docs/advanced-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ You can adjust various development and production settings by setting environmen
2323
| INLINE_RUNTIME_CHUNK | 🚫 Ignored | ✅ Used | By default, Create React App will embed the runtime script into `index.html` during the production build. When set to `false`, the script will not be embedded and will be imported as usual. This is normally required when dealing with CSP. |
2424
| IMAGE_INLINE_SIZE_LIMIT | 🚫 Ignored | ✅ Used | By default, images smaller than 10,000 bytes are encoded as a data URI in base64 and inlined in the CSS or JS build artifact. Set this to control the size limit in bytes. Setting it to 0 will disable the inlining of images. |
2525
| EXTEND_ESLINT | ✅ Used | ✅ Used | When set to `true`, ESLint configs that extend `eslint-config-react-app` will be used by `eslint-loader`. Any rules that are set to `"error"` will stop the application from building. |
26+
| TSC_COMPILE_ON_ERROR | ✅ Used | ✅ Used | When set to `true`, you can run and properly build TypeScript projects even if there are TypeScript type check errors. These errors are printed as warnings in the terminal and/or browser console. |

packages/react-dev-utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ The `args` object accepts a number of properties:
337337
- **urls** `Object`: To provide the `urls` argument, use `prepareUrls()` described below.
338338
- **useYarn** `boolean`: If `true`, yarn instructions will be emitted in the terminal instead of npm.
339339
- **useTypeScript** `boolean`: If `true`, TypeScript type checking will be enabled. Be sure to provide the `devSocket` argument above if this is set to `true`.
340+
- **tscCompileOnError** `boolean`: If `true`, errors in TypeScript type checking will not prevent start script from running app, and will not cause build script to exit unsuccessfully. Also downgrades all TypeScript type checking error messages to warning messages.
340341
- **webpack** `function`: A reference to the webpack constructor.
341342

342343
##### `prepareProxy(proxySetting: string, appPublicFolder: string): Object`

packages/react-dev-utils/WebpackDevServerUtils.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function createCompiler({
108108
urls,
109109
useYarn,
110110
useTypeScript,
111+
tscCompileOnError,
111112
webpack,
112113
}) {
113114
// "Compiler" is a low-level interface to Webpack.
@@ -190,16 +191,28 @@ function createCompiler({
190191

191192
const messages = await tsMessagesPromise;
192193
clearTimeout(delayedMsg);
193-
statsData.errors.push(...messages.errors);
194+
if (tscCompileOnError) {
195+
statsData.warnings.push(...messages.errors);
196+
} else {
197+
statsData.errors.push(...messages.errors);
198+
}
194199
statsData.warnings.push(...messages.warnings);
195200

196201
// Push errors and warnings into compilation result
197202
// to show them after page refresh triggered by user.
198-
stats.compilation.errors.push(...messages.errors);
203+
if (tscCompileOnError) {
204+
stats.compilation.warnings.push(...messages.errors);
205+
} else {
206+
stats.compilation.errors.push(...messages.errors);
207+
}
199208
stats.compilation.warnings.push(...messages.warnings);
200209

201210
if (messages.errors.length > 0) {
202-
devSocket.errors(messages.errors);
211+
if (tscCompileOnError) {
212+
devSocket.warnings(messages.errors);
213+
} else {
214+
devSocket.errors(messages.errors);
215+
}
203216
} else if (messages.warnings.length > 0) {
204217
devSocket.warnings(messages.warnings);
205218
}

packages/react-scripts/scripts/build.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,17 @@ checkBrowsers(paths.appPath, isInteractive)
122122
);
123123
},
124124
err => {
125-
console.log(chalk.red('Failed to compile.\n'));
126-
printBuildError(err);
127-
process.exit(1);
125+
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
126+
if (tscCompileOnError) {
127+
console.log(chalk.yellow(
128+
'Compiled with the following type errors (you may want to check these before deploying your app):\n'
129+
));
130+
printBuildError(err);
131+
} else {
132+
console.log(chalk.red('Failed to compile.\n'));
133+
printBuildError(err);
134+
process.exit(1);
135+
}
128136
}
129137
)
130138
.catch(err => {

packages/react-scripts/scripts/start.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ checkBrowsers(paths.appPath, isInteractive)
9595
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
9696
const appName = require(paths.appPackageJson).name;
9797
const useTypeScript = fs.existsSync(paths.appTsConfig);
98+
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
9899
const urls = prepareUrls(protocol, HOST, port);
99100
const devSocket = {
100101
warnings: warnings =>
@@ -110,6 +111,7 @@ checkBrowsers(paths.appPath, isInteractive)
110111
urls,
111112
useYarn,
112113
useTypeScript,
114+
tscCompileOnError,
113115
webpack,
114116
});
115117
// Load proxy config

0 commit comments

Comments
 (0)