typescript:什么是异步函数定义语法



我是打字脚本的新手,我正在努力解决一些基本的语法问题。也许这些都是非常基本的,以至于我找不到一个好的指南/教程来解释这些东西。

这是代码:

export const createEntity = async <T extends EntityConstructor>(
Constructor: T,
input: Partial<InstanceType<T>>,
): Promise<InstanceType<T>> => {
const instance = Constructor.create(input);
return validateAndSaveEntity(instance as InstanceType<T>);
};
  1. < >async关键字之后做什么
  2. 我看到( )< >关键字后面的代码指定了输入变量及其类型
  3. < >Promise< >中做什么
  1. <T extends EntityConstructor>是一个类型定义,但编译器根据给定的变量例如声明类型
// type of output will be 'string'
let output = identity<string>("myString");  

// type of output will be 'string', the compiler will figure out `T`
// based on the value passed in
let output = identity("myString");  

// type of output will be 'number'
let output = identity(8675309);  

在给定的memans的情况下,<T extends EntityConstructor>很可能会采用与EntityConstructor具有相同参数的对象,并且可以获得声明的其他变量

  1. 对此我不确定

我认为,它将期望函数通过具有或需要这些参数的可观察对象传递。

  1. Promise类型是在< >中声明的,因此您可以执行Promise<string>,并且任何不是字符串的东西都不会作为Promise传递

最新更新