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 发布文章使用:只允许注册用户才可以访问!


TypeScript & LeetCode的更多相关文章

  1. 「LeetCode」0003-Add Two Numbers(Typescript)

    分析 代码 /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers=fu ...

  2. TypeScript 技巧

    前言 很早以前就尝试过使用 TypeScript 来进行日常编码,但自己对静态类型语言的了解并不深入,再加上 TypeScript 的类型系统有着一定的复杂度,因此感觉自己并没有发挥好这门语言的优势, ...

  3. 数据结构篇-数组(TypeScript版+Java版)

    1.TypeScript版本 export default class MyArray<E> { public data: E[]; public size: number = 0; /* ...

  4. TypeScript: Angular 2 的秘密武器(译)

    本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...

  5. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  6. TypeScript为Zepto编写LazyLoad插件

    平时项目中使用的全部是jQuery框架,但是对于做webapp来说jQuery太过于庞大,当然你可以选择jQuery 2.*针对移动端的版本. 这里我采用移动端使用率比较多的zepto框架,他跟jqu ...

  7. TypeScript Vs2013 下提示Can not compile modules unless '--module' flag is provided

    VS在开发TypeScript程序时候,如果import了模块有的时候会有如下提示: 这种情况下,只需要对当前TypeScript项目生成设置为AMD规范即可!

  8. TypeScript

    TypeScript: Angular 2 的秘密武器(译)   本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch? ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. js--数组的find()和findIndex()方法的使用介绍

    前言 阅读本文前先来思考一个问题,面对一个非空数组,你如何快速对数组进行遍历,如何快速找到数组中第一个我们需要关注的数据元素,并且如何知道该元素在数组中对应的下标索引,可能用for循环遍历,然后判断元 ...

  2. Java三种IO模型和LinuxIO五种IO模型

    Java: https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/BIO-NIO-AIO.md https://github.co ...

  3. java实现注销登录

    servlet HttpServletRequest request HttpSession session=request.getSession(); session.removeAttribute ...

  4. Go - 实现项目内链路追踪

    为什么项目内需要链路追踪?当一个请求中,请求了多个服务单元,如果请求出现了错误或异常,很难去定位是哪个服务出了问题,这时就需要链路追踪. 从图中可以清晰的看出他们之间的调用关系,通过一个例子说明下链路 ...

  5. linux 下解决mysql root 权限无法远程连接问题

    问题描述:MySQL数据库安装成功后,在服务器本地可以连接成功,但是使用工具navicat无法进行远程连接,如图: 原因:MySQL默认只允许root帐户在本地登录,如果要在其它机器上连接mysql, ...

  6. Docker容器启动Mysql,Docker实现Mysql主从,读写分离

    Docker容器启动Mysql,Docker实现Mysql主从,读写分离 一.Docker文件编排 二.配置主从复制 2.1 配置master 2.2 配置slave 三.验证主从复制 3.1 mas ...

  7. $().arr使用

    $("#handmoney").attr("readonly","readonly"); 只读 $("img").att ...

  8. 浅聊TCP的三次握手和四次挥手

    三次握手: 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源.Client端接收到ACK报文后也向Server段发生ACK报文,并分配资源,这样TCP连 ...

  9. 深信服上网行为管理配置跨三层MAC识别

    1.在认证高级选项里点击新增 如果PC的IP和MAC存在于多个三层交换机,则需新增多个. 点击上图"查看服务器信息"测试能否从交换机获取PC的IP和MAC,有返回结果则能正常获取, ...

  10. centos 7.6安装WeADMIN ITOSS步骤

    0.升级系统并安装必要的软件 yum -y update yum -y install wget unzip nmap 1.下载JDK jdk下载地址:http://www.oracle.com/te ...