构建纯TypeScript应用
构建纯TypeScript应用
现在只有命令行应用的例子。
前言
现在,应用开发的趋势是命令行接口应用和Web应用。
node.js 和 typescript的崛起所以,这里讨论如何创建纯的TypeScript CLI(Command Line Interface)应用和Web server-side应用。
概念
环境准备
- 安装node.js
从node.js网站上下载,并安装。
这时,npm也一并被安装了。
:: check version
node -v
npm -v
- 更新npm
node.js带的npm不一定是最新的,所以要更新npm
:: Update npm to the latest version
npm install npm@latest -g
npm -v
- 安装typescript
:: install typescript
npm install -g typescript
:: check version
tsc -v
构建typescript CLI应用
- 创建一个新的项目
mkdir myproj
cd myproj
npm init
这个命令会在当前目录创建一个项目配置文件:package.json
内容类似如下:
{
"name": "myproj",
"version": "1.0.0",
"description": "",
"main": "App.ts",
"scripts": {
"test": "ts-node .\\src\\App.ts name address"
},
"keywords": [
"my",
"example"
],
"author": "your name",
"license": "MIT",
}
- 下一个App.ts代码
创建目录src
在目录src下,创建一个文件App.ts
class Startup {
public static main(): number {
var args = process.argv;
args.splice(0, 2);
console.log(args);
return 0;
}
}
Startup.main();
- 运行App.ts
运行目录: myproj
ts-node .\src\App.ts name address
:: or run script 'test' in the package.json
npm run test
- 设置项目的依赖
运行目录: myproj
npm install @types/node --save-dev
这时,npm会下载@types/node到./node_modules下,package.json文件会增加下面内容:
{
"devDependencies": {
"@types/node": "^8.0.33"
}
}
构建typescript Web server-side应用
好吧。我还不会。
Type Script 的文件
.ts
TypeScript源码文件。.tsx
主要是支持React的jsx文件,是一种可以嵌入XML-like的TypeScript源文件。.d.ts
TypeScript源码的声明文件,一般是可以自动生成的。
有些像是C++的.h文件。我想一个作用是方便参照使用,性能更好一些。tsconfig.json
tsconfig.json是Type Script项目的配置文件。
了解tsconfig.json
tsconfig.json是Type Script项目的配置文件。主要包含如下信息:
编译器选项 - compilerOptions
在下一小节里,专门介绍主要的编译器选项。项目的文件 - files/include/exclude
基本上可以使用下面这个统一形式:
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"../**/*.spec.ts"
]
}
extends
指定一个基本配置文件。
详见tsconfig-jsoncompileOnSave 是否自动编译
编译器选项 - tsconfig.json - compilerOptions
@types, typeRoots, types
默认,编译器会从本地的node_modules/@types找types的声明文件。
所以,一般不要含有typeRoots和types设定(空的也不行)。
如果有typeRoots设定,编译器将忽略node_modules/@types
如果有types设定,编译器将只找types定义的声明文件包。Pure Type Script 编译器选项
"compilerOptions": {
"emitDecoratorMetadata": true, // for typeorm
"experimentalDecorators": true, // for typeorm
"lib": ["esnext"],
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"target": "esnext",
"module": "commonjs", // work with node.js only
"moduleResolution": "node"
}
- lib
- target/module/moduleResolution
了解一下npm
npm提供了丰富的功能,可以做很多事情,包括:
manage packages
install/uninstall/update/docs/doctor/star/unstarmanage a project
/init/install/uninstall/updaterun a project
build/rebuild/run/scripts/start/test/restart/stoppublish a project
pack/registry/publish/unpublish/deprecate/adduser/owneroptions
config/root/prefix/bin
npm folders and files
Prefix folder
%AppData%\npmGlobal install path
%AppData%\npm\node_modulesGlobal cache
%AppData%\npm-cachePackage install path
.\node_modulesnpmrc
npm 配置文件。四个Level:
per-project config file (/path/to/my/project/.npmrc)
per-user config file (~/.npmrc)
global config file ($PREFIX/etc/npmrc)
npm builtin config file (/path/to/npm/npmrc)package.json
项目配置文件package-lock.json
自动生成的配置文件,不会被发布。
精确描述依赖包,辅助安装、发布功能。npm-shrinkwrap.json
由npm shrinkwrap命令产生,完完全全和package-lock.json内容一样,但是会被发布。
npm 命令
- init
:: 问一系列问题,产生一个package.json
npm init [-f|--force|-y|--yes]
init的参数:
-f|--force|-y|--yes: 没有提示,产生一个默认的package.json文件。
- install
:: 安装当前项目的依赖包.
npm install (with no args, in package dir)
:: 安装包
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
:: 安装一个online的包
npm install <git-host>:<git-user>/<repo-name>
:: 安装一个online的包
npm install <git repo url>
:: 安装一个已下载的包
npm install <tarball file>
:: 安装一个online的包
npm install <tarball url>
:: 安装一个已经下载并解开(或者是没有打包)的包,<folder>是包的位置。
npm install <folder>
install的别名: npm i
install的参数:
-P|--save-prod: 默认,并保存到项目的dependencies配置中。
-D|--save-dev: 并保存到项目的devDependencies配置中。
-O|--save-optional: 并保存到项目的optionalDependencies配置中。
-E|--save-exact: 使用精确的版本信息保存
-B|--save-bundle: 并保存到项目的bundleDependencies配置中。
--no-save: 不保存到项目配置中。
-g|--global: 安装包到global位置。
-f|--force: 强制重新安装。
- run|run-script
npm run-script <command> [--silent] [-- <args>...]
:: or alias
npm run <command> [--silent] [-- <args>...]
其它工具
参照Integrating with Build Tools
- gulp: 构建系统
- Karma: JavaScript的测试工具
参照
构建纯TypeScript应用的更多相关文章
- 【react】使用 create-react-app 构建基于TypeScript的React前端架构----上
写在前面 一直在探寻,那优雅的美:一直在探寻,那精湛的技巧:一直在探寻,那简单又直白,优雅而美丽的代码. ------ 但是在JavaScript的动态类型.有时尴尬的自动类型转换,以及 “0 == ...
- vue cli4构建基于typescript的vue组件并发布到npm
基于vue cli创建一个vue项目 首先安装最新的vue cli脚手架, npm install --global @vue/cli npm WARN optional SKIPPING OPTIO ...
- 如何开发Vite3插件构建Electron开发环境
新用户购买<Electron + Vue 3 桌面应用开发>,加小册专属微信群,参与群抽奖,送<深入浅出Electron>.<Electron实战>作者签名版. 1 ...
- Nodejs in Visual Studio Code 12.构建单页应用Scrat实践
1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博主应该是far away ...
- 动手实现 Redux(三):纯函数(Pure Function)简介
我们接下来会继续优化我们的 createStore 的模式,让它使我们的应用程序获得更好的性能. 但在开始之前,我们先用一节的课程来介绍一下一个函数式编程里面非常重要的概念 —— 纯函数(Pure F ...
- EdgeFormer: 向视觉 Transformer 学习,构建一个比 MobileViT 更好更快的卷积网络
前言 本文主要探究了轻量模型的设计.通过使用 Vision Transformer 的优势来改进卷积网络,从而获得更好的性能. 欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结.最新技术跟 ...
- Blazor VS 传统Web应用程序
原文作者: Christian Findlay 原文链接: https://christianfindlay.com/2020/07/09/blazor-vs-traditional-web-apps ...
- Jser 设计模式系列之面向对象 - 接口封装与继承
GOF在<设计模式>中说到:面向接口编程,而非面向实现编程 鉴于此,这个概念可见一斑! JS却不像其他面向对象的高级语言(C#,Java,C++等)拥有内建的接口机制,以确定一组对象和另一 ...
- 加速编码的 JavaScript 库和工具
JavaScript库是 一个提前写好的JavaScript文件库,它可以很容易的开发基于JavaScript的应用,特别是AJAX和一些其它的以web为中心的技术.运用JavaScript最基本的方 ...
随机推荐
- c 语言常量
1,整数常量 整数常量可以是十进制.八进制或十六进制的常量.前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,不带前缀则默认表示十进制. 整数常量也可以带一个后缀,后缀是 U 和 L 的组合 ...
- GIT的安装及命令使用
http://blog.jobbole.com/78960/ 因此:多人协作工作模式一般是这样的: 首先,可以试图用git push origin branch-name推送自己的修改. 如果推送失败 ...
- Docker学习笔记 - Docker的容器
docker logs [-f] [-t] [--tail] 容器名 -f -t --tail="all" 无参数:返回所有日志 -f 一直跟踪变化并返回 -t 带时间戳返 ...
- DevExpress控件的一些快捷操作
用的DevExpress控件时,有一些操作并不太方便,根据我自己需要的封装了一些控件的事件,调用的时候直接绑定控件的事件就可以了 例如: this.ComboBoxEdit.KeyDown += Ct ...
- 如何解释vue的生命周期才能令面试官满意?
当面试官问:"谈谈你对vue的生命周期的理解",听到这句话你是不是心里暗自窃喜:这也太容易了吧,不就是beforeCreate.created.beforeMount.mounte ...
- 解决将/etc/passwd文件中1000改为0后只能guest进入系统的问题 ||ubuntu下将普通用户权限升级为root用户权限的方法;
其实我现在才知道linux系统对于用户权限管理比较严,在ubuntu下系统不允许root权限的用户进入图像界面系统.由于之前没弄过权限这个东西瞬间掉坑了了. 我是想修改一下root下的nginx.co ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] 01 Matrix 零一矩阵
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...
- [LeetCode] Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】
原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...