0

Here is my try:

type Comb1 = "123" | "1234";
type Comb2 = "123" | "1234" | "12345";

type Res<T, U> = T extends U ? T : never;

// Res1 === "123" | "1234"
type Res1 = isExtends<Comb1, Comb2>;

// Res2 === "123" | "1234"
type Res2 = isExtends<Comb2, Comb1>;

Why Res2 is not 'never'?

And what 'extends' do in this case?

Hanzc
  • 3
  • 1
  • Does this answer your question? [Typescript: what is a "naked type parameter"](https://stackoverflow.com/questions/51651499/typescript-what-is-a-naked-type-parameter) – ford04 Dec 01 '20 at 07:43
  • Check this link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types. – Николай Гольцев Dec 01 '20 at 07:44
  • Also related: https://stackoverflow.com/questions/55382306/typescript-distributive-conditional-types – ford04 Dec 01 '20 at 07:49
  • 1
    Thanks for your comments, and i've got the key point is to understand distributive conditional types. @ford04 – Hanzc Dec 02 '20 at 02:13

1 Answers1

0

Change Your isExtends type to:

type isExtends<T, U> = T extends U ? 1 : 0;

then You'll see:

type Res1 = isExtends<"123" | "1234", "123" | "1234" | "12345">; // Res1 === 1
type Res2 = isExtends<"123" | "1234" | "12345", "123" | "1234">; // Res2 === 0 | 1
// --------------------------------------------------------------------------^^^^^

So, now return to your original case

type isExtends<T, U> = T extends U ? T : never;
type Res2 = isExtends<"123" | "1234" | "12345", "123" | "1234">;

it results in "123" | "1234" because it's de facto ("123" | "1234") | never

This

isExtends<"123" | "1234" | "12345", "123" | "1234">

can be expanded as:

isExtends<"123" | "1234", "123" | "1234"> | isExtends("12345", "123" | "1234")

which is

("123" | "1234") | never

which is simply

"123" | "1234"
Tomasz Gawel
  • 8,379
  • 4
  • 36
  • 61