04 # 第一个 TypeScript 程序

初始化项目以及安装依赖

新建 ts_in_action 文件夾

npm init -y

安装好 typescript,就可以执行下面命令查看帮助信息

npm i typescript -g
tsc -h

创建配置文件,执行下面命令就会生成一个 tsconfig.json 文件

tsc --init

使用 tsc 编译一个 js 文件

新建 src/index.ts

let kaimo:string = "hello typescript"

执行下面命令编译该 ts 文件

tsc ./src/index.ts

编译结果如下:

var kaimo = "hello typescript";

也可以使用 https://www.typescriptlang.org/play 查看

配置构建工具 webpack 环境

安装依赖

npm i webpack@4.35.2 webpack-cli@3.3.5 webpack-dev-server@3.7.2 -D
npm i ts-loader@6.0.4 typescript@3.5.2 -D
npm i html-webpack-plugin@3.2.0 clean-webpack-plugin@3.0.0 webpack-merge@4.2.1 -D

配置相应的环境

基础配置:

// 公共环境配置

// 下面这行用于 vscode 中智能化自动提示 webpack 配置项
/** @type {import('webpack').Configuration} */

const HtmlWebpackPlugin = require("html-webpack-plugin");

const config = {
   
    entry: "./src/index.ts",
    output: {
   
        filename: "app.js"
    },
    resolve: {
   
        extensions: [".js", ".ts", ".tsx"]
    },
    module: {
   
        rules: [
            {
   
                test: /\.tsx?$/i,
                use: [
                    {
   
                        loader: "ts-loader"
                    }
                ],
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
   
            template: "./src/tpl/index.html"
        })
    ]
};
module.exports = config;

开发配置:

// 开发环境配置

// 下面这行用于 vscode 中智能化自动提示 webpack 配置项
/** @type {import('webpack').Configuration} */

const config = {
   
    devtool: "cheap-module-eval-source-map"
};

module.exports = config;

生产配置:

// 生产环境配置

// 下面这行用于 vscode 中智能化自动提示 webpack 配置项
/** @type {import('webpack').Configuration} */

const CleanWebpackPlugin = require("clean-webpack-plugin");

const config = {
   
    plugins: [new CleanWebpackPlugin()]
};

module.exports = config;

webpack 文件入口配置:

// webpack 文件入口

// 下面这行用于 vscode 中智能化自动提示 webpack 配置项
/** @type {import('webpack').Configuration} */

const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.config");
const devConfig = require("./webpack.dev.config");
const proConfig = require("./webpack.pro.config");

const config = merge(baseConfig, process.NODE_ENV === "development" ? devConfig : proConfig);

module.exports = config;

package.json 脚本配置

"scripts": {
   
    "start": "webpack-dev-server --mode=development --config ./build/webpack.config.js",
    "build": "webpack --mode=production --config ./build/webpack.config.js"
},

启动服务并且打包测试

添加模板

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>TypeScript in Action</title>
    </head>
    <body>
        <div class="app"></div>
    </body>
</html>

添加功能

let kaimo:string = "hello typescript";

document.querySelectorAll(".app")[0].innerHTML = kaimo;

启动服务 npm run start,访问 http://localhost:8080/

在这里插入图片描述
在这里插入图片描述

打包 npm run build

在这里插入图片描述

相关推荐

  1. 04 开发第一组件

    2023-12-05 17:40:08       36 阅读
  2. 第一Rust程序

    2023-12-05 17:40:08       12 阅读
  3. 开发第一SpringBoot程序

    2023-12-05 17:40:08       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 17:40:08       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 17:40:08       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 17:40:08       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 17:40:08       18 阅读

热门阅读

  1. webpack的plugin和loader的区别

    2023-12-05 17:40:08       35 阅读
  2. ubuntu22.04更新内核后无wifi选项

    2023-12-05 17:40:08       41 阅读
  3. 【开发规范】前端开发中引用文件的方式

    2023-12-05 17:40:08       39 阅读
  4. 前端开启gzip优化页面加载速度

    2023-12-05 17:40:08       32 阅读
  5. 前端学习笔记

    2023-12-05 17:40:08       41 阅读
  6. MacBook续命,XCode硬盘占用问题

    2023-12-05 17:40:08       43 阅读
  7. base64转PDF

    2023-12-05 17:40:08       38 阅读
  8. Flutter, pub 无法安装依赖 等问题

    2023-12-05 17:40:08       36 阅读
  9. Redis 集群搭建 哨兵模式搭建

    2023-12-05 17:40:08       35 阅读
  10. netstat

    netstat

    2023-12-05 17:40:08      38 阅读