question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Zero-config TypeScript with Babel 7

See original GitHub issue

Zero-config TypeScript with Babel 7

As mentioned in several other issues, with the release of Babel 7 and CRA 2, TypeScript support is now possible in a way that does not dramatically change the core of CRA internals. Before proceeding with a PR, would love to gather feedback on a solution / implementation I came up with over the weekend:

Current Behavior

TypeScript users cannot use CRA directly. Popular alternatives include react-scripts-ts (a fork of CRA that uses the TypeScript compiler) and Microsoft’s React TypeScript boilerplate.

Desired Behavior

CRA works with TypeScript via Babel 7 with zero-config. To enable TypeScript, users can just rename src/index.js to src/index.tsx.

Suggested Solution

  • Remove filename extension from paths.appIndexJs (i.e. src/index.js -> src/index) so it works regardless of .js or .ts or .tsx.
  • Add .ts and .tsx to the extensions in webpack config (resolve, babel loader, file-loader) and Jest config
  • Add a useTypescript flag (so that this choice is explicit) and related @babel/preset-typescript package to babel-preset-react-app
  • Use the filename extension of paths.appIndexJs to determine whether to set the useTypeScript flag in webpack babel-loader options and jest babel transformation
  • Add documentation about TypeScript usage

Areas of Interest/Discussion/Questions

  • Alternative ways to determine whether to enable TS?
  • Should tsconfig.json and tslint.json be zero config? (probably not)
  • Should we add built-in type checking with fork-ts-webpack-plugin? (my vote is against this)
  • (bonus) Add tslint-loader to Webpack config if in “TS mode”
    • Question: can we use eslint-loader with Babel 7 TS? (I’m not sure)
    • If not, we should add a minimal tslint-config-react-app package that is congruent with eslint-config-react-app

Suggested Documentation

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Check out this introduction to using static types in JavaScript if you are new to this concept.

Recent versions of TypeScript work with Create React App projects out of the box thanks to Babel 7. Babel 7 TypeScript does not allow some features of TypeScript such as constant enum and namespaces. You can read more about Babel TypeScript here.

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

  • Runnpm install --save typescript @types/react @types/react-dom (or yarn add typescript @types/react @types/react-dom).
  • Add "type-check": "tsc" to the scripts section of your package.json.
  • Add a "resolutions" section to your package.json to avoid conflicting types of React and ReactDOM in your dependencies:
"resolutions": {
    "**/@types/react": "16.0.40",
    "**/@types/react-dom": "16.0.4"
  },
  • Create a file called tsconfig.json in the root of your project:
{
  "compilerOptions": {
    "target": "esnext",                     /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    "module": "commonjs",                   /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "jsx": "react",                          /* Use React to interpret JSX */
    "noEmit": true,                         /* Do not emit outputs. */
    "allowSyntheticDefaultImports": true    /* Allow default imports from modules with no default export. 
  }
}
  • Rename src/index.js to src/index.tsx

Now you can run npm run type-check (or yarn type-check) to check the files for type errors. You can optionally use an IDE like Nuclide or VSCode for a better integrated experience.

To learn more about TypeScript, check out its documentation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:192
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

28reactions
brunolemoscommented, Sep 29, 2018

I made the PR #4837 with TypeScript support using babel 7. Pretty similar to what was proposed here.

16reactions
jaredpalmercommented, Apr 6, 2018

@evertbouw I disagree. Furthermore as described above, this setup is almost identical to the effort / config required to setup Flow + plus changing filename.

Read more comments on GitHub >

github_iconTop Results From Across the Web

7.9.0 Released: Smaller preset-env output, Typescript 3.8 ...
We are excited to announce that its functionality has now been merged into @babel/preset-env ! This means that its optimizations can be applied ......
Read more >
Dependencies | beidou-example-with-react-loadable | npm
arrow_right babel‑eslint. 8.2.6 Notes Relation Licenses Dep... Version 8.2.6 Published Description arrow_right babel‑plugin‑dynamic‑import‑node. 1.2.0 Notes Relation Licenses Dep... Version 1.2.0 Published Description
Read more >
v2 Addon Format (Embroider Compatibility) - Ember RFCs
babel.majorVersion : the version of babel the app's settings were written for. Only 6 and 7 are supported at this time. Unlike addons, ......
Read more >
Guillermo Rauch on Twitter: "@dan_abramov @danm_c137 ...
Typescript by default already? ... RFC: Zero-config TypeScript with Babel 7 · Issue #4146 · facebook/create-react-app. Zero-config TypeScript with Babel 7 ...
Read more >
Parcel: an Easier Build Process - Matt Burgess
The real key difference is a zero config development experience. ... Among other tools, Parcel will parse and compile TypeScript, Elm, Less, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found