简体   繁体   English

为什么泛型的类型变量通常被声明为单个字母(例如:<T>)?

[英]Why are type variables for generics commonly declared as a single letter (e.g.: <T>)?

For every example in the TypeScript generics documentation and in most type definition files I've came across, type variables are declared as a single letter, usually <T> . 对于TypeScript泛型文档中的每个示例以及我遇到的大多数类型定义文件,类型变量都声明为单个字母,通常为<T>

Example: 例:

function identity<T>(arg: T): T {
    return arg;
}

Questions: 问题:

  • why are type variables for generics commonly declared as a single letter? 为什么泛型的类型变量通常被声明为单个字母?
    • what are the reasons for doing this? 这样做的原因是什么?
    • does it have a purpose other than convention and brevity? 它有除了惯例和简洁之外的目的吗?
  • what are the pros and cons of using of more descriptive names? 使用更具描述性的名称的利弊是什么?

The Java docs for Generics Types has a really good paragraph about this - and it applies to all languages that supports type variables: 泛型类型的Java文档有一个非常好的段落 - 它适用于所有支持类型变量的语言:

Type Parameter Naming Conventions 键入参数命名约定

By convention, type parameter names are single, uppercase letters. 按照惯例,类型参数名称是单个大写字母。 This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name. 这与您已经了解的变量命名约定形成鲜明对比,并且有充分的理由:如果没有这种约定,就很难区分类型变量和普通类或接口名称。

The most commonly used type parameter names are: 最常用的类型参数名称是:

.... ....

T - Type T型

Basically you want to be able to quickly recognize that something is a type variable, and naming the type variable T just became a standard code convention at some point. 基本上你希望能够快速识别某些东西是一个类型变量,并且命名类型变量T在某些时候才成为标准代码约定。

Microsoft's docs on C++ and C# has this convention too. 微软关于C ++C#的文档也有这个约定。

暂无
暂无

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

相关问题 如何访问嵌套类型:K extends keyof T, T[K], eg T[K]["someSubField"] - How to access nested type of: K extends keyof T, T[K], e.g. T[K]["someSubField"] 为什么某些npm库(例如Facebook SDK)的TypeScript类型定义没有导出? - Why do TypeScript type definitions for some npm libraries (e.g. Facebook SDK) have no exports? 类型定义:特定类型的数组,至少包含2个元素 - Type definition: An array of a certain type at least with e.g. 2 elements React Typescript 泛型类型重载(例如 useState) - React Typescript Generic Type Overloading (e.g. useState) 如果不在内部保存,为什么不收集 Observable,例如在类变量中 - Why Observable is not collected if not saved internally, e.g. in a class variable Angular `HttpClient` `.get()` 泛型可以有非简单的属性类型吗? (例如,除了 `string` 或 `number`) - Can Angular `HttpClient` `.get()` generics have non-simple property types? (e.g. other than `string` or `number`) 如何在 Typescript 中指定通用 class 类型的类型? (例如 typeof (AppResponse<string> ))</string> - How to specify the type of generic class type in Typescript? (e.g. typeof (AppResponse<string>)) 可选链接在一行中使用变量访问两级深度? 例如`a[b]?[c]?.d` - Optional chaining access two level deep using variables in one line? E.g. `a[b]?[c]?.d` Typescript联合类型数组作为数组函数参数,例如在$ q.all中 - Typescript union type arrays as array function parameters, e.g. in $q.all 类变量的正确类型(例如 fArr = Uint32Array) - Correct type for class variable (e.g. fArr = Uint32Array)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM