[英]Typescript: where is the body of this function defined and are the generics really used?
I was having a look at the documentation of Angular's "AsyncPipe" and I got kind of stuck in line 26, where the ' resolve ' function is called: this.resolve !('hi there!');
我正在查看Angular 的“AsyncPipe”的文档,但我有点卡在第 26 行,其中调用了 ' resolve ' 函数:
this.resolve !('hi there!');
Could anyone answer the following questions:任何人都可以回答以下问题:
resolve(1, 'hello', true)
, or resolve(1)
, or resolve()
.resolve(1, 'hello', true)
或resolve(1)
或resolve()
。 In every case, the value of the FIRST parameter was returned (it was an empty output in the last case), but I do not see anywhere such a function definition.Promise
is defined with <string>
. Promise
是用<string>
定义的。 Nonetheless, as shown in (A), I was able to pass and return strings, numbers, booleans... perhaps I misunderstood what this typing is suposed to do? Thanks!谢谢!
A couple of things to address here:这里有几件事要解决:
(A) resolve
is a specific function to Promises
along with the function reject
. (A)
resolve
是Promises
一个特定函数以及函数reject
。 They are your asynchronous functions to determine what to do with the result ( resolve
) or the error ( reject
).它们是您的异步函数,用于确定如何处理结果 (
resolve
) 或错误 ( reject
)。 The resolve
function returns the first parameter and nothing else, if you need to return more than one value it can be done in an object. resolve
函数只返回第一个参数,不返回其他参数,如果需要返回多个值,可以在一个对象中完成。
(B) Typescript only exists in the IDE/code editor, not at runtime. (B) Typescript 仅存在于 IDE/代码编辑器中,而不存在于运行时。 Typescript at runtime is transpiled down to JavaScript and a lot of the functionality (generics included) are lost in the browser.
运行时的 Typescript 被转换为 JavaScript,并且许多功能(包括泛型)在浏览器中丢失了。 The generics are there to help you when developing your code so you don't run a Number function on a String, or some similar idea.
泛型可以在开发代码时为您提供帮助,这样您就不会在字符串上运行 Number 函数或一些类似的想法。 So when you get to running the Angular application you'll be able to send in anything you want, but in development your IDE should warn you if the types mismatch (especially if you are using
tslint
with your development tools)因此,当您开始运行 Angular 应用程序时,您将能够发送任何您想要的内容,但在开发过程中,您的 IDE 应该在类型不匹配时向您发出警告(尤其是当您在开发工具中使用
tslint
时)
The this.resolve
function is assigned at line 19: this.resolve
函数在第 19 行赋值:
this.greeting = new Promise<string>((resolve, reject) => { this.resolve = resolve; });
The Promise<string>
only defines the variable resolve
should accept a string argument (ie resolve: (string) => void
). Promise<string>
只定义了变量resolve
应该接受一个字符串参数(即resolve: (string) => void
)。
this.resolve
is defined in line 13: this.resolve
在第 13 行定义:
private resolve: Function|null = null;
Because it is defined with type Function|null
, it does not restrict on the number of arguments it should be called with nor the type of those arguments.因为它是用类型
Function|null
定义的,所以它不限制调用它的参数数量,也不限制这些参数的类型。
It is referenced with !
它被引用
!
(in this.resolve!(...)
) because its type has null
in it, and the !
(在
this.resolve!(...)
)因为它的类型中有null
,而!
tells the compiler that the value is not null
or undefined
at that moment.告诉编译器此时该值不为
null
或undefined
。
The example is not that well written.这个例子写得不是很好。 It could be improved like this:
可以这样改进:
export class AsyncPromisePipeComponent {
greeting: Promise<string>;
arrived: boolean = false;
private resolve: (string) => void;
constructor() { this.reset(); }
reset() {
this.arrived = false;
this.greeting = new Promise<string>((resolve, reject) => { this.resolve = resolve; });
}
clicked() {
if (this.arrived) {
this.reset();
} else {
this.resolve('hi there!');
this.arrived = true;
}
}
}
The changes are:变化是:
null
and null
initialization:null
和null
初始化:
null
,null
初始化值,this.reset()
which set those properties immediately.this.reset()
立即设置这些属性。 So they will not be null.undefined
if you really need to (JavaScript should not have two bottom values to begin with).undefined
(JavaScript 不应以两个底值开头)。!
!
in this.resolve!()
this.resolve!()
private resolve: (string) => void
: private resolve: (string) => void
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.