为什么我们在泛型中使用字母 T,U

Why do we use the letters T,U in generics

我注意到大多数 tutorials/courses/articles 使用 T,U... 作为泛型类型的名称。确切地说,为什么不指定普通名称呢?

这只是一个约定;不同种类的事物具有明显不同的名称格式,因此您可以根据其名称的书写格式来判断事物的种类。例如,在 Java、Javascript 和 Typescript 中,通常使用camelCase 用于变量和方法名称,UpperCamelCase 用于 class 名称,UPPER_SNAKE_CASE 用于常量;并且通常使用 T 作为类型参数。

这样,如果您看到一个名为 T 的类型,您就知道它是一个类型参数,而如果您看到一个名为 Person 的类型,您就知道它不是。如果您看到 person,您就知道它不是一种类型。能够区分 class 和类型参数是有用的,因为 type erasure; classes 在运行时存在,但类型参数在编译期间从代码中删除。 (这在 Java 中比在 Typescript 中更适用,因为在 Java 中只有 类型参数被删除,而在 Typescript 中接口和类型别名也被删除。但是约定来自 Java 和类似的语言,所以它一直存在。)

最有可能的是,默认使用的字母是 T,因为它代表“类型”,然后就像在数学中一样,通常使用连续的字母表示表示相同种类事物的多个变量,所以STUV很常见。此外,K 通常用于通用键类型,而 V 用于与之关联的值类型;在某些情况下,E 用于集合中“元素”的类型。

I noticed most tutorials/courses/articles use T,U... as the name of generic types. Why exactly, why not assign normal names instead?

主要是因为它们是平凡而糟糕的例子。

正如我在下面提到的,在大多数情况下,最好在某些内容前加上 T 而不是只说 T Array<T> 是真正开放的典范。如果您要说,编写您自己的 .Net's Dictionary 版本,它包含键值对,那么您会明白为什么您可以在下面的 接口 示例中更加明确。

Dictionary<T,U>

// or

Dictionary<TKey, TValue>

摘自我自己对一个非常相似问题的回答:

'T' is going to be a type declared at run-time instead of compile time. The T variable could be any non-declared variable (I couldn't find a reference, but I would assume any valid set of characters that could be used for a variable names). Similarly in , if the type T represents is not a value type but a more complex type (class) or interface, it could be named/declared as TVehicle or TAnimal to help denote a valid type for future programmers (and could be considered best practice because just T is not intuitive). I prefer TSomething because I know that uppercase T means a generic type. WSometing or ASomething is also valid, but I just don't prefer it. (Microsofts APIs are almost always TContext or TEntity for example).

You can explain to me why in the Typescript documentation they put <T> instead of putting something more descriptive like <Identity> for example. Nothing and <T> for me is the same. Now does everyone use the <T> like fools, or did I miss something?

These are all going to be assumptions in the following. I do not know neither the team who designed the typescript generic system nor the team who wrote the documentation.

At the root level of generics is the ability to use T as any possible type (not to be confused with typescript any). Meaning Array<T> is the interface (for lack of a better word) and when we create a concrete type we replace T with a declared type:

Array<number>

So for the interface Array<T> what makes more sense than T? I don't know. I do know that T has to be a Type (number, string, etc) so it makes sense to use T because it the first letter of the word Type. I think Array<Type> would be really confusing and/or might even be invalid if type or Type became reserved or restricted (currently type has special meaning in certain contexts so it's also a poor choice) so avoiding those is a good choice. Other languages (C-sharp, Java) also choose to use T, so switching between languages and being able to use the same term is advantageous.