当前位置:网站首页>TypeScript—语法简介
TypeScript—语法简介
2022-07-21 19:49:00 【测试狗一枚】
TypeScript官方指导文档:https://www.tslang.cn/docs/home.html
基本类型
变量声明
使用let
和const两个关键字声明变量,具体形式如下:
let user = "Jane User";
接口定义
interface SquareConfig {
color?: string;//?代表可选属性
width?: number;
readonly x: number;//readonly 表示只读属性
readonly y: number;
}
function createSquare(config: SquareConfig): { color: string; area: number } {
let newSquare = {color: "white", area: 100};
if (config.clor) {
// Error: Property 'clor' does not exist on type 'SquareConfig'
newSquare.color = config.clor;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
let mySquare = createSquare({color: "black"});
类定义
abstract class Department {
constructor(public name: string) {
}
printName(): void {
console.log('Department name: ' + this.name);
}
abstract printMeeting(): void; // 必须在派生类中实现
}
class AccountingDepartment extends Department {
constructor() {
super('Accounting and Auditing'); // 在派生类的构造函数中必须调用 super()
}
printMeeting(): void {
console.log('The Accounting Department meets each Monday at 10am.');
}
generateReports(): void {
console.log('Generating accounting reports...');
}
}
let department: Department; // 允许创建一个对抽象类型的引用
department = new Department(); // 错误: 不能创建一个抽象类的实例
department = new AccountingDepartment(); // 允许对一个抽象子类进行实例化和赋值
department.printName();
department.printMeeting();
department.generateReports(); // 错误: 方法在声明的抽象类中不存在
相关知识点:
class关键字,继承关键字extends,公有public、私有private与受保护protected的修饰符,只读属性修饰符readonly、静态属性static、getters/setters存取器、抽象类abstract、
函数定义
// Named function
function add(x, y) {
return x + y;
}
// Anonymous function
let myAdd = function(x, y) { return x + y; };
相关知识点:
有名函数、匿名函数、可选参数、默认参数、剩余参数、
边栏推荐
- 测试环境建设的基本原则
- 工作任务“杂乱难”?这个小工具帮你轻松搞定!
- Servlet优化
- Some optimization of code
- How to carry out the anti disclosure work of enterprise source code
- C # implement setting expiration time for PDF documents
- Carbon 语言【中文入门教程】
- linux如何查询oracle错误日志
- 2022 latest Hubei construction eight members (Mechanics) simulated examination question bank and answers
- New product release: VGA display driver module for bus video monitoring
猜你喜欢
窗口函数的5种方法总结
[微信小程序开发者工具] × #initialize-error: Error: ENOENT: no such file or directory, open
Weekly recommended short video: what is the difference between IOT development and embedded development?
MongoDB的基本操作介绍、数据库/表的创建和删除、数据的查询
pango logos 双启动
Interview difficulties: difficulties in implementing distributed session, this is enough!
每周推荐短视频:物联网开发和嵌入式开发有什么区别?
企业源代码防泄密工作该如何开展
新品发布:总线视频监控专用VGA显示驱动模组
IDEA 2022.2 正式发布,骚操作,跟不上了!
随机推荐
day02-2
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
Servlet optimization
Idea 2022.2 was officially released, and I can't keep up with it!
【信息收集】从FoFa—API接口数据写入TXT和Excel
利用西门子低代码实现企业质量管理流程的敏捷性
Response响应字节数据
814. Binary tree pruning
New product release: VGA display driver module for bus video monitoring
基于ROS的机器人模型建立及3D仿真【物理/机械意义】
2022-7-21 第七组 pan小堂 继承与super与this
Branch merge
What is the fastapi asynchronous framework? (comprehensive understanding)
如何获得 Apache 官方域名邮箱?专访 Apache Linkis 五位新晋 Committer
Encapsulation
mysql
How to use document tools for API management?
Chapter1 Beginning Bash
leetcode 792. Number of Matching Subsequences(匹配的子串数量)
Halcon series (2): hyperboxes