TypeScript中的内在特性是什么?

huangapple go评论51阅读模式
英文:

What's intrinsic in TypeScript?

问题

我在 `lib.es5.d.ts` 中找到了一些类型:

```typescript
/**
 * 获取构造函数类型的返回类型
 */
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;

/**
 * 将字符串字面类型转换为大写
 */
type Uppercase<S extends string> = intrinsic;

/**
 * 将字符串字面类型转换为小写
 */
type Lowercase<S extends string> = intrinsic;

/**
 * 将字符串字面类型的首字母转换为大写
 */
type Capitalize<S extends string> = intrinsic;

/**
 * 将字符串字面类型的首字母转换为小写
 */
type Uncapitalize<S extends string> = intrinsic;

我发现了一个名为 intrinsic 的关键字,即使在 TypeScript 的官方文档中也没有看到过。

我以为像 UppercaseLowercase 这样的类型是由其他类型(比如 InstanceType)转换/映射/推断出来的。但事实并非如此。似乎 intrinsic 关键字对用户来说是一个完全的黑盒。

它是什么?用户应该在他们自己的类型中使用它吗?

当我尝试使用它时:

type MyCapitalize<S extends string> = intrinsic;

得到错误:

'intrinsic' 关键字只能用于声明编译器提供的内置类型。(2795)


<details>
<summary>英文:</summary>

I found some types in `lib.es5.d.ts`:

```typescript
/**
 * Obtain the return type of a constructor function type
 */
type InstanceType&lt;T extends abstract new (...args: any) =&gt; any&gt; = T extends abstract new (...args: any) =&gt; infer R ? R : any;

/**
 * Convert string literal type to uppercase
 */
type Uppercase&lt;S extends string&gt; = intrinsic;

/**
 * Convert string literal type to lowercase
 */
type Lowercase&lt;S extends string&gt; = intrinsic;

/**
 * Convert first character of string literal type to uppercase
 */
type Capitalize&lt;S extends string&gt; = intrinsic;

/**
 * Convert first character of string literal type to lowercase
 */
type Uncapitalize&lt;S extends string&gt; = intrinsic;

There is a keyword named intrinsic I have never seen, even in the TS official documentation.

I thought the types like Uppercase, and Lowercase are transformed/mapped/inferred by other types like InstanceType. But they are not. It seems the intrinsic keyword is a total black box to the user.

What's it? Should users use it in their own types?

When I try to use it:

type MyCapitalize&lt;S extends string&gt; = intrinsic;

Got error:

> The 'intrinsic' keyword can only be used to declare compiler provided intrinsic types.(2795)

答案1

得分: 2

新的 intrinsic 关键字用于指示类型别名引用编译器提供的实现。

在类型别名声明中,只有在等号分隔符之后紧跟 intrinsic 才是正确的,但只有当类型别名命名为 UppercaseLowercaseCapitalizeUncapitalize 并且带有单个类型参数时才可以指定 intrinsic(当然,未来可能会提供额外的内置实现)。

指向 TypeScript 存储库的来源

英文:

> The new intrinsic keyword is used to indicate that the type alias references a compiler provided implementation.

> It is an error to specify intrinsic anywhere but immediately following the = separator in a type alias declaration for a type named Uppercase, Lowercase, Capitalize or Uncapitalize taking a single type parameter (but, of course, it is possible that additional intrinsic implementations will be provided in the future).

Source pointing to the Typescript repo

huangapple
  • 本文由 发表于 2023年2月16日 16:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定