打字稿:什么是“裸类型参数"? [英] Typescript: what is a "naked type parameter"

查看:23
本文介绍了打字稿:什么是“裸类型参数"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参见 https:///www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types

条件类型,其中检查类型是裸类型参数...

Conditional types in which the checked type is a naked type parameter...

Google 没有帮助,或者答案是针对 C#,我不知道.在 Typescript 文档中也找不到该术语.从上下文中获取含义也很困难...

Google doesn't help, or answers are for C#, which I don't know. Couldn't find the term in Typescript docs either. Getting a meaning from context is also hard...

顺便说一句,我知道什么是类型参数".但是裸体"是什么意思?

BTW I do know what a "type parameter" is. But what does "naked" mean?

推荐答案

当他们在这里说裸体时,他们的意思是类型参数存在而不被另一种类型包裹,(即,一个数组,或一个元组,或一个函数、promise 或任何其他泛型类型)

When they say naked here, they mean that the type parameter is present without being wrapped in another type, (ie, an array, or a tuple, or a function, or a promise or any other generic type)

例如:

type NakedUsage<T> = T extends boolean ? "YES" : "NO"
type WrappedUsage<T> = [T] extends [boolean] ? "YES" : "NO"; // wrapped in a tuple

naked 与 non nakes 的重要原因是,naked 用法分布在一个联合中,这意味着条件类型应用于联合的每个成员,结果将是所有应用程序的联合

The reason naked vs non nakes is important is that naked usages distribute over a union, meaning the conditional type is applied for each member of the union and the result will be the union of all application

type Distributed = NakedUsage<number | boolean > // = NakedUsage<number> | NakedUsage<boolean> =  "NO" | "YES" 
type NotDistributed = WrappedUsage<number | boolean > // "NO"    
type NotDistributed2 = WrappedUsage<boolean > // "YES"

此处阅读有关条件类型分布的信息.

Read here about conditional type distribution.

这篇关于打字稿:什么是“裸类型参数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