Typescript: what is a “naked type parameter”

后端 未结 1 1506
我在风中等你
我在风中等你 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> = T extends boolean ? "YES" : "NO"
    type WrappedUsage<T> = [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<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.

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