我是打字脚本的新手,我正在努力解决一些基本的语法问题。也许这些都是非常基本的,以至于我找不到一个好的指南/教程来解释这些东西。
这是代码:
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>);
};
< >
在async
关键字之后做什么- 我看到
( )
中< >
关键字后面的代码指定了输入变量及其类型 < >
在Promise< >
中做什么
<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
具有相同参数的对象,并且可以获得声明的其他变量
- 对此我不确定
我认为,它将期望函数通过具有或需要这些参数的可观察对象传递。
- Promise类型是在
< >
中声明的,因此您可以执行Promise<string>
,并且任何不是字符串的东西都不会作为Promise传递