英文:
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 的官方文档中也没有看到过。
我以为像 Uppercase
和 Lowercase
这样的类型是由其他类型(比如 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<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
/**
* Convert string literal type to uppercase
*/
type Uppercase<S extends string> = intrinsic;
/**
* Convert string literal type to lowercase
*/
type Lowercase<S extends string> = intrinsic;
/**
* Convert first character of string literal type to uppercase
*/
type Capitalize<S extends string> = intrinsic;
/**
* Convert first character of string literal type to lowercase
*/
type Uncapitalize<S extends string> = 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<S extends string> = intrinsic;
Got error:
> The 'intrinsic' keyword can only be used to declare compiler provided intrinsic types.(2795)
答案1
得分: 2
新的
intrinsic
关键字用于指示类型别名引用编译器提供的实现。
在类型别名声明中,只有在等号分隔符之后紧跟
intrinsic
才是正确的,但只有当类型别名命名为Uppercase
、Lowercase
、Capitalize
或Uncapitalize
并且带有单个类型参数时才可以指定intrinsic
(当然,未来可能会提供额外的内置实现)。
英文:
> 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论