当前位置:网站首页>Typescript基础学习记录
Typescript基础学习记录
2022-07-21 05:06:00 【qq_41302594】
TypeScript 基础
这里是我首次接触 TypeScript
的学习笔记。
基础类型
这里需要注意的是 null
和 undefined
是所有基础类型的子集,这意味着,你可以像这样赋值:
let varA:number = 1234;
varA = undefined;
但是不能反过来,将 1234
赋值给本是 undefined
的 varA
在 typescript
中新出现的一种基础数据类型,never
,是其他类型(包括 null
和 undefined
的子集,代表从不会出现的值。
这就意味着,你不能够再利用 never
去给 null
和 undefined
以及其他基础类型赋值,never
只能被 never
赋值。在函数中它通常表现为抛出异常或无法执行到终止点(例如无限循环)
只用 any 类型可以使用.运算符对其中的变量进行赋值。
循环
for… in 循环需要注意:
const list:string = '1234556';
for (const item in list) {
}
这里的 item
不能预先定义其类型。
函数
在 typescript
中,如果函数拥有返回值(或者没有,类型为 void
)需要为函数的返回值进行变量类型声明,函数的参数也必须带变量类型的声明:
function add(x:number, y:number):number {
return x + y;
}
function caller(x:number, y:number):void {
console.log(add(2, 3));
}
和 js
中不同,如果我们规定了参数,那么这些参数必须传入,当然,可以利用 ?
将某些参数定位可选参数
function scream(a:string, b?:string) {
if (b) {
console.log(a, b);
} else {
console.log(a);
}
}
在普通的 ES6
中:
let scream = (x: string, y:string): string => `he sayed ${
x} and ${
y}!`
只是对右侧的匿名函数做了类型定义,而左侧的变量是通过赋值类型推论而推断出来的。如果我们需要手动给 scream 添加类型,则应该是这样的:
let scream: (x: string, y: string) => number = function (x: string, y: string): string {
return `he sayed: ${
x} and ${
y}!`;
}
不要混淆了 Typescript 中的 =>
和 ES6
中的箭头函数。
在 Typescript 类型定义中, =>
是用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。
重载函数,需要设置为每个重载方法设置独一无二的参数类型列表,参数数量不同你可以将不同的参数设置为可选。
function a(x:string):void {
}
function a(x:number):void {
}
function a(x:number, y?:string):void {
}
数组
定义数组与之前定义变量不同,需要在变量类型后面加上 []
,或者利用数组的泛型,比如:
const arr_name: number[] = new Array<number>(4);
返回数组的函数,也要使用上面的方式:
function getSites() :string[] {
return new Array<string>('Google', 'Baidu', 'Tencent', 'Facebook');
}
元组
一般来讲,数组的类型都是相同的(any 类型可以不同),如果存储的数据类型不同,则使用元组。
元组用来代表有限个数的数据类型任意的集合。
如:
const tom: [string, number] = ['tom', 25]
联合类型
联合类型(Union Type)可以通过管道(|
)将变量设置为多种类型,赋值时可以根据设置的类型来赋值。
const val:string|number;
val = 12;
console.log(val);
联合类型也可作为函数参数:
function disp(name:string|string[]) {
if(typeof name == "string") {
console.log(name)
} else {
var i;
for(i = 0;i<name.length;i++) {
console.log(name[i])
}
}
}
disp("Runoob")
console.log("输出数组....")
disp(["Runoob","Google","Taobao","Facebook"])
也可以作为函数的返回类型:
function testFunction(flag: boolean): string|number {
if (flag) {
return '12345';
} else {
return 1234567;
}
}
console.log(testFunction(false));
数组也可以使用。
接口
在 typescript
中 interface
不能被编译成 javascript
语法,这是 typescript
独有的。
接口在 typescript
中用来对对象或者函数进行一定的限制。
比如:
interface IPerson {
firstName: string,
lastName: string,
sayHi():string,
}
const Customer: IPerson = {
firstName: 'Tom',
lastName: 'Hanks',
sayHi: function():string {
return 'Hi there'},
}
console.log(Customer.sayHi());
这就意味着,在实现该接口的对象中,必须要 firstName
、lastName
、sayHi
这三个属性。
接口中也可以使用 ?
来规定可选属性:
interface IPerson {
firstName: string,
lastName: string,
sayHi?():string,
}
const Customer: IPerson = {
firstName: 'Tom',
lastName: 'Hanks',
}
console.log(Customer.firstName);
接口可以继承,通过 extends
来进行继承,继承方式类似于 java。
类
基础的类的用法类似于 javascript
中的类的用法,只不过需要明确其中的变量类型
class Car {
// 字段
engine: string;
// 构造函数
constructor(engine: string) {
this.engine = engine;
}
// 方法
disp(): void {
console.log('发动机为:', this.engine);
}
}
typescript
中的类也只允许单继承,不允许多继承,但是允许多重继承。
断言
注意,不能滥用断言,断言可能会出现某些问题。断言会欺骗 typescript 编辑器,但是无法避免运行时错误。
断言也是有限制的,并不是一个类型都可以被被断言为任何另一个类型的。
具体来说,必须要 A 兼容 B,或者 B 兼容 A,那么两者可以互相断言。这是由于,Typescript 是一门结构类型系统,类型之间的对比指挥比较它们最终的结构,而会忽略它们定义时的关系。
所以:
- 联合类型可以被断言为其中一个类型
- 父类可以被断言为子类
- 任何类型都可以被断言为 any
- any 可以被断言为任何类型
- 要使得 A 能够被断言为 B,只需要 A 兼容 B 或 B 兼容 A 即可。
前四种情况都是最后一个的特例。
边栏推荐
- qml 实现csdn搜索框,无规则圆角
- Relationship between prototype, prototype chain, constructor and instance
- C语言实现三子棋小游戏---格局打开版(
- [C language] run away and get out of this secular C preliminary and final battle full of fireworks
- Acwing 175 circuit maintenance
- Detailed derivation of Differential Flatness of UAV
- Optimization of image multi picture pages
- $.each的用法
- Cannot read property mode of undefined
- Global variable configuration dev . dev.dev . dev.test
猜你喜欢
[C语言] 选择 循环还可以这样玩 代码表白光之国
MySQL import and export & View & Index & execution plan
Cannot read property mode of undefined
xml建模
1.vite初识、vite搭建项目
xcode升级后找不到 C语言头文件 stdio.h的解决办法
js运行机制
[C language] please fly the detailed ability functions and arrays back to the country of light
Common formula for matrix derivation (avoiding pit) + derivation of matrix modulus and absolute value
推荐一个好用的 所见即所得的 markdown 编辑器 Mark Text
随机推荐
uniapp 判断用户蓝牙是否打开
Upload pictures and avatars
Jump and receive parameters of JS page
Output statements on the console
用大白话让你理解TypeScript的要点.(三)
window. history. back(); The problem that layer cannot be closed after returning
2020 Changzhou programming expert real problem and solution
【好文记录】嵌入式框架Zorb Framework搭建过程
第四局 借问酒家何处有?牧童遥指杏花村 下
js页面的跳转以及接收参数
Running goofy group code on ROS encounters [global_planning_node-5] process has did [pid 3143, exit code -6, CMD /home/gaofei/ros
Mise en œuvre personnalisée du cadre MVC
MySQL之函数
How to get the second child element under the first child element
In the fourth game, where is the restaurant? Shepherd boy points to Xinghua village
useMemo和useCallback的区别以及使用场景
In depth analysis of string -- strcpy & strncpy
在ROS上跑高飞组代码遇到[global_planning_node-5] process has died [pid 3143, exit code -6, cmd /home/gaofei/ROS
Deep analysis of string -- strlen & strtok
Why use the fast and slow pointer to find the link of the linked list, and the fast pointer and the slow pointer must meet?