yarn add --dev \
@babel/core \
@babel/cli \
@babel/preset-env \
@babel/preset-typescript \
typescript
Configure babel.config.js
to add preset-typescript
in there.
module.exports = {
presets: ['@babel/preset-typescript', '@babel/preset-env']
}
Create tsconfig.json
.
{
"compilerOptions": {
// Target latest version of ECMAScript.
"target": "esnext",
// Search under node_modules for non-relative imports.
"moduleResolution": "node",
// Process & infer types from .js files.
"allowJs": true,
// Don't emit; allow Babel to transform files.
"noEmit": true,
// Enable strictest settings like strictNullChecks & noImplicitAny.
"strict": true,
// Import non-ES modules as default imports.
"esModuleInterop": true,
"jsx": "react"
},
"include": [
"src" // <-- change this to where your source files are
]
}
Add the tsc
script to your package.json. Configure your CI to run yarn run tsc
for checks.
"scripts": {
"watch": "babel --watch src --out-dir lib --extensions .ts,.tsx",
"build": "babel src --out-dir lib --extensions .ts,.tsx",
"tsc": "tsc"
}
This will run type checks.
yarn run tsc
Could you please fix code example in "Add
tsc
script` block?Instead of
---out-dir
should be--out-dir
(two dashes).Thank you.
Update for Windows users:
Also, it it required to remove quotes around extensions -
'.ts,.tsx'
->.ts,.tsx