如何阅读TypeScript的复杂interface?-灵析社区

MastFancy

看到一个interface的声明,非常复杂: interface PromiseLike { /** * Attaches callbacks for the resolution and/or rejection of the Promise. * @param onfulfilled The callback to execute when the Promise is resolved. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of which ever callback is executed. */ then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): PromiseLike; } 1、这个很难看懂,请问一下,应该如何学习这个定义呢? 是否可以拆分一点一点地学到它的含义? 2、需要带着什么样的思路才能去看懂,从想要定义它的目的去阅读吗?

阅读量:135

点赞量:0

问AI
最简单的方法是,写实际的例子去进行对比,当然前提是熟悉 promise 怎么使用。 type IsEqual = T1 extends T2 ? (() => G extends T1 ? 1 : 2) extends () => G extends T2 ? 1 : 2 ? true : false : false type AnyIsEqual = T1 extends T2 ? IsEqual extends true ? true : never : never type BrowserNativeObject = Date | FileList | File type Primitive = null | undefined | string | number | boolean | symbol | bigint type PathImpl = V extends Primitive | BrowserNativeObject ? `${K}` : true extends AnyIsEqual ? `${K}` : `${K}` | `${K}.${PathInternal}` type TupleKeys> = Exclude type ArrayKey = number type PathInternal = T extends ReadonlyArray ? IsTuple extends true ? { [K in TupleKeys]-?: PathImpl; }[TupleKeys] : PathImpl : { [K in keyof T]-?: PathImpl; }[keyof T] type Path = T extends any ? PathInternal : never 这个例子看起来就非常复杂,但是如果写一个例子放上去,很容易就知道这是做什么,如果不写例子估计看半天都不知道干什么的