TypeScript support using Babel 7#4837
Conversation
965fb74 to
2a72721
Compare
|
One way to fix image imports TS error: However this doesn't take into account the recent improvements made in CRA 2.0 such as #3718 |
|
@iainbeeston thanks! I added an index.d.ts file fixing We currently only have one |
9f3e2c2 to
0db667c
Compare
06d8ef5 to
f33e0ca
Compare
| @@ -0,0 +1,12 @@ | |||
| declare module '*.jpg' | |||
There was a problem hiding this comment.
I don't think this file should be called index.d.ts as this name will conflict with the automatically generated typings file for index.ts. In my projects I normally call this file externals.d.ts or similar but I don't really mind what it is called.
| declare module '*.png' | ||
|
|
||
| declare module '*.svg' { | ||
| import * as React from 'react' |
There was a problem hiding this comment.
To be consistent with index.js this should be import React from 'react'. This does assume that esModuleInterop: true is specified in tsconfig.json but I think this is best practice for new projects as it means that typescript and babel are more closely aligned.
| declare module '*.svg' { | ||
| import * as React from 'react' | ||
|
|
||
| export const ReactComponent: React.SFC<React.SVGProps<SVGSVGElement>> |
There was a problem hiding this comment.
I'm interested to know why this is needed?
| @@ -1,5 +1,7 @@ | |||
| import 'jest'; | |||
There was a problem hiding this comment.
An alternative to adding this import is to add types: [ 'jest' ] to tsconfig.json
There was a problem hiding this comment.
I don't believe either should be needed. Typescript should pick up the node_modules/@types definitions automatically without any extra effort.
| ```json | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNEXT", |
|
|
|
Just throwing in my two cents as a ts-jest maintainer: Most of the features Babel doesn't support aren't very widely used (we didn't support them for a long time and got very few issues) If you decide to use ts-jest for testing you'll: I'd probably just use Babel |
|
TypeScript PM here. Broadly speaking, I feel that
Anyway, @RyanCavanaugh might have some thoughts here too. |
|
Hi all, I was looking moving over to this from react-scripts-ts, but I have an issue around paths. In my tsconfig I have the following: I.E. aliases with multiple paths. Unfortunately babel-plugin-module-resolver doesn't seem to support arrays as aliases, so I can't get my project working. Not a showstopper for most people, but could cause some issues. |
@DanielRosenwasser do we still have "global namespace" where types added automatically from definition-only files? So we don't need to "virtual import" those types everywhere. |
|
@Serguzest can you give an example of what scenario you have in mind here? |
src/models.ts src/someDirectory/someOtherFile.tsx |
|
Any thoughts when are we going to get this through? |
|
@brunolemos I saw that these items were removed:
Any reasons why? Which one should we use, would tslint make more sense since it's typescript? And last, how to use it with tslint? |
TL/DR, ESLint was not enabled on ts files because it requires typescript-eslint-parser which is still not mature and has a big TSLint built-in support was removed because CRA itself does not lint style preferences, but only critical things like undeclared variable names and typescript already has good defaults for this. Also, the user can run tslint on their own with no problem. (#4837 (comment)) |
|
Great! Now what? Release, shall we? |
|
Huge thanks to @brunolemos for all his effort and of course huge thanks to the team of create react app : ) Can't wait for this to be released : ) |
|
IT'S MERGED 🎉🎉🎉 EDIT: Just tweeted about it 💙 |
|
Amazing stuff! :) Finally :) |
|
Many thanks to @brunolemos for your efforts.. Looking forward for the release :) |
|
I'm so happy this has merged! A future improvement could be strongly typed JSON support by setting this option in |
|
Awesome. Thank you! |
|
I've locked this PR since it's high profile and to keep notification spam low. We'll post updates here when we have them. Please file any concerns as new issues, thanks! |
|
TypeScript is now officially supported as of Create React App 2.1. Read the release notes to get started! |
Adds TypeScript support.
Closes #4146
Closes #2815
The user can just rename
.jsto.tsxand it will work.To enable type checking, the user needs to create a
tsconfig.jsonfile at the root directory and install thetypescript fork-ts-checker-webpack-plugindependencies. It will appear at the same console as the build one. Another option is to install onlytypescriptand runnpx tsc -won a new terminal tab instead.Includes
.tsand.tsxfile extensionsfork-ts-checker-webpack-plugin(opt in)json,bmp,gif,jpeg,jpg,pngandsvgtypescriptoption onreact-appbabel preset to enable/disable@babel/preset-typescript(enabled by default, just likeflow)These items were included, but removed per review suggestion
TSLint support using(TypeScript support using Babel 7 #4837 (comment), TypeScript support using Babel 7 #4837 (comment))fork-ts-checker-webpack-pluginESLint support for TypeScript files using(TypeScript support using Babel 7 #4837 (comment))typescript-eslint-parserSupport(TypeScript support using Babel 7 #4837 (comment))tsconfig.prod.jsonandtslint.prod.jsonAutomatically enableflowortypescriptpresets by detecting.flowconfigandtsconfig.jsonPrevent enabling bothflowandtypescriptin the same projectPending suggestions (help wanted!)
Fix build test not passing even though it's correct (Tests are using code from npm instead of code from pull request #5440)Use(it's ok for this first iteration)async: trueon type checking? (would need to fix errors not showing up)Make sure ESLint works great on tsx filesMake sure there are no conflicts between typescript preset and flow plugin because both are being enabled together by default(none were found or reported by now)tsconfig.jsonwhen typescript is imported for the first time?tsconfig.jsonoptions likeisolatedModules: true?Remove typescript dependency? (will probably need to change something onfork-ts-checker-webpack-plugin, because it isn't working without it)Enable type checking on test files using ts-jest? (TypeScript support using Babel 7 #4837 (comment))(no)Screenshot
How to try this PR while it's not merged
git clone git@github.com:brunolemos/create-react-app.gitcd create-react-appgit checkout next-typescriptyarncd packages/react-scripts/yarn linkcd ~/your/projects/foldernpx create-react-app app-namecd app-nameyarn link react-scriptsyarn start