[英]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.