简体   繁体   English

打字稿:这个函数的主体在哪里定义,泛型真的被使用了吗?

[英]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:任何人都可以回答以下问题:

  • (A) Where is the body of the function defined? (A) 函数体在哪里定义? I tried the code and played a bit around, changing the type and amount of parameters, for example: 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.在每种情况下,都返回了 FIRST 参数的值(在最后一种情况下它是一个空输出),但我在任何地方都没有看到这样的函数定义。
  • (B) I see that in line 19 the Promise is defined with <string> . (B) 我看到在第 19 行中, 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?尽管如此,如 (A) 所示,我能够传递和返回字符串、数字、布尔值……也许我误解了这种类型应该做什么?

Thanks!谢谢!

A couple of things to address here:这里有几件事要解决:

(A) resolve is a specific function to Promises along with the function reject . (A) resolvePromises一个特定函数以及函数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.告诉编译器此时该值不为nullundefined

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:变化是:

  • removing the null and null initialization:删除nullnull初始化:
    • there is no reason to initialize the value with null ,没有理由用null初始化值,
    • the constructor calls this.reset() which set those properties immediately.构造函数调用this.reset()立即设置这些属性。 So they will not be null.所以它们不会为空。
    • always use undefined if you really need to (JavaScript should not have two bottom values to begin with).如果确实需要,请始终使用undefined (JavaScript 不应以两个底值开头)。
    • now you can skip the !现在你可以跳过! in this.resolve!()this.resolve!()
  • private resolve: (string) => void : private resolve: (string) => void
    • always be specific when possible.尽可能具体。

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

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