繁体   English   中英

打字稿中的逗号是什么意思

[英]What does the comma mean in typescript

什么是,什么意思? 是不是一样的| 它可以是第一种类型还是任何类型?

CustomFieldValue<Sometype, any>

它是类型参数列表 - 非常类似于参数列表。 每个类型参数用逗号分隔。 例如,与

type CustomFieldValue<K, V> = Map<K, V>;
type mapOfStringsByNumbers = CustomFieldValue<number, string>

第一个类型参数number对应K,第二个类型参数string对应V,所以结果是一个键是数字,值是字符串的Map的类型。

您的

CustomFieldValue<Sometype, any>

将两个类型参数传递给CustomFieldValueSometypeany 这不是工会。

在您的示例中, CustomFieldValue有两种泛型类型,一种是SomeType ,另一种是any A ,只是一个泛型类型分隔符。 您可以在此处了解有关 Typescript 泛型的更多信息。

具有两个通用字段的类 CustomFieldValue 的示例可能是:

public class CustomFieldValue<T, K> {
    field1: T;
    field2: K;
}

然后,当你想使用它时:

const myValue = new CustomFieldValue<SomeType, any>();
// myValue.field1 is of type SomeType
// myValue.field2 is of type any

一个| 用于定义联合类型,如所描述这里

暂无
暂无

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

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