TS基础笔记
TS优势
更好的错误的提示,开发中及时发现问题;
编辑器语法提示更完善;
类型声明可以看出数据结构的语义,可读性更好;
TS环境搭建
1.安装node;
2.npm install typescript@3.6.4 -g;
因为ts文件不能直接在浏览器和node环境中运行,此时需要用运行:tsc xx.ts,自动生成一个js文件,然后运行:node xx.js才可以
3.npm install ts-node -g
此时运行 ts-node xx.ts就可以
TS基础类型和对象类型
//基础类型 null,undefined,symbol,boolean,void
const count: number = 123;
const myName: string = "lizhao";
//对象类型
class Person {}
const teacher: {
name: string;
} = {
name: "lizhao",
};
const nums: number[] = [1, 2, 3];
const person: Person = new Person();
const getTotal: () => number = () => {
return 123;
};
类型注解和类型推断
类型注解,我们来告诉ts变量是什么类型;
类型推断,ts会自动去尝试分析变量的类型;
如果ts能够自动分析变量类型,我们就说明也不需要做了,如果无法分析变量类型,我们就需要使用类型注解。
函数类型相关
//普通参数
function fun(p1: number, p2: number) {
return p1 + p2;
}
fun(1, 2);
//结构参数
function fun1({ p1, p2 }: { p1: number; p2: number }) {
return p1 + p2;
}
fun1({ p1: 1, p2: 2 });
//返回值类型
function fun2(p1: number, p2: number): number {
return p1 + p2;
}
fun2(1, 2);
function fun3(p1: number, p2: number): void {
console.log(p1 + p2);
}
function fun4(p1: number, p2: number): never {
throw new Error();
}
//以下几种写法都可以
const fun5_1 = (p1: number, p2: number): number => {
return p1 + p2;
};
const fun5_2: (p1: number, p2: number) => number = (p1, p2) => {
return p1 + p2;
};
数组和元祖
// 数组
const arr: (string | number)[] = [1, 2, "1"];
//type 类型别名
type user = { name: string; age: number };
const arr1: user[] = [{ name: "lz", age: 18 }];
class Teacher {
name: string;
age: number;
}
const objectArr:Teacher[]=[
new Teacher(),
{
name: 'lz',
age: 18
}
]
// 元组
const info: [string, number] = ["lizhao", 18];
const infoList: [string, number][] = [
["lizhao", 18],
["lizhao", 19],
];
interface接口
interface Person {
readonly sex: string; //只读属性
name: string; //必传属性
age?: number; //非必传属性 ?
[propName: string]: any; //其它属性
say(): string; //方法
}
const getPersonNane = (person: Person): void => {
console.log(person.name);
};
const setPersonNane = (person: Person, name: string): void => {
person.name = name;
};
const p1: Person = {
sex: "女",
name: "lz",
otner: "xxx",
say() {
return "hello lz";
},
};
getPersonNane(p1);
//继承
interface Teacher extends Person {
teach(): string;
}
//应用:类应用接口,类里边必须具备 必传属性
class User implements Person {
sex = "女";
name = "lz";
say() {
return "hello lz";
}
}
//定义函数类型
interface SayHi {
(word: string): string;
}
const say: SayHi = (word: string) => {
return "lz";
};
类
class Person {
name = "li";
getName() {
return this.name;
}
}
//子类可以继承父类,可以重写父类方法,子类用super可以调用父类的方法。
class Teacher extends Person {
getName() {
return super.getName() + "zhao";
}
}
const p1 = new Teacher();
console.log(p1.getName());
类的访问属性和constructo
//private,protected,publick访问类型
// publick允许在类内外调用
// protected允许在类内和继承的子类中使用
// private只能在类内用
//constructor
class Person {
//传统写法
// public name: string;
// constructor(name: string) {
// this.name = name;
// 简便写法
constructor(public name: string) {}
}
const p1 = new Person("lz");
//子类继承父类的时候,子类如果有constructor,constructor里必须调用super(),不论父类是否有constructor。
class Teacher extends Person {
constructor(name: string, public age: number) {
super(name);
}
}
const t1 = new Teacher("lz1", 18);
console.log(p1);
console.log(t1);
getter setter
class Person {
constructor(private _name: string) {}
get name() {
return this._name;
}
set name(name: string) {
this._name = name;
}
}
const p1 = new Person("lz");
console.log(p1.name);
单例模式
class Demo {
private static instance: Demo;
private constructor(public name: string) {}
static getInstance() {
if (!this.instance) {
this.instance = new Demo("lz");
}
return this.instance;
}
}
const demo1 = Demo.getInstance();
const demo2 = Demo.getInstance();
console.log(demo1.name);
console.log(demo2.name);
readonly
限制一个public属性只能读不能改
class Demo {
public readonly name: string;
constructor(name: string) {
this.name = name;
}
}
const demo1 = new Demo("dell");
抽象类
抽象类,把公共的基础的东西抽象出来
abstract class Geom {
width: number = 12;
getType() {
return "Geom";
}
abstract getArea(): number;
}
class Circle extends Geom {
getArea() {
return this.width * 12;
}
}
const c1 = new Circle();
console.log(c1.getArea());
interface继承简化代码
interface Person {
name: string;
}
interface Teacher extends Person {
age: number;
}
interface Student extends Person {
sex: string;
}
const teacher: Teacher = { name: "lz", age: 18 };
const student: Student = { name: "lz", sex: "女" };
const getUserInfo = (user: Person) => {
console.log(user.name);
};
泛型
可以把泛型看做一个占位符,在使用的时候,在动态的填入类型值。
//例1:
function echo<T>(arg: T): T {
return arg;
}
const str = echo("str");
const num = echo(123);
//例2:接口
interface Obj<T, U> {
key: T;
val: U;
}
const obj: Obj<number, string> = {
key: 1,
val: "234",
};
//Array也是一个interface,Array<number>是interface搭配泛型的用法
let arr: Array<number> = [1, 2, 3];
//例3:函数
interface Plus<T> {
(a: T, b: T): T;
}
const plusNum: Plus<number> = (a: number, b: number) => {
return a + b;
};
const plusStr: Plus<string> = (a: string, b: string) => {
return a + b;
};
console.log(plusNum(1, 2));
console.log(plusStr("1", "2"));
约束泛型
用extends来约束泛型
interface withLength {
length: number;
}
function echoWithLength<T extends withLength>(arg: T) {
return arg.length;
}
const str = echoWithLength({ length: 2 });
类型别名
type user = { name: string; age: number };
const arr1: user[] = [{ name: "lz", age: 18 }];
类型断言
function getLength(input: string | number): number {
if ((<string>input).length) {
return (<string>input).length;
} else {
return input.toString().length;
}
}
TS基础笔记的更多相关文章
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 | 被围观 25,969 views+ 1.Anno ...
- php代码审计基础笔记
出处: 九零SEC连接:http://forum.90sec.org/forum.php?mod=viewthread&tid=8059 --------------------------- ...
- MYSQL基础笔记(六)- 数据类型一
数据类型(列类型) 所谓数据烈性,就是对数据进行统一的分类.从系统角度出发时为了能够使用统一的方式进行管理,更好的利用有限的空间. SQL中讲数据类型分成三大类:1.数值类型,2.字符串类型和时间日期 ...
- MYSQL基础笔记(五)- 练习作业:站点统计练习
作业:站点统计 1.将用户的访问信息记录到文件中,独占一行,记录IP地址 <?php //站点统计 header('Content-type:text/html;charset=utf-8'); ...
- MYSQL基础笔记(四)-数据基本操作
数据操作 新增数据:两种方案. 1.方案一,给全表字段插入数据,不需要指定字段列表,要求数据的值出现的顺序必须与表中设计的字段出现的顺序一致.凡是非数值数据,到需要使用引号(建议使用单引号)包裹. i ...
- MYSQL基础笔记(三)-表操作基础
数据表的操作 表与字段是密不可分的. 新增数据表 Create table [if not exists] 表名( 字段名 数据类型, 字段名 数据类型, 字段n 数据类型 --最后一行不需要加逗号 ...
- MYSQL基础笔记(二)-SQL基本操作
SQL基本操作 基本操作:CRUD,增删改查 将SQL的基本操作根据操作对象进行分类: 1.库操作 2.表操作 3.数据操作 库操作: 对数据库的增删改查 新增数据库: 基本语法: Create da ...
- MYSQL基础笔记(一)
关系型数据库概念: 1.什么是关系型数据库? 关系型数据库:是一种建立在关系模型(数学模型)上的数据库 关系模型:一种所谓建立在关系上的模型. 关系模型包含三个方面: 1.数据结构:数据存储的问题,二 ...
随机推荐
- Docker部署ELK之部署logstash7.6.0(4)
前言: logstash 和filebeat都具有日志收集功能,filebeat更轻量,占用资源更少,但logstash 具有filter功能,能过滤分析日志.一般结构都是filebeat采集日志,然 ...
- linux copy_id
ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制到远程机器的authorized_keys文件中,ssh-copy-id也能让你有到远程机器的home, ~./ssh ...
- Vue CLI安装报错 npm ERR! Exit handler never called!
安装Vue CLI时报错: npm install –g vue-cli 试了四种办法 1.把全局安装-g放到后面 npm install @vue/cli –g 2.命令行输入 npm 缓存清理命令 ...
- OSPF的Router-Id
一.实验拓扑 二.实验编址 三.实验步骤: 1.设置PC的IP等信息 2.启动设备(全选) 3.根据实验编址配置路由器端口IP(先不设置lookback端口) R1: R2: R3: R4: 看一下r ...
- linux /etc/passwd详解
文件概述 Linux 系统中的 /etc/passwd 文件,是系统用户配置文件,存储了系统中绝大部分的用户基本信息,并不是所有,所有用户都可以对此文件执行读操作.(如果通过其他方式创建管理的用户名. ...
- SQL 练习18
按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 SELECT t.cid,t.sid,t.score ,COUNT(t1.score)+1 as 排名 from sc as t LE ...
- NOIP 模拟 $26\; \rm 幻魔皇$
题解 \(by\;zj\varphi\) 观察可发现一个点向它的子树走能到的白点,黑点数是一个斐波那契数列. 对于白色点对,可以分成两种情况: 两个白点的 \(lca\) 是其中一个白点 两个白点的 ...
- NOIP 模拟 6 辣鸡
题解 难得啊,本来能 \(AC\) 的一道题,注释没删,挂了五分,难受 此题暴力很好想,就是直接 \(n^2\) 枚举不同的矩阵组合,记录块内答案和跨块的答案 出题人不会告诉你,这题只要输出块内答案就 ...
- Linux 第一个静态库 (两种方法)
方法1 --------------------------------------------- Linux下 静态库 一般为.a为扩展名 类似 lib***.a 的文件名.利用静态函数库编译 ...
- 【springcloud】Zuul 超时、重试、并发参数设置
转自:https://blog.csdn.net/xx326664162/article/details/83625104 一. Zuul 服务网关 服务网关 = 路由转发 + 过滤器 1.路由转发: ...