当前位置:网站首页>TypeScript入门知识总结
TypeScript入门知识总结
2022-08-07 05:11:00 【初辰ge】
安装TypeScript
通过npm(Node.js包管理器)
npm install -g typescript
构建第一个TypeScript文件
创建1.ts文件并将下面代码粘贴到文件中
function greeter(person) {
return "Hello, " + person;
}
let user = "初辰";
console.log(greeter(user));
编译代码
在命令行上,运行TypeScript编译器
tsc 1.ts
输出结果为一个1.js文件,它包含了和输入文件中相同的JavsScript代码。 一切准备就绪,就可以运行这个使用TypeScript写的JavaScript应用了
类型注解
给 person函数的参数添加: string类型注解,并将user变量赋值为数组,如下:
function greeter(person: string) {
return "Hello, " + person;
}
let user = [0, 1, 2];
console.log(greeter(user));
重新编译,会产生一个如下错误:
尽管代码里有错误,但仍然可以使用TypeScript。但在这种情况下,TypeScript会警告可能不会按预期执行。
接口
在TypeScript里,只在两个类型内部的结构兼容那么这两个类型就是兼容的。 这就允许我们在实现接口时候只要保证包含了接口要求的结构就可以,而不必明确地使用 implements语句。
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + person.lastName;
}
let user = { firstName: "初", lastName: "辰" };
console.log(greeter(user));

类
TypeScript支持JavaScript的新特性,比如支持基于类的面向对象编程。
创建一个Student类,它带有一个构造函数和一些公共字段。
类和接口可以一起工作,在构造函数的参数上使用public等同于创建了同名的成员变量。
class Student {
fullName: string;
constructor(
public firstName,
public middleInitial,
public lastName
) {
this.fullName = firstName + middleInitial + lastName;
}
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello," + person.firstName + person.lastName;
}
let user = new Student("初", "and", "辰");
console.log(greeter(user));
重新运行tsc 1.ts,你会看到生成的JavaScript代码和原先的一样。 TypeScript里的类只是JavaScript里常用的基于原型面向对象编程的简写。
运行TypeScript Web应用
新建1.html文件并引入1.js文件即可运行tsweb应用
<body>
<script src="1.js"></script>
</body>
document.body.innerHTML = greeter(user);

边栏推荐
- 《国际学术论文写作与发表》参考答案
- Linear algebra learning notes 4-2: understanding of the system of linear equations, gaussian elimination, LU decomposition
- Records about log4j security vulnerabilities and version to replace
- Linear Algebra Study Notes 4-5: Solving Systems of Linear Equations
- 【毕业设计】基于STM32的自动加油站加油系统 -物联网 单片机 嵌入式
- C51 基本函数、中断函数和库函数的详解
- Redis 事务说明与 watch 命令监控事务
- Go language | 05 Template learning
- 什么是负载均衡
- 线性代数学习笔记4-4:求解非齐次线性方程组Ax=b,从秩的角度看方程
猜你喜欢

MySQL advanced 1 - underlying data structure B+ tree

线性代数学习笔记5-2:(正交的)投影、投影矩阵、A^T A、最小二乘法LS

向量化引擎对HTAP的价值与技术思考

IDEA 2022.2 released

CentOs通过yum在线安装Mysql

人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准

谭浩强第五版第三章课后习题

线性代数学习笔记6-1:行列式与线性变换

理想汽车×OceanBase:当造车新势力遇上数据库新势力

From the rejection of my resume to the harvest of 8 big company offers, it took me 3 months to successfully break through the cocoon and become a butterfly
随机推荐
远程连接 redis 时,报错 (error) DENIED Redis is running in protected mode because protected mode is enabled
对话ACE第四期:分布式数据库未来发展的挑战和机遇
openssl 1.1.1k & libsrtp2-dev 2.3.0-2 installed
happens-before rule and thread singleton safety exercise
OK-MY TODO LIST
Small P weekly, Vol. 14
Terminal data encryption
MySQL---Single-table and multi-table query exercises
Talk about 7 magic skills of Redis memory optimization
洛谷P1201 贪婪的送礼者Greedy Gift Givers
线性代数学习笔记4-3:求解齐次线性方程组Ax=0、消元法、行最简阶梯型矩阵RRFE
乘势而上,OceanBase推动数字支付精益增长
动态规划之背包问题
谭浩强第五版c语言第四章课后习题
IDEA 2022.2 released
洛谷P1188 PASTE
洛谷P1189 `SEARCH`
Linear algebra study notes 4-4: Solving the inhomogeneous system of linear equations Ax=b, looking at the equations from the perspective of rank
页面底部出现横向滚动条解决方法
洋葱集团携手OceanBase实现分布式升级,全球数据首次实现跨云融合