Typescript: what is a “naked type parameter”

后端 未结 1 1518
我在风中等你
我在风中等你 2020-11-28 12:59

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

Conditional types in which the check

1条回答
  •  执念已碎
    2020-11-28 13:28

    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)

    Ex:

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

    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 // = NakedUsage | NakedUsage =  "NO" | "YES" 
    type NotDistributed = WrappedUsage // "NO"    
    type NotDistributed2 = WrappedUsage // "YES"
    

    Read here about conditional type distribution.

    0 讨论(0)
提交回复
热议问题