[英]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>
将两个类型参数传递给CustomFieldValue
: Sometype
和any
。 这不是工会。
在您的示例中, 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.