[英]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
接口的以下内容:
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? 还有什么写方法呢?
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>
. 然而,接口类型以及
then
和catch
的返回值均为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
. 编译器知道
str
是string
类型,而num
是number
类型。
You can read more about this process here: promise chaining in MDN 您可以在此处阅读有关此过程的更多信息: MDN中的Promise链接
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
值或Thenable
到U
的Thenable
。
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
返回类型为U
或Thenable
的值。
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
的实例,该值是onFulfilled
或onRejected
执行的结果。
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
它具有方法,该方法带有两个参数onFulfilled
和onRejected
,该方法本身是通用的-它取决于另一个类型的参数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
. 这是
R
和U
之间的关系:如果您具有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.