一个待办任务债台高筑的夜晚,偷懒和家人共处了一小段家庭时间之后,还是不愿意开始消灭TODO LIST。往往这个时候会掏出手机打发一下时间,见微信群中有发 https://dev.to/ 是个不错的网站。索性用自己碎片时间翻译一些文章。这篇文章作者是:Jeremy Likness 微软的工程师,他想写一篇如何用Typescript开发Node.js,文章比较简单,作者给出来一个小视频来讲解。通过这篇文章,可以快速了解快速上手Typescript开发Node.js


    19-11-7_14-52-17.638_51575.jpeg

    几年前,我应邀参加了一次分享,分享主题是帮助Node.js 开发人员通过拥抱TypeScript 来更好的增强Javascript 所带来的益处。非常幸运的是其中有一位听众David Neal,他是位才华横溢的艺术家,会议期间创作了一幅草图来总结我的分享。

    DKRHs8FUQAAv_vV.jpg

    推文见: David Neal 🥓🥑 的 Twitter: “TypeScript for Node.js @jeremylikness #visualnotes #connecttech17… ”
    完整的分享文稿和代码参见:GitHub - JeremyLikness/typescript-for-node: Presentation materials for Connect.Tech 2017. Covers building Node.js apps with TypeScript.

    我录了以视频来阐述这些概念。 在不到十分钟的时间内,我来演示下如何从零开始用TypeScript构建一个Node.js 应用。 看吧,也让我了解下你的想法。

    简短的记录下视频教程

    初始化项目

    1. mkdir MakeThings
    2. cd MakeThings
    3. npm init -y

    安装依赖

    1. npm i typescript @types/node -D

    初始化tsconfig.json

    1. node node_modules/typescript/lib/tsc --init

    创建common.ts

    1. mkdir src && touch src/common.ts

    common.ts

    1. export interface INamed {
    2. name: string
    3. }

    创建animal.ts

    1. touch src/animal.ts

    common.ts

    1. import { INamed } from ‘./common
    2. export class Animal implements INamed {
    3. constructor(public name: string) {}
    4. }
    5. export class Animals extends Array<Animal> {
    6. constructor() {
    7. super()
    8. ;[‘Lynx’, Jaguar’, Panther’, Leopard’, Tiger’, Lion’].forEach(name => {
    9. this.push(new Animal(name))
    10. })
    11. }
    12. }

    修改tsconfig.json 只罗列出了修改部分

    1. {
    2. compilerOptions”: {
    3. declaration”: true,
    4. sourceMap”: true,
    5. outDir”: “./lib
    6. rootDir”: “./src
    7. }
    8. }

    修改package.json

    1. scripts”: {
    2. test”: echo \”Error: no test specified\” && exit 1”,
    3. build”: tsc
    4. },

    尝试执行

    1. npm run build

    这时候会把ts代码 打到 lib目录下

    EFFF25FE-B52A-4130-9EC1-E9D53154193E.png

    在src 继续添加一些代码
    color.ts

    1. import { INamed } from ‘./common
    2. export class Color implements INamed {
    3. constructor(public name: string) {}
    4. }
    5. export class Colors extends Array<Color> {
    6. constructor() {
    7. super()
    8. ;[‘Red’, Orange’, Yellow’, Green’, Blue’, Indigo’, Violet’].forEach(
    9. color => this.push(new Color(color))
    10. )
    11. }
    12. }

    thing.ts

    1. import { INamed } from ‘./common
    2. import { Animal } from ‘./animal
    3. import { Color } from ‘./color
    4. export class Thing implements INamed {
    5. private _animal: Animal
    6. private _color: Color
    7. public get animal(): Animal {
    8. return this._animal
    9. }
    10. public get color(): Color {
    11. return this._color
    12. }
    13. public get name(): string {
    14. return `${this._color.name} ${this._animal.name}`
    15. }
    16. constructor(color: Color, animal: Animal) {
    17. this._animal = animal
    18. this._color = color
    19. }
    20. }
    21. function pickOne<T>(list: Array<T>): T {
    22. var idx = Math.floor(Math.random() * list.length)
    23. return list[idx]
    24. }
    25. export var makeThing = (colorList: Array<Color>, animalList: Array<Animal>) => {
    26. return new Thing(pickOne(colorList), pickOne(animalList))
    27. }

    main.ts

    1. import { Animals } from ‘./animal
    2. import { Colors } from ‘./color
    3. import { makeThing } from ‘./thing
    4. var colors = new Colors()
    5. var animals = new Animals()
    6. ;[1, 2, 3, 4, 5].forEach(() => {
    7. console.log(makeThing(colors, animals).name)
    8. })

    运行 npm run build 把Ts代码编译成js代码

    最后运行 node lib/main.js

    ![A40D7276-0803-45A5-B1BE-8CD913783EAD.png](https://cdn.nlark.com/yuque/0/2019/png/100715/1573176670842-e5a11901-017a-4545-8d3e-9c52933a5fbc.png#align=left&display=inline&height=85&name=A40D7276-0803-45A5-B1BE-8CD913783EAD.png&originHeight=85&originWidth=190&size=9058&status=done&width=190)

    代码见:typescript-for-node/TypeScriptProject at master · JeremyLikness/typescript-for-node · GitHub


    为了方便墙内的同学看上传视频到B站

    TypeScript for Node.js.mp4 (32.39MB)