zoukankan      html  css  js  c++  java
  • typescript里一些有趣的点

    联合类型

    在原生的JS里,null和undefined经常会导致BUG的产生,
    在ts里,你又想用null,又担心出错的时候
    你可以考虑用联合类型,当某值可能为 number或null,你可以声明它的类型为number | null

    let a : number | null = 2;

    类型兼容

    实现接口时,只要包含了接口要求的数据结构即可兼容这个接口

    interface Person {
        firstName: string;
        lastName: string;
    }
    function greeter(person: Person) {
        return "Hello, " + person.firstName + " " + person.lastName;
    }
    let user = { firstName: "Jane", lastName: "User" };
    document.body.innerHTML = greeter(user);

    面向对象

    继承( extends关键字 )
    public private  protected  static  readonly get和set  都有,连namespace也有;
    抽象类( abstract关键字 )

    关于this

    let deck: Deck = {
        // NOTE: The function now explicitly specifies that its callee must be of type Deck
        createCardPicker: function(this: Deck) {
            return () => {
               console.log(this)
            }
        }
    }

    指明this的类型必须是Deck

    泛型

    function identity<T>(arg: T): T {
        return arg;
    }
    let output = identity<string>("myString");  // type of output will be 'string'
    //或者
    let output = identity("myString");  // type of output will be 'string'

    泛型方法、泛型类、泛型约束都有



     

  • 相关阅读:
    ajax工作原理
    ajax 和xmlHttpRequest区别
    ajax 基本语法
    javascript 中XMLHttpRequest 实现前台向后台的交互
    Javascript 中ajax实现前台向后台交互
    javascript 中函数eval()
    两道有趣的面试题
    linux 中的快捷键
    linux awk命令详解
    linux sed命令详解
  • 原文地址:https://www.cnblogs.com/liulun/p/11044436.html
Copyright © 2011-2022 走看看