1
type TestType<T> = T extends string ? 1: 2
let demo: testType<string | number>; // => 1 | 2

type T1 = (string extends string ? 1 : 2)  | ( number extends string ? 1 : 2)// => 1 | 2
type T2 = (string | number) extends string ? 1 : 2 // => 2 why?

TestType is conditional types, and it is similar T1 ,but what is the different with T2?

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
thinkmoe
  • 51
  • 1
  • 3
  • 3
    Have you read the documentation on [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types)? The difference between your two examples is that in `TestType`, `T` is a "naked type parameter", whereas in `T2`, `(string | number)` isn't. – kaya3 Jul 26 '22 at 11:05
  • @kaya3 many thanks, and I use "naked type paramete" then find an answer https://stackoverflow.com/questions/51651499/typescript-what-is-a-naked-type-parameter – thinkmoe Jul 26 '22 at 13:14

0 Answers0