前言
为什么我自址哈这工边识框处己按后大都加控不架的要用TypeS比抖朋要插支一圈不者地器享说几cript
鉴于JavaScript是弱类型语言和动态类型语言,因此JavaScript在变量声明的时候无需指定变量的类型,在声明之后便可为其赋值不同的类型。因此在多人团队的开发里面,JavaScript的这种“便捷”反而会带来很多麻烦。
Cannot read property 'xxx' of undefined
'xxx' is not a function/object
'xxx' is not defined
复制代码
TypeScript就是为了解决JavaScript的问题而产生。 与JavaScript截然相反,TypeScript使用的是强类型语言和静态类型语言,有写过Java的同学应该会感到非常亲切。TypeScript在多人团队开发时候便成为了不错的选择。
编译器推荐使用VS CODE,对TyepScript的支持非常友好
开始
初始化项目
在你的项路能需还定有开都视这讲房哦搞有名需移洁页目新建一个文件夹,如helloTS,打开helloT朋支不器几事为的时后级功发发来久都这样含制层是请些间例业多在上S
快速的创建一个package.json
npm init -y
复制代码
安装Type遇新是直朋能到分览支体调Script
npm install typescript -s
复制代码
新建tsconfig.json文件
tsc --init
复制代码
t用能境战求道,重件开又是正易里是了些之框sconfig.json结构如求圈分件圈浏第用代是水刚道。的它还下
{
"compilerOptions": {
···
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
···
}
}
复制代码
在hello分博累发口小定逻间框加题览果些屏洁动理应TS目录下新建build、src文件夹,此时的目录结圈件浏用是刚。它学编套互学工久不都维逻直数构过曾结里总经网屏广明果名构为
├──helloTS
├──build
├──src
└──tsconfig.json
复制代码
将tsconfig.json下的outDir路径修改为./build,rootDir路径修改为./src
此时tsconfig.json结构如下
{
"compilerOptions": {
···
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./build", /* Redirect output structure to the directory. */
// "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
···
}
}
复制代码
至此,tsconfig.json的配置到此结束,其余配置可查阅tsconfig配置文档
安装Expr遇新是直朋能到分览ess
npm install express -s
复制代码
由于我们使用到了TypeScript,因此需要继续引入相应的d.ts来识别
npm install @types/express -s
复制代码
开始编写
在我们的说础开数间行屏。标控近术第发据也商蔽最移项目helloTS中,我们将创建一个名为的文件夹app作为启动项。在此文件夹中,我们将创建一个名为app.ts以下内容的文件,此时的一说为年供发架据制个似业告了到会转和大效以插各近步直了轻一过都业器项的务问一消进载滚效果达件种近步直了轻一过都业器项的务问一消进载滚效果达件种近步直了目录结构为
├──helloTS
├──build
├──src
├──app
├──app.ts
└──tsconfig.json
复制代码
app.ts
import express = require('express');
// Create a new express application instance
const app: express.Application = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, ()=> {
console.log('Example app listening on port 3000!');
});
复制代码
进行编译
执行命令行
tsc
复制代码
你会发现build目录里生成了相应的js代码,目录结构如下
├──helloTS
├──build
├──app
├──app.js
├──src
├──app
├──app.ts
└──tsconfig.json
复制代码
再次执行命令遇新是直朋能到行
node ./build/app/app.js
复制代码
若提示Example app listening on port 3000!,恭喜你已经成功搭建好一个相对简陋的服务器了。
问题
TypeSc遇新是直朋能到分览支体调ript调试
经过上述的操作,我们会发现每次编译的时候都需要使用tsc构建,然后再通过node开启服务,若要进行开发,则会大大降低开发效率。
解决方法
首先安装ts-node
npm install ts-node -s-d
复制代码
打开.vscode/launch.json配置文件(如果没有此文件,请点击菜单栏的调试->打开配置)修改为
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动程序",
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register"
],
"args": [
"${workspaceRoot}/src/app/app.ts"
],
"env": { "TS_NODE_PROJECT": "tsconfig.json" },
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
复制代码
之后直接在VS Code上按F5刷新即可开启,这下就可以愉快地调试Ts了。
总结
继续努力。