当前位置:网站首页>01 邂逅typescript,环境搭建
01 邂逅typescript,环境搭建
2022-07-31 15:06:00 【是云呀!】
我们编写的typescript代码最后都会编译成javascript代码来运行,所以需要搭建对应的环境
npm全局安装typescript
npm install typescript -g查看版本
tsc --version编写第一个ts文件

此时直接运行这个ts文件是报错的,我们需要转为js文件,通过我们刚才安装的tsc
// 在控制台输入 tsc 加你的文件
tsc .\01_hello-typescript.ts此时你会发现你的目录下多了一个文件,这个js文件就是转化后的

这样处理ts代码太麻烦,目前有两种方案解决
方案一:搭建webpack环境
方案二:安装ts-node ,ts-node帮我们做两件事,编译,运行在node环境
这里学习阶段先用ts-node
npm install ts-node -g
// ts-node还依赖其他两个包,我们也需要全局安装
npm install tslib @types/node -g
通过ts-node运行ts文件,
![]()
webpack搭建ts环境(可跳过)
这是我的初始目录,创建main.ts util.ts 后 npm init 之后一直回车创建package.json文件

局部安装webpack webpack-cli
npm install webpack webpack-cli -D先在package.json中配置build命令,

在根目录创建index.html(创建本地服务用的模板)用!生成模板后就OK了
之后创建webpack.config.js文件
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "development",
entry: "./src/main.ts",
output: {
path: path.resolve(__dirname, "./dist"),
filename: "bundle.js",
},
resolve: {
extensions: [".ts", ".js", ".json", ".cjs"],
},
devServer: {},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./index.html",
}),
],
};
以下是各种安装,在命令行
// ts-loader依赖typescript
npm install ts-loader typescript -D
// 需要tsconfig.json文件,这里我们自动生成
tsc --init
// 到这里就可以打包了,引用打包后的文件
// 调试麻烦,我们开启本地服务
npm install webpack-dev-server -D
// 这里指定模板
npm install html-webpack-plugin -D设置serve脚本

npm run serve就ok了
边栏推荐
- R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
- Essential Learning for Getting Started with Unity Shader - Transparency Effect
- The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
- R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗
- 安装Xshell并使用其进行Ymodem协议的串口传输
- Small test knife: Go reflection helped me convert Excel to Struct
- 435. 无重叠区间
- 763.划分字母区间——之打开新世界
- 基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
- 网银被盗?这篇文章告诉你如何安全使用网银
猜你喜欢
随机推荐
易驱线主控芯片对比(电动三轮电机90O瓦世纪通达)
Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
Use of el-tooltip
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry
Architecture actual combat battalion module 8 message queue table structure design
OpenShift 4 - 用 Operator 部署 Redis 集群
DBeaver连接MySQL 8.x时Public Key Retrieval is not allowed 错误解决
ES6 类
Getting started with UnityShader (3) - Unity's Shader
「秋招系列」MySQL面试核心25问(附答案)
华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东
梅克尔工作室-第一次
mongo进入报错
工程水文学复习资料
AVH Deployment Practice (1) | Deploying the Flying Paddle Model on Arm Virtual Hardware
R语言ggstatsplot包ggbarstats函数可视化条形图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)、检验结果报告符合APA标准
SIGABRT 报错时的注意事项和解决方法
四象限时间管理有多好用?
【CUDA学习笔记】初识CUDA








