• 深入理解TypeScript——文档篇之类型推断


    一、基础

    TypeScript里,在有些没有明确指出类型的地方,类型推论会帮助提供类型

    let x = 3; // let x: number
    

    二、最佳通用类型

    计算通用类型算法会考虑所有的候选类型,并给出一个兼容所有候选类型的类型。

    // demo 1
    let x = [0, 1, null, 'haha']; // let x: (string | number | null)[]
    
    // demo 2
    class Rhino {
      constructor() {
        
      }
    }
    
    class Elephant {
      constructor() {
        
      }
    }
    
    class Snake {
      constructor() {
        
      }
    }
    
    let zoo = [new Rhino(), new Elephant(), new Snake()]; // let zoo: (Rhino | Elephant | Snake)[]
    

    三、上下文类型

    TypeScript类型推论也可能按照相反的方向进行。 这被叫做“按上下文归类”。按上下文归类会发生在表达式的类型与所处的位置相关时。
    TypeScript类型检查器使用Window.onmousedown函数的类型来推断右边函数表达式的类型。因此,就能推断出 mouseEvent参数的类型了。
    如果函数表达式不是在上下文类型的位置, mouseEvent参数的类型需要指定为any,这样也不会报错了。

    window.onmousedown = function(mouseEvent) { // (parameter) mouseEvent: any
    
        console.log(mouseEvent.button);  //<- Error
    };
    

    注:如果上下文类型表达式包含了明确的类型信息,上下文的类型被忽略。

    // test 1
    window.onmousedown = function(mouseEvent: any) {
        console.log(mouseEvent.button);  //<- Now, no error is given
    };
    
    // test 2
    interface Name {
      button: string;
    }
    
    function App() {
      window.onmousedown = function(mouseEvent: Name) { // TypeScript类型检查器使用Window.onmousedown函数的类型来推断右边函数表达式的类型。 因此,就能推断出 mouseEvent参数的类型了。 
        console.log(mouseEvent.button);  //<- Error
    };
    // 不能将类型“(mouseEvent: Name) => void”分配给类型“((this: //GlobalEventHandlers, ev: MouseEvent) => any) & ((this: Window, ev: MouseEvent) => any)”。
    // 不能将类型“(mouseEvent: Name) => void”分配给类型“(this: GlobalEventHandlers, ev: MouseEvent) => any”。
    // 参数“mouseEvent”和“ev” 的类型不兼容。
    // 不能将类型“MouseEvent”分配给类型“Name”。
    // 属性“button”的类型不兼容。
    
  • 相关阅读:
    celery异步任务
    redis过期策略与内存淘汰机制分析
    爬虫数据去重-布隆过滤器
    scrapy-redis数据去重与分布式框架
    redis哨兵机制
    C#从零单排上王者系列---元组
    玩转VSCode插件之Remote-SSH
    C#从零单排上王者系列---数据类型
    Cassandra之Docker环境实践
    centos7安装nginx并配置前端环境
  • 原文地址:https://www.cnblogs.com/hackftz/p/13811810.html
Copyright © 2020-2023  润新知