36

See 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 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?

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89

1 Answers1

48

When they say naked here, they mean that the type parameter is present without being wrapped in another type, (i.e., 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 naked 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.

Damian Green
  • 6,895
  • 2
  • 31
  • 43
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • @NurbolAlpysbayev yes you are right the order should be reversed, fixed, 10x – Titian Cernicova-Dragomir Aug 02 '18 at 11:04
  • Hi @TitianCernicova-Dragomir! I'm totally new to typescript so pls bare with my dumb question. Is the part `` called a `union` in typescript terms? – PenguinBlues Dec 30 '18 at 02:55
  • 3
    @PenguinBlues Yes it is (excluding `<` and `>` which denote *type arguments*). `A | B` is a union, while `A & B` is an intersection. The typescript handbook has all the info. – Nurbol Alpysbayev Dec 31 '18 at 10:20
  • It's really sad that the official docs don't mention the tuple trick. – aleclarson Jan 16 '19 at 17:48
  • @TitianCernicova-Dragomir , this definition doesn't seem exactly right, as Id counts as a naked type parameter when Id is defined as `type Id = T`. So it seems the definition must include more than just syntax. – Max Heiber Jun 02 '20 at 08:21
  • 2
    @MaxHeiber I guess if the type alias is trivially (at the declaration site) the same as `T` distribution still works but this is not really clear from the docs either – Titian Cernicova-Dragomir Jun 02 '20 at 18:09
  • @TitianCernicova-Dragomir I noticed if I wrap `T[number]` in a variable, then distribution does happen, e.g. `type AnyOf=...` if you do `I extends` afterwards - then distribution does take place. But if I used only `T[number] extends ...` instead of `I`, it doesn't distribute. In this case TS considers `I` as naked? – Giorgi Moniava Dec 05 '22 at 16:35
  • @GiorgiMoniava Yup, exactly. `I` is a type parameter, so it distributes over it. `T[number]` is more complicated than a simple type parameter – Titian Cernicova-Dragomir Dec 05 '22 at 16:44