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.数据结构:数据存储的问题,二 ...
随机推荐
- 我的第一个开源项目 Kiwis2 Mockserver
我的第一个开源作品Kiwis2 Mock Server,目前公测中,欢迎大家提供宝贵意见. 代码:https://github.com/kiwis2/mockserver 主页:https://kiw ...
- 日志导致jvm内存溢出相关问题
生产环境日志级别为info,请看如下这行代码: LOGGER.debug("the DTO info: {}", JSON.toJSONString(DTO)); 这段代码主要有两 ...
- C# CS0050 可访问性不一致: 返回类型 错误
今天学习C#代码过程中,遇到可访问性不一致的错误: 严重性 代码 说明 项目 文件 行 禁止显示状态错误 CS0050 可访问性不一致: 返回类型"Transaction"的可访问 ...
- Skywalking-07:OAL原理——解释器实现
OAL 解释器实现 OAL 解释器是基于 Antlr4 实现的,我们先来了解下 Antlr4 Antlr4 基本介绍 Antlr4 使用案例 参考Antlr4的使用简介这篇文章,我们实现了一个简单的案 ...
- Set重写hashCode和equals方法实现引用对象去重
运作原理: 首先判断hashCode是否相同,如果不同,直接判定为两个不同的对象.如果hashCode相同,再去比较equals是否一样,如果一样,则为同一个对象.如果不一样,则是两个不同对象. 那么 ...
- PostgreSQL隐藏字段
1)创建了一个表 apple=# \d test_time Table "public.test_time" Column | Type | Modifiers --------+ ...
- Blazor+Dapr+K8s微服务之事件发布订阅
我们要实现的是:在blazorweb服务中发布一个事件,并传递事件参数,然后在serviceapi1服务中订阅该事件,接收到blazorweb服务中发布的事件和参数. 1 在blazo ...
- The Second Week lucklyzpp
The Second Week 文件通配符模式 在Linux系统中预定义的字符类 1.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录 2.复制/etc目录下 ...
- redis subscribe/publish(发布订阅)
redis的发布端 package dubbo.wangbiao.project.pubsub; import org.apache.commons.pool2.impl.GenericObjectP ...
- 三大操作系统对比使用之·MacOSX
时间:2018-11-13 整理:byzqy 本篇是一篇个人对Mac系统使用习惯和应用推荐的分享,在此记录,以便后续使用查询! 打开终端: command+空格,调出"聚焦搜索(Spotli ...