2

See these types:

type Test1<T> = [T, T];
type Test2<T> = (T extends string ? 'Something' : [T, T]);

type S1 = Test1<string>;   // 'Something'
type N1 = Test1<number>;   // [number, number]
type B1 = Test1<boolean>;  // [boolean, boolean]
type X1 = Test1<1|2>;      // [2|1, 2|1]

type S2 = Test2<string>;   // 'Something'
type N2 = Test2<number>;   // [number, number]
type B2 = Test2<boolean>;  // [false, false] | [true, true]  <---
type X2 = Test2<1|2>;      // [2, 2] | [1, 1]                <---

Why is B2 and X2 different from B1 and X1 and how can I get [boolean, boolean] out of Test2<boolean>. Typescipt 4.6.4

hansmaad
  • 18,417
  • 9
  • 53
  • 94
  • Your `Test2` conditional type is [distributive](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types) across unions, and `boolean` is represented in TS as the union `true | false`. If you don't want that, you should write `[T] extends [string] ? 'Something' : [T, T]` instead. See the linked questions and answers for more information. – jcalz Aug 23 '22 at 14:39
  • 2
    @jcalz Thanks, once you know which words to use (distributive) it's possible to find a solution :) Your comment is actually a perfect tl:dr: answer. `[T] extends [string] ? 'Something' : [T, T]` does the trick. – hansmaad Aug 23 '22 at 14:44

0 Answers0