简体   繁体   English

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

[英]What does the comma mean in typescript

What does the , mean?什么是,什么意思? Is it the same as |是不是一样的| which is it can be either the first type or any?它可以是第一种类型还是任何类型?

CustomFieldValue<Sometype, any>

It's the list of type parameters - pretty similar to an argument list.它是类型参数列表 - 非常类似于参数列表。 Each type parameter is separated by a comma.每个类型参数用逗号分隔。 For example, with例如,与

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

The first type parameter number corresponds to K, and the second type parameter string corresponds to V, so the result is the type of a Map whose keys are numbers and values are strings.第一个类型参数number对应K,第二个类型参数string对应V,所以结果是一个键是数字,值是字符串的Map的类型。

Your您的

CustomFieldValue<Sometype, any>

is passing two type parameters to CustomFieldValue : Sometype and any .将两个类型参数传递给CustomFieldValueSometypeany It's not a union.这不是工会。

In your example, CustomFieldValue has two generic types one is SomeType and the other is any .在您的示例中, CustomFieldValue有两种泛型类型,一种是SomeType ,另一种是any A , is simply a generic type separator. A ,只是一个泛型类型分隔符。 You can learn more about Typescript Generics here .您可以在此处了解有关 Typescript 泛型的更多信息。

An example of a class CustomFieldValue with two generic fields could be:具有两个通用字段的类 CustomFieldValue 的示例可能是:

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

Then, when you want to use it:然后,当你想使用它时:

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

A |一个| is used to define union types, as described here用于定义联合类型,如所描述这里

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

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