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基础类型和对象类型

  1. //基础类型 null,undefined,symbol,boolean,void
  2. const count: number = 123;
  3. const myName: string = "lizhao";
  4. //对象类型
  5. class Person {}
  6. const teacher: {
  7. name: string;
  8. } = {
  9. name: "lizhao",
  10. };
  11. const nums: number[] = [1, 2, 3];
  12. const person: Person = new Person();
  13. const getTotal: () => number = () => {
  14. return 123;
  15. };

类型注解和类型推断

类型注解,我们来告诉ts变量是什么类型;
类型推断,ts会自动去尝试分析变量的类型;
如果ts能够自动分析变量类型,我们就说明也不需要做了,如果无法分析变量类型,我们就需要使用类型注解。

函数类型相关

  1. //普通参数
  2. function fun(p1: number, p2: number) {
  3. return p1 + p2;
  4. }
  5. fun(1, 2);
  6. //结构参数
  7. function fun1({ p1, p2 }: { p1: number; p2: number }) {
  8. return p1 + p2;
  9. }
  10. fun1({ p1: 1, p2: 2 });
  11. //返回值类型
  12. function fun2(p1: number, p2: number): number {
  13. return p1 + p2;
  14. }
  15. fun2(1, 2);
  16. function fun3(p1: number, p2: number): void {
  17. console.log(p1 + p2);
  18. }
  19. function fun4(p1: number, p2: number): never {
  20. throw new Error();
  21. }
  22. //以下几种写法都可以
  23. const fun5_1 = (p1: number, p2: number): number => {
  24. return p1 + p2;
  25. };
  26. const fun5_2: (p1: number, p2: number) => number = (p1, p2) => {
  27. return p1 + p2;
  28. };

数组和元祖

  1. // 数组
  2. const arr: (string | number)[] = [1, 2, "1"];
  3. //type 类型别名
  4. type user = { name: string; age: number };
  5. const arr1: user[] = [{ name: "lz", age: 18 }];
  6. class Teacher {
  7. name: string;
  8. age: number;
  9. }
  10. const objectArr:Teacher[]=[
  11. new Teacher(),
  12. {
  13. name: 'lz',
  14. age: 18
  15. }
  16. ]
  17. // 元组
  18. const info: [string, number] = ["lizhao", 18];
  19. const infoList: [string, number][] = [
  20. ["lizhao", 18],
  21. ["lizhao", 19],
  22. ];

interface接口

  1. interface Person {
  2. readonly sex: string; //只读属性
  3. name: string; //必传属性
  4. age?: number; //非必传属性 ?
  5. [propName: string]: any; //其它属性
  6. say(): string; //方法
  7. }
  8. const getPersonNane = (person: Person): void => {
  9. console.log(person.name);
  10. };
  11. const setPersonNane = (person: Person, name: string): void => {
  12. person.name = name;
  13. };
  14. const p1: Person = {
  15. sex: "女",
  16. name: "lz",
  17. otner: "xxx",
  18. say() {
  19. return "hello lz";
  20. },
  21. };
  22. getPersonNane(p1);
  23. //继承
  24. interface Teacher extends Person {
  25. teach(): string;
  26. }
  27. //应用:类应用接口,类里边必须具备 必传属性
  28. class User implements Person {
  29. sex = "女";
  30. name = "lz";
  31. say() {
  32. return "hello lz";
  33. }
  34. }
  35. //定义函数类型
  36. interface SayHi {
  37. (word: string): string;
  38. }
  39. const say: SayHi = (word: string) => {
  40. return "lz";
  41. };

  1. class Person {
  2. name = "li";
  3. getName() {
  4. return this.name;
  5. }
  6. }
  7. //子类可以继承父类,可以重写父类方法,子类用super可以调用父类的方法。
  8. class Teacher extends Person {
  9. getName() {
  10. return super.getName() + "zhao";
  11. }
  12. }
  13. const p1 = new Teacher();
  14. console.log(p1.getName());

