Amadeus's blog Amadeus's blog
首页
  • 前端文章

    • JavaScript
    • Vue
    • TypeScript
    • 前端工程化
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • 小程序笔记
  • HTML
  • CSS
  • stylus
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 口语
  • 音标
  • 语法
  • 简单
  • 中等
  • 困难
  • 20年10月
  • 20年11月
  • 20年12月
  • 21年01月
  • 21年02月
  • 21年03月
  • 21年04月
  • 21年05月
  • 21年06月
  • 21年07月
  • 21年08月
  • 21年09月
  • 21年10月
  • 21年11月
  • 21年12月
  • 22年01月
  • 22年02月
  • 22年03月
  • 22年04月
  • 22年05月
  • 22年06月
  • 22年07月
  • 22年08月
  • 22年09月
  • 21年3月
  • 知识笔记
  • 22年5月
  • 22年8月
  • 22年9月
  • 学习
  • 书法
  • 面试
  • 音乐
  • 驾照
  • 深度强化学习
  • 心情杂货
  • 友情链接
关于
  • 网站
  • 资源
  • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Amadeus

起风了,唯有努力生存!
首页
  • 前端文章

    • JavaScript
    • Vue
    • TypeScript
    • 前端工程化
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • 小程序笔记
  • HTML
  • CSS
  • stylus
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 口语
  • 音标
  • 语法
  • 简单
  • 中等
  • 困难
  • 20年10月
  • 20年11月
  • 20年12月
  • 21年01月
  • 21年02月
  • 21年03月
  • 21年04月
  • 21年05月
  • 21年06月
  • 21年07月
  • 21年08月
  • 21年09月
  • 21年10月
  • 21年11月
  • 21年12月
  • 22年01月
  • 22年02月
  • 22年03月
  • 22年04月
  • 22年05月
  • 22年06月
  • 22年07月
  • 22年08月
  • 22年09月
  • 21年3月
  • 知识笔记
  • 22年5月
  • 22年8月
  • 22年9月
  • 学习
  • 书法
  • 面试
  • 音乐
  • 驾照
  • 深度强化学习
  • 心情杂货
  • 友情链接
