tsconfig.json

這是一個最小的 tsconfig,可以幫助你啟動和執行。

{
    "include": [
        "src/*"
    ],
    "compilerOptions": {
        "target": "es5",
        "jsx": "react",
        "allowSyntheticDefaultImports": true
    }
}

讓我們逐個瀏覽一下這些屬性:

include

這是一個原始碼陣列。這裡我們只有一個條目 src/*,它指定 src 目錄中的所有內容都包含在編譯中。

compilerOptions.target

指定我們要編譯為 ES5 目標

compilerOptions.jsx

將此設定為 true 將使 TypeScript 自動將你的 tsx 語法從 <div /> 編譯為 React.createElement("div")

compilerOptions.allowSyntheticDefaultImports

方便的屬性,允許你匯入節點模組,就像它們是 ES6 模組一樣,所以不要這樣做

import * as React from 'react'
const { Component } = React

你可以這樣做

import React, { Component } from 'react'

沒有任何錯誤告訴你 React 沒有預設匯出。