类的访问属性和constructo

  1. //private,protected,publick访问类型
  2. // publick允许在类内外调用
  3. // protected允许在类内和继承的子类中使用
  4. // private只能在类内用
  5. //constructor
  6. class Person {
  7. //传统写法
  8. // public name: string;
  9. // constructor(name: string) {
  10. // this.name = name;
  11. // 简便写法
  12. constructor(public name: string) {}
  13. }
  14. const p1 = new Person("lz");
  15. //子类继承父类的时候,子类如果有constructor,constructor里必须调用super(),不论父类是否有constructor。
  16. class Teacher extends Person {
  17. constructor(name: string, public age: number) {
  18. super(name);
  19. }
  20. }
  21. const t1 = new Teacher("lz1", 18);
  22. console.log(p1);
  23. console.log(t1);

getter setter

  1. class Person {
  2. constructor(private _name: string) {}
  3. get name() {
  4. return this._name;
  5. }
  6. set name(name: string) {
  7. this._name = name;
  8. }
  9. }
  10. const p1 = new Person("lz");
  11. console.log(p1.name);

单例模式

  1. class Demo {
  2. private static instance: Demo;
  3. private constructor(public name: string) {}
  4. static getInstance() {
  5. if (!this.instance) {
  6. this.instance = new Demo("lz");
  7. }
  8. return this.instance;
  9. }
  10. }
  11. const demo1 = Demo.getInstance();
  12. const demo2 = Demo.getInstance();
  13. console.log(demo1.name);
  14. console.log(demo2.name);

readonly

  1. 限制一个public属性只能读不能改
  2. class Demo {
  3. public readonly name: string;
  4. constructor(name: string) {
  5. this.name = name;
  6. }
  7. }
  8. const demo1 = new Demo("dell");

抽象类

  1. 抽象类,把公共的基础的东西抽象出来
  2. abstract class Geom {
  3. width: number = 12;
  4. getType() {
  5. return "Geom";
  6. }
  7. abstract getArea(): number;
  8. }
  9. class Circle extends Geom {
  10. getArea() {
  11. return this.width * 12;
  12. }
  13. }
  14. const c1 = new Circle();
  15. console.log(c1.getArea());

interface继承简化代码

  1. interface Person {
  2. name: string;
  3. }
  4. interface Teacher extends Person {
  5. age: number;
  6. }
  7. interface Student extends Person {
  8. sex: string;
  9. }
  10. const teacher: Teacher = { name: "lz", age: 18 };
  11. const student: Student = { name: "lz", sex: "女" };
  12. const getUserInfo = (user: Person) => {
  13. console.log(user.name);
  14. };

泛型

  1. 可以把泛型看做一个占位符,在使用的时候,在动态的填入类型值。
  2. //例1:
  3. function echo<T>(arg: T): T {
  4. return arg;
  5. }
  6. const str = echo("str");
  7. const num = echo(123);
  8. //例2:接口
  9. interface Obj<T, U> {
  10. key: T;
  11. val: U;
  12. }
  13. const obj: Obj<number, string> = {
  14. key: 1,
  15. val: "234",
  16. };
  17. //Array也是一个interface,Array<number>是interface搭配泛型的用法
  18. let arr: Array<number> = [1, 2, 3];
  19. //例3:函数
  20. interface Plus<T> {
  21. (a: T, b: T): T;
  22. }
  23. const plusNum: Plus<number> = (a: number, b: number) => {
  24. return a + b;
  25. };
  26. const plusStr: Plus<string> = (a: string, b: string) => {
  27. return a + b;
  28. };
  29. console.log(plusNum(1, 2));
  30. console.log(plusStr("1", "2"));

约束泛型

  1. extends来约束泛型
  2. interface withLength {
  3. length: number;
  4. }
  5. function echoWithLength<T extends withLength>(arg: T) {
  6. return arg.length;
  7. }
  8. const str = echoWithLength({ length: 2 });

类型别名

  1. type user = { name: string; age: number };
  2. const arr1: user[] = [{ name: "lz", age: 18 }];

类型断言

  1. function getLength(input: string | number): number {
  2. if ((<string>input).length) {
  3. return (<string>input).length;
  4. } else {
  5. return input.toString().length;
  6. }
  7. }