关于
  • 网站
  • 资源
  • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • TypeScript笔记

    • TypeScript是什么
    • 前端工程化

    • 面试

    • 小程序

    • Vue3源码解析

    • 设计模式

    • NestJS笔记

    • JavaScript文章

    • Vue文章

    • 学习笔记

    • 前端
    • TypeScript笔记
    Amadeus
    2022-11-21
    目录

    TypeScript是什么

    # 1.什么是TypeScript

    # 2.JS、ES及TS的关系

    # 3.TS优化编译、降级编译等

    # 4.基元类型string、number和boolean

    # 5.数组类型的定义

    # 6.Any类型

    # 7.函数给参数和返回值定义类型

    # 8.类型

    ?:指定类型|undefined,可选参数

    ?.判断对象是否存在,存在才调用方法,不存在就不调用

    对象类型 {}

    联合类型 |

    类型别名 type

    类型扩展

    type a = {
        x:number
    }
    type b = a & {
        y:number
    }
    
    1
    2
    3
    4
    5
    6

    # 9.接口

    interface

    interface a {
        x:number
    }
    // 再写一遍即可扩展接口,type不行
    interface a {
        y:number
    }
    // 继承a接口
    interface b extends a {
        z:number
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    # 10.类型断言

    as T

    # 11.枚举

    enum Direction {
        Up = 1,
        Down,
        Left,
        Right
    }
    
    1
    2
    3
    4
    5
    6

    # 12.bigint和symbol

    bigint存储大整数

    symbol创建全局唯一引用

    # 13.typeof类型守卫

    # 14.真值、等值缩小

    Boolean()转换成布尔型

    !!转换成布尔型

    # 15.in操作符缩小

    确定对象是否有某个名称的属性

    # 16.类型谓词

    interface Fish {
        type: string
        swim: () => void
    }
    function isFish(animal: Fish): animal is Fish{
        return (animal as Fish).swim !== undefined
    }
    
    1
    2
    3
    4
    5
    6
    7

    # 17.使用unions的问题

    问题:里面的属性是可选的,所以在代码编写时很容易被ts检测出错误,可以在名称尾部添加!来说明这个属性是必然存在,来阻止ts报错,但是这种方法也不是很可取。

    # 18.never类型帮助穷尽性检查

    声明never类型,表明这个变量是不会具有其他类型,即这个变量的赋值是永远不可达的。用来穷尽类型的枚举。

    interface Circle{
        kind: 'circle'
    }
    interface Square{
        kind: 'square'
    }
    type Shape = Circle | Square
    let shape: Circle = {kind: 'circle'}
    switch(shape.kind):
        case 'circle':
        	return shape.kind
        case 'square':
        	return shape.kind
        default:
        	const type: never = shape
        	return type
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    # 19.函数类型表达式

    // 表示函数的类型
    // 参数类型,返回值类型
    (a: string) => void
    
    1
    2
    3

    # 20.调用签名

    type despFunction = {
        description: string,
        (someArgs: number) : number
    }
    
    function fn(someArgs: number){
        return number
    }
    fn.description = 'hello'
    function doSomething(fn: despFunction){
        console.log(fn.description + ' returned ' + fn(6))
    }
    doSomething(fn)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    # 21.构造签名

    class Ctor {
        constrcutor(someArgs: number){
            this.someArgs = someArgs
        }
    }
    type despConstructor = {
        new (someArgs: number) : Ctor
    }
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 22.泛型函数

    function fn<T>(arg: T[]): T {
        return arg[0]
    }
    fn<string>(['a', 'b'])
    fn<number>([1, 2])
    
    1
    2
    3
    4
    5

    泛型限制条件 extends

    // 要求传入类型必须具有length属性且属性类型是number
    function longest<T extends {length:number}>(a: T, b: T): T {
        if(a.length>b.length){
            return a
        }
        return b
    }
    
    1
    2
    3
    4
    5
    6
    7

    指定类型参数

    function combine<T>(a: T[], b: T[]): T[] {
        return a.concat(b)
    }
    // 传入泛型是联合类型,使泛型类型可多选,不推荐使用
    combine<string|number>(['a','b'],[1,2])
    
    1
    2
    3
    4
    5

    # 23.可选参数

    // 用?:声明可选参数
    function a(x?:number){
        console.log(123)
    }
    
    1
    2
    3
    4

    # 24.函数重载

    1.声明重载函数签名

    function getDate(timestamp: number): Date
    function getDate(m: number, d: number, y: number): Date
    
    1
    2

    2.使用一个函数实现上述多个重载函数签名

    function getDate(mOrTimestamp:number, d?: number, y?: number): Date {
        
    }
    
    1
    2
    3

    用this作为函数参数名

    interface User{
        admin: false
    }
    interface DB {
        filterUsers(filter: (this: User) => boolean): User[]
    }
    function a(this: User): void {
        console.log(this.admin)
    }
    const db:DB = {
        filterUsers(filter: (this: User) => boolean) => {
        
    	}
    }
    const admins = db.filterUsers(function(this: User){
        return this.admin
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    # 25.其他类型

    void: void跟undefined不一样

    object:任何不是基元类型的值,基元类型包括:string、number、bigint、boolean、symbol、null、undefined。object不是Object也不是{}。

    unknown:比any更安全

    never:永远不会被观察到的值

    Function:在类型中没声明输入和返回值,不建议使用。推荐使用()=>void类型

    # 26.参数展开运算符

    形参展开

    function multiply(n: number, ...m: number[]): number[] {
        return m.map( (x) => x * n )
    }
    multiply(10, 1, 2, 3, 4)
    
    1
    2
    3
    4

    实参展开

    const arr1 = [1,2,3]
    const arr2 = [4,5,6]
    arr1.push(...arr2)
    // 将[4,5,6]扩展成4,5,6三个参数传入
    // as const 将args数组固定成常量,则它的长度就确定是声明的两个
    const args = [8, 5] as const
    // atan2的参数只能是2个,没有as const,无法确定args数组长度是两个,ts会报错。
    // 声明了const类型还要使用as const,很奇怪。
    const angle = Math.atan2(...args) 
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 27.参数结构

    function add({a, b, c}: {a: number, b: number, c: number}): number {
        return a + b + c
    }
    add({
        a:1,
        b:2,
        c:3
    })
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 28.只读属性

    在类型声明中,属性名前添加readonly关键字,就可以让属性不被修改

    interface Person {
        readonly name: string
        age: number
    }
    // 如果属性是对象,则依旧可以修改对象中的某一属性,但不能直接对对象整体进行修改
    
    1
    2
    3
    4
    5

    # 29.索引签名

    interface Person {
        [index: number]: string
        number: string
    }
    
    1
    2
    3
    4
    编辑 (opens new window)
    为何要前端工程化

    为何要前端工程化→

    最近更新
    01
    最长递增子序列
    04-21
    02
    非递减子序列
    04-21
    03
    全排列
    04-21
    更多文章>
    Theme by Vdoing | Copyright © 2020-2024 Amadeus | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式