简体   繁体   English

了解Typescript中的复杂类型声明

[英]Understanding complex type declaration in Typescript

Looking at a this type declaration: 查看此类型声明:

export interface Thenable<R> {
    then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>
    then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>
    catch<U>(onRejected?: (error: any) => U | Thenable<U>): Thenable<U>
}

I'm aware of what it does. 我知道它的作用。 I can do something like this: 我可以做这样的事情:

type MessagesSendResponse = ...
export const announce = (options: sendParameters):Thenable<MessagesSendResponse> => {
 return Promise.all([emailMessage(options),smsMessage(options)] 
}

This makes it smart enough to know that in context 这使得它足够聪明,可以了解上下文

const val1 = announce(..)

that val1 is a Thenable/Promise whereas in this context val1是Thenable / Promise,而在这种情况下

const val2 = await announce(..)

val2 is of type MessagesSendResponse val2的类型为MessagesSendResponse

My question is that I don't understand the following about the Thenable interface: 我的问题是我不了解有关Thenable接口的以下内容:

  1. What does it mean to say Thenable<U> I understand that U is a generic type, yet what does Thenable<U> mean? Thenable<U>是什么意思我知道U是泛型类型,那么Thenable<U>是什么意思? What's another way to write that? 还有什么写方法呢?

  2. Somehow this definition is saying that a function returns a thenable / promise that in turn returns a generic. 这个定义以某种方式说一个函数返回一个thenable / promise,然后返回一个泛型。 Yet both the interface type and the return value for both then and catch are of type Thenable<U> . 然而,接口类型以及thencatch的返回值均为Thenable<U>类型。 It looks like its saying that it returns itself, which I suppose is right since a thenable can return another thenable, but how does it know that it know that the resolution is MessagesSemdResponse if it says it returns Thenable<U> ? 好像它说的是返回自身,我认为这是正确的,因为thenable可以返回另一个thenable,但是如果它说它返回Thenable<U> ,它怎么知道它知道解析为MessagesSemdResponse呢? Is there some build in feature in the IDE for this? IDE中是否为此有一些内置功能?

I realize question 2 reflect my confusion. 我意识到问题2反映了我的困惑。 Any links to references appreciated, I couldn't find anything similar to this pattern. 任何引用的链接都值得赞赏,我找不到与该模式相似的东西。

Thenable<T> means that you have an object from which you can obtain a value of type T . Thenable<T>表示您有一个对象,可以从中获取类型T的值。
Or, if naming it Promise<T> you get a promise to a value of type T . 或者,如果将其命名为Promise<T>可以得到类型T的值的保证。

Because promises are usually used for asynchronous operations, the promise doesn't "return a value" but gives you an api for getting a reference to that value when it is available. 因为promise通常用于异步操作,所以promise不会“返回值”,而是为您提供了一个在可用时获取对该值的引用的api。

The reason that then / catch return Thenable<T> is so that you can chain the calls: then / catch返回Thenable<T>是可以链接调用:

announce(...).then(value => {
    ...
}).catch(error => {
    ...
});

The function that you are passing to then / catch will be called upon when the value is available or when something went wrong. 当值可用或出现问题时,将调用传递给then / catch的函数。

The promise that is being returned by then / catch is not the same promise that you called the function on, it's a new instance, and the compiler infers the generic type of this new promise based on the return value of the function you passed, for example: then / catch返回的promise与调用该函数的promise不同,它是一个新实例,并且编译器根据您传递的函数的返回值来推断此新promise的泛型类型,例如例:

const fn = (): Promise<string> => {
    return Promise.resolve("43"); 
}

fn().then(str => Number(str)).then(num => console.log(num))

The compiler knows that str is of type string and that num is of type number . 编译器知道strstring类型,而numnumber类型。

You can read more about this process here: promise chaining in MDN 您可以在此处阅读有关此过程的更多信息: MDN中的Promise链接


Edit 编辑

In the signature: 在签名中:

then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U> 

onFulfilled : onFulfilled
A function that expects a value of type R and returns either a value of type U or a Thenable to U . 一个期望值为R的值并返回U值或ThenableUThenable

onRejected : onRejected
A function that expects a value of type any and returns either a value of type U or a Thenable to U . 一个函数,该函数期望类型为any的值,并向U返回类型为UThenable的值。

returns : 返回
A instance of Thenable for a value of type U , which is the result of the execution of either onFulfilled or onRejected . U值类型为Thenable的实例,该值是onFulfilledonRejected执行的结果。

part1: 第1部分:

export interface Thenable<R> {

we are defining generic interface with one type parameter R 我们正在使用一种类型参数R定义通用接口

part2: 第2部分:

then<U>(onFulfilled?: ...,  onRejected?: ...): ...

it has method then , which takes two parameters onFulfilled and onRejected , and the method itself is generic - it depends on another type paramerer U . then它具有方法,该方法带有两个参数onFulfilledonRejected ,该方法本身是通用的-它取决于另一个类型的参数U

part3, the most interesting one: 第3部分,最有趣的一个:

onFulfilled is declared with this type: 使用以下类型声明onFulfilled

(value: R) => U | Thenable<U>

it means that it's either a function taking R and returning U , or another Thenable<U> 这意味着它要么是使用R并返回U的函数,要么是另一个Thenable<U>

Here is a relationship between R and U : if you have Thenable<R> , it's then method accepts a callback which is called with a value of type R (the one that original thenable produced), and should return U . 这是RU之间的关系:如果您具有Thenable<R>then方法接受一个以R类型(原始thenable产生的值)调用的回调,并应返回U Or it can accept another Thenable<U> . 或者它可以接受另一个Thenable<U>

The return value of this then method, then, is Thenable<U> . 然后,此then方法的返回值是Thenable<U>

In short, this is how chaining of promises is described with types. 简而言之,这就是如何用类型描述承诺链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM