2022/04/01 TypeScript_Day1
背景记录
首先我有java
和go
的语言基础,然后我是一名测试,现在遇到的问题是公司使用solidity
的truffle
和hardhat
框架进行开发,solidity
这门语言是运行在以太坊虚拟机上面的,他编译出来的文件可以作为javascript
的包引入在javascript
当中然后使用javascript
编写测试用例.所以这就需要我去掌握javascript
由于弱类型语言在看代码的时候转型的时候搞不清楚,本着逻辑清晰的原则所以选择学习typescript
环境声明
使用的是Mac pro 14
已经预先安装了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
文件进行编译:
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
保留的关键字
这里只重点拿出几个关键的:
as
number
get
moudle
export
any
yield
TypeScript
拥有的一些特点
- 分号可选
- 区分大小写
- 面向对象
什么是"对象"
谈恋爱的就叫"对象"[doge]
示例代码:
声明一个动物的对象,他们有眼睛和四只脚,并切能够叫和走
/** * Declare a class which is about animal */ class Animal{ eyes: number = 2 foot: number = 4
// Provide some function to obtain corresponding @R_713_4045@ion
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]
?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。