zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    TypeScript & LeetCode

    TypeScript In Action

    TypeScript 复杂类型

    编写复杂的 TypeScript 类型

    // 方法「只可能」有两种类型签名
    
    interface Action<T> {
      payload?: T
      type: string
    }
    
    asyncMethod<T, U>(input: Promise<T>): Promise<Action<U>>
    
    syncMethod<T, U>(action: Action<T>): Action<U>
    
    
    interface Action<T> {
      payload?: T;
      type: string;
    }
    
    // 还可能有一些任意的「非函数属性」
    
    class EffectModule {
      count = 1;
      message = "hello!";
    
      delay(input: Promise<number>) {
        return input.then((i) => ({
          payload: `hello ${i}!`,
          type: "delay",
        }));
      }
    
      setMessage(action: Action<Date>) {
        return {
          payload: action.payload!.getMilliseconds(),
          type: "set-message",
        };
      }
    }
    
    
    // 现在有一个叫 connect 的函数,它接受 EffectModule 实例,将它变成另一个对象,
    // 这个对象上只有「EffectModule 的同名方法」,但是方法的类型签名被改变了
    
    asyncMethod<T, U>(input: Promise<T>): Promise<Action<U>> 
    // 变成了
    asyncMethod<T, U>(input: T): Action<U>
    
    syncMethod<T, U>(action: Action<T>): Action<U> 
    // 变成了
    syncMethod<T, U>(action: T): Action<U>
    
    

    demo

    interface Action<T> {
      payload?: T;
      type: string;
    }
    
    class EffectModule {
      count = 1;
      message = "hello!";
    
      delay(input: Promise<number>) {
        return input.then((i) => ({
          payload: `hello ${i}!`,
          type: "delay",
        }));
      }
    
      setMessage(action: Action<Date>) {
        return {
          payload: action.payload!.getMilliseconds(),
          type: "set-message",
        };
      }
    }
    
    
    
    type Connected = {
      delay(input: number): Action<string>;
      setMessage(action: Date): Action<number>;
    };
    
    const effectModule = new EffectModule();
    const connected: Connected = connect(effectModule);
    
    

    要求:

    在题目链接[1] 里面的 index.ts 文件中,
    有一个 type Connect = (module: EffectModule) => any,将 any 替换成题目的解答,让编译能够顺利通过,
    并且 index.ts 中 connected 的类型与 Connected「完全匹配」

    type Connected = {
      delay(input: number): Action<string>;
      setMessage(action: Date): Action<number>;
    };
    
    

    https://github.com/LeetCode-OpenSource/hire/blob/master/foundations-zh/src/index.ts

    
    export interface ErrorMessage {
      message: string
      stack: Array<{
        line: number
        column: number
        filename: string
      }>
    }
    
    export function parseError(err: Error): ErrorMessage {
      // implement
    }
    
    

    solution

    
    

    refs

    LeetCode 面试题

    https://github.com/LeetCode-OpenSource/hire

    https://mp.weixin.qq.com/s/dWF9bPd9QhgsnuAHX33bNQ

    https://github.com/LeetCode-OpenSource/hire/blob/master/foundations_zh.md

    https://github.com/LeetCode-OpenSource/hire/blob/master/typescript_zh.md



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    前中后序建立树或者直接历遍
    Leetcode:面试题 04.03. 特定深度节点链表
    按层数层序历遍
    Solidity函数修饰符
    无线传感网定位技术
    无线传感器网络概述,传感器网络结构
    Solidity高级用法
    智能合约交互
    内存
    CPU的态
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13611172.html
Copyright © 2011-2022 走看看