Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Latest commit

 

History

History
59 lines (44 loc) · 1.54 KB

typescript.md

File metadata and controls

59 lines (44 loc) · 1.54 KB

React with TypeScript example

Install dependencies

npm install typescript

Install required type declarations, for example for the default React setup:

npm install @types/react @types/react-dom @types/react-helmet @types/styled-jsx

Create a tsconfig.json file in your project root, as required by your project. For example:

{
    "compilerOptions": {
        "target": "es5",
        "strict": true,
        "esModuleInterop": true,
        "jsx": "react"
    }
}

Important: Parcel 1.x uses tsc compiler for compiling TypeScript files, which means that Babel plugins such as styled-jsx do not work out of the box (source). Parcel 2.x will allow optional TypeScript compiling via Babel 7+ using @babel/preset-typescript (source), but for now a 3d party Parcel plugin can be used.

To use Babel plugins install the required dependencies:

npm install parcel-plugin-babel-typescript @babel/preset-typescript

Update your .babelrc presets afterwards:

{
    "presets": [
        "@babel/preset-typescript"
    ],
    "plugins": [
        "styled-jsx/babel"
    ]
}

Make sure that your tsconfig.json is configured to leave JSX transformations to Babel:

{
    "compilerOptions": {
        "jsx": "preserve"
    }
}