TS基础笔记的更多相关文章

  1. C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用

    C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备)  https://blog.csdn.net/u013519551/article/details/51220841 1. . ...

  2. Java基础笔记 – Annotation注解的介绍和使用 自定义注解

    Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 |  被围观 25,969 views+ 1.Anno ...

  3. php代码审计基础笔记

    出处: 九零SEC连接:http://forum.90sec.org/forum.php?mod=viewthread&tid=8059 --------------------------- ...

  4. MYSQL基础笔记(六)- 数据类型一

    数据类型(列类型) 所谓数据烈性,就是对数据进行统一的分类.从系统角度出发时为了能够使用统一的方式进行管理,更好的利用有限的空间. SQL中讲数据类型分成三大类:1.数值类型,2.字符串类型和时间日期 ...

  5. MYSQL基础笔记(五)- 练习作业:站点统计练习

    作业:站点统计 1.将用户的访问信息记录到文件中,独占一行,记录IP地址 <?php //站点统计 header('Content-type:text/html;charset=utf-8'); ...

  6. MYSQL基础笔记(四)-数据基本操作

    数据操作 新增数据:两种方案. 1.方案一,给全表字段插入数据,不需要指定字段列表,要求数据的值出现的顺序必须与表中设计的字段出现的顺序一致.凡是非数值数据,到需要使用引号(建议使用单引号)包裹. i ...

  7. MYSQL基础笔记(三)-表操作基础

    数据表的操作 表与字段是密不可分的. 新增数据表 Create table [if not exists] 表名( 字段名 数据类型, 字段名 数据类型, 字段n 数据类型 --最后一行不需要加逗号 ...

  8. MYSQL基础笔记(二)-SQL基本操作

    SQL基本操作 基本操作:CRUD,增删改查 将SQL的基本操作根据操作对象进行分类: 1.库操作 2.表操作 3.数据操作 库操作: 对数据库的增删改查 新增数据库: 基本语法: Create da ...

  9. MYSQL基础笔记(一)

    关系型数据库概念: 1.什么是关系型数据库? 关系型数据库:是一种建立在关系模型(数学模型)上的数据库 关系模型:一种所谓建立在关系上的模型. 关系模型包含三个方面: 1.数据结构:数据存储的问题,二 ...

随机推荐

  1. 我的第一个开源项目 Kiwis2 Mockserver

    我的第一个开源作品Kiwis2 Mock Server,目前公测中,欢迎大家提供宝贵意见. 代码:https://github.com/kiwis2/mockserver 主页:https://kiw ...

  2. 日志导致jvm内存溢出相关问题

    生产环境日志级别为info,请看如下这行代码: LOGGER.debug("the DTO info: {}", JSON.toJSONString(DTO)); 这段代码主要有两 ...

  3. C# CS0050 可访问性不一致: 返回类型 错误

    今天学习C#代码过程中,遇到可访问性不一致的错误: 严重性 代码 说明 项目 文件 行 禁止显示状态错误 CS0050 可访问性不一致: 返回类型"Transaction"的可访问 ...

  4. Skywalking-07:OAL原理——解释器实现

    OAL 解释器实现 OAL 解释器是基于 Antlr4 实现的,我们先来了解下 Antlr4 Antlr4 基本介绍 Antlr4 使用案例 参考Antlr4的使用简介这篇文章,我们实现了一个简单的案 ...

  5. Set重写hashCode和equals方法实现引用对象去重

    运作原理: 首先判断hashCode是否相同,如果不同,直接判定为两个不同的对象.如果hashCode相同,再去比较equals是否一样,如果一样,则为同一个对象.如果不一样,则是两个不同对象. 那么 ...

  6. PostgreSQL隐藏字段

    1)创建了一个表 apple=# \d test_time Table "public.test_time" Column | Type | Modifiers --------+ ...

  7. Blazor+Dapr+K8s微服务之事件发布订阅

    我们要实现的是:在blazorweb服务中发布一个事件,并传递事件参数,然后在serviceapi1服务中订阅该事件,接收到blazorweb服务中发布的事件和参数. 1         在blazo ...

  8. The Second Week lucklyzpp

    The Second Week  文件通配符模式  在Linux系统中预定义的字符类 1.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录 2.复制/etc目录下 ...

  9. redis subscribe/publish(发布订阅)

    redis的发布端 package dubbo.wangbiao.project.pubsub; import org.apache.commons.pool2.impl.GenericObjectP ...

  10. 三大操作系统对比使用之·MacOSX

    时间:2018-11-13 整理:byzqy 本篇是一篇个人对Mac系统使用习惯和应用推荐的分享,在此记录,以便后续使用查询! 打开终端: command+空格,调出"聚焦搜索(Spotli ...