If you have a codebase written in Flow but want to transition to TypeScript, it would be useful for Create React App to support doing this incrementally without additional config.
At the moment, when following the 'Adding Flow' instructions followed by the 'Adding TypeScript' instructions instructions, you get an error such as:
'import type' declarations can only be used in TypeScript files. TS8006
1 | // @flow
2 | import React from 'react';
> 3 | import type { Node } from 'react';
Example steps to recreate in the first 3 commits here:
https://github.com/penx/cra-flow-ts/commits/master
If you follow the 'Adding TypeScript' instructions and add TypeScript to a project that already uses Flow, it should compile both Flow and TypeScript files correctly.
This is possible by manually editing .tsconfig after it is generated, as per @tebuevd's comment below. However, Create React App should detect the presence of flow (either flow-bin being in dependencies or presence of a .flowconfig file) and setup .tsconfig to have "include": ["src/**/*.ts", "src/**/*.tsx"] and "allowJs": false when it is generated.
This actually already works. Here is a tsconfig.js file my team is using to transition away from Flow incrementally:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
The important part to make it work is the the include line 馃槉
@tebuevd Thanks! I've updated the ticket accordingly. The allowJs: false also seems important in some cases, maybe when TypeScript files import JavaScript files.
As create-react-app complains if there's both a tsconfig.json and a jsconfig.json - would the settings above still respect what you have in tsconfig.json when transpiling JS files?
Sorry, I'm not familiar with jsconfig.json. We don't use it in our project.
Most helpful comment
This actually already works. Here is a
tsconfig.jsfile my team is using to transition away from Flow incrementally:The important part to make it work is the the
includeline 馃槉