2022/04/01 TypeScript_Day1

2022/4/2 6:51:03

本文主要是介绍2022/04/01 TypeScript_Day1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

2022/04/01 TypeScript_Day1

背景记录

首先我有javago的语言基础,然后我是一名测试,现在遇到的问题是公司使用soliditytrufflehardhat框架进行开发,solidity这门语言是运行在以太坊虚拟机上面的,他编译出来的文件可以作为javascript的包引入在javascript当中然后使用javascript编写测试用例.所以这就需要我去掌握javascript

由于弱类型语言在看代码的时候转型的时候搞不清楚,本着逻辑清晰的原则所以选择学习typescript

同时还需要能看懂solidity代码的上下文构造测试场景.

环境声明

使用的是Mac pro 14已经预先安装了Node.js的环境,所以通过Node.js的包管理工具进行下载即可

修改Node.js包管理工具的镜像源:

npm config set registry https://registry.npmmirror.com

安装typescript环境:

npm install -g typescript

查看typescript版本:

tsc -v

HelloWorld of TypeScript

var message:string = "HelloWorld" console.log(message) .ts文件进行编译:

编译后会生成一个同名的.js文件

tsc HelloWorld.ts

运行.js文件:

node HelloWorld.js

熟悉TypeScript编译指令

  • 多文件编译 ---> tsc file1.ts file2.ts ...
  • 额外生成一个.d.ts扩展名文件 --->该文件是个类似声明的文件 --->tsc helloworld.ts --declaration
  • 删除源文件的注释 --->tsc helloworld.ts --removeComments
  • 生成一个sourcemap(.map)文件 --->一个sourcemap是一个存储源代码与编译代码对应位置映射的信息文件 --->tsc helloworld.ts --sourcemap
  • 在表达式和声明上有隐含的any类型时报错 --->tsc helloworld.ts module nolmplicitAny
  • 监视模式下运行编译器(监视输出文件,输出文件被更改时重新编译) --->tsc helloworld.ts --watch

TypeScript保留的关键字

这里只重点拿出几个关键的:

  1. as
  2. number
  3. get
  4. moudle
  5. export
  6. any
  7. yield

TypeScript拥有的一些特点

  • 分号可选
  • 区分大小写
  • 面向对象
什么是"对象"

谈恋爱的就叫"对象"[doge]

所谓对象就是用一些属性和行为描述的一组或者一类型的内容

示例代码:

声明一个动物的对象,他们有眼睛和四只脚,并切能够叫和走

/** * Declare a class which is about animal */ class Animal{ eyes: number = 2 foot: number = 4

// Provide some function to obtain corresponding information
getEyesNumber(): number {
    return this.eyes
}

getFootNumber(): number {
    return this.foot
}

// Provide some function about animal
// Shout function
shout(object: Animal): void {
    console.log("Animal can shout!")
}

move(object: Animal): void {
    console.log("Animal can move")
}

}

// Call the function about animal var animal = new Animal()

var eyeNumver: number = animal.getEyesNumber()

var footNumber: number = animal.getFootNumber()

console.log(eyeNumver) console.log(footNumber) animal.shout(animal) animal.move(animal)

存在疑问

为什么上面两个get方法打印的结果是[Function]?



这篇关于2022/04/01 TypeScript_Day1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程