迁移React项目至TypeScript

关于 TypeScript

TypeScript 是 JavaScript 的一个超集,主要提供了类型系统对 ES6 的支持,它由 Microsoft 开发,代码开源于 GitHub 上。html

它的第一个版本发布于 2012 年 10 月,经历了屡次更新后,如今已成为前端社区中不可忽视的力量。TypeScript是一种静态类型语言,增长了代码的可读性和可维护性,应用愈来愈普遍。接下来,咱们来看下如何迁移本身的项目至TypeScript。前端

安装TypeScript

npm install typescript
复制代码

书写配置文件

TypeScript使用tsconfig.json文件管理工程配置,例如你想包含哪些文件和进行哪些检查。 让咱们先建立一个简单的工程配置文件:node

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "strictNullChecks": false,
        "module": "commonjs",
        "target": "ESNext",
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "moduleResolution": "node",
        "allowJs": true
    },
    "include": [
        "./src/**/*"
    ]
}
复制代码

这里咱们为TypeScript设置了一些东西:react

读取全部可识别的src目录下的文件(经过include)。 接受JavaScript作为输入(经过allowJs)。 生成的全部文件放在dist目录下(经过outDir)。 ... 你能够在这里了解更多关于tsconfig.json文件的说明。webpack

建立一个webpack配置文件

在工程根目录下建立一个webpack.config.js文件。git

module.exports = {
    entry: './src/index.tsx',
    output: {
        filename: 'bundle.js',
        path: `${__dirname}/dist`
    },

    // Enable sourcemaps for debugging webpack's output. devtool: "#source-map", resolve: { // Add '.ts' and '.tsx' as resolvable extensions. extensions: ['.js', '.ts', '.tsx'] }, module: { rules: [ // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'. { test: /\.tsx?$/, loader: 'awesome-typescript-loader', options: { useCache: true, // Use internal file cache useBabel: true, // Invoke Babel to transpile files babelCore: '@babel/core' } }, ] } }; 复制代码

这里咱们设置useBabeltrue, 调用babel生成文件。github

修改babel配置文件

安装须要的包web

npm install @babel/preset-typescript
复制代码

将上面安装的包加入工程目录下的babel.config.js文件。typescript

module.exports = {
    presets: ["@babel/preset-typescript", '@babel/preset-react', '@babel/preset-env', 'mobx'],
    plugins: [
        ...
    ]
}
复制代码

准备工做完成。 终于能试试期待已久的TypeScript了,心情好happy 😜 可是,等等,What?为何报错了?express

TS2304: Cannot find name 'If'.
TS2304: Cannot find name 'Choose'.
TS2304: Cannot find name 'When'.
复制代码

原来是咱们在React项目中使用了jsx-control-statements致使的。 怎么办?在线等,挺急的... 😜 咱们发现,这里咱们能够用tsx-control-statements来代替。

配置 tsx-control-statements

安装

npm install tsx-control-statements
复制代码

tsconfig.json文件的files选项中添加

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "strictNullChecks": false,
        "module": "commonjs",
        "target": "ESNext",
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "moduleResolution": "node",
        "allowJs": true
    },
    "files": [
        "./node_modules/tsx-control-statements/index.d.tsx"
    ]
}
复制代码

在webpack的配置文件webpack.config.js中添加

const statements = require('tsx-control-statements').default;

module.exports = {
    ...
    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            {
                test: /\.tsx?$/,
                loader: 'awesome-typescript-loader',
                options: {
                    useCache: true, // Use internal file cache
                    useBabel: true, // Invoke Babel to transpile files
                    babelCore: '@babel/core',
                    // Add tsx-control-statements here
                    getCustomTransformers: () => ({ before: [statements()] })
                }
            },
        ]
    },
    ...
};
复制代码

接下来咱们按照TypeScript官网指南来把咱们的代码改为TypeScript就能够了,这里就不做详细介绍了。

更便利的与ECMAScript模块的互通性

可是这就结束了么,no no no... 在编译过程当中,咱们发现有些包的导入有问题 好比,将i18next做为外部资源引用时(webpackexternals能够帮助咱们实现该方式),咱们发现代码被编译成

i18next_1['default'].t
复制代码

可是i18next_1['default']的值是undefined,执行出错 为何?哪里又双叒叕...有问题了?😭

ECMAScript模块在ES2015里才被标准化,在这以前,JavaScript生态系统里存在几种不一样的模块格式,它们工做方式各有不一样。 当新的标准经过后,社区遇到了一个难题,就是如何在已有的“老式”模块模式之间保证最佳的互通性。

TypeScript与Babel采起了不一样的方案,而且直到如今,还没出现真正地固定标准。 在以前的版本,TypeScript 对 CommonJs/AMD/UMD 模块的处理方式与 ES6 模块不一样,这会致使一些问题:

  • 当导入一个 CommonJs/AMD/UMD 模块时,TypeScript 视 import * as koa from 'koa'const koa = require('koa') 等价,但使用 import * as 建立的模块对象实际上不可被调用以及被实例化。
  • 相似的,当导入一个 CommonJs/AMD/UMD 模块时,TypeScript 视 import koa from 'koa'const koa = require('koa').default 等价,但在大部分 CommonJs/AMD/UMD 模块里,它们并无默认导出。

在 2.7 后的版本里,TypeScript提供了一个新的 esModuleInterop标记,旨在解决上述问题。 当使用这个新的esModuleInterop标记时,可调用的CommonJS模块必须被作为默认导入:

import express from "express";

let app = express();
复制代码

咱们将其加入tsconfig.json文件中

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "strictNullChecks": false,
        "module": "commonjs",
        "target": "ESNext",
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "allowSyntheticDefaultImports": true, // 容许使用 ES2015 默认的 import 风格
        "esModuleInterop": true, // 可调用的CommonJS模块必须被作为默认导入,在已有的“老式”模块模式之间保证最佳的互通性
        "moduleResolution": "node",
        "allowJs": true
    },
    "files": [
        "./node_modules/tsx-control-statements/index.d.tsx"
    ]
}
复制代码

到了这里,咱们的程序终于能完美的运行起来了。 咱们不想再区分哪些须要使用import * as,哪些使用import,所以咱们将格式统一为

import XX from 'XX'
复制代码
相关文章
相关标签/搜索