#Conditional types

8 messages · Page 1 of 1 (latest)

tranquil valve
#
funtion(a: string | number) {
  double(a) // return type is string | number
}

// here is how it distributes over union
   (string | number) extends string ? string : number
-> (string extends string ? string : number) |
   (number extends string ? string : number) |
-> string | number

However, there is an issue in this case:

type Comparable<T> =
  T extends Date ? Date | number :
  T extends number ? number :
  T extends string ? string : never;

declare function isLessThan<T>(a: T, b: Comparable<T>): boolean;

let dateOrStr = Math.random() < 0.5 ? new Date() : 'A'; // ^? let dateOrStr: Date | string
isLessThan(dateOrStr, 'B') // ok, but should be an error

// The type [A] is assignable to [B] if and only if A is assignable to B. This does not change the semantics of Comparable therefore. However, as [T] is a tuple now and not bare T, therefore the rule of distributing over union is not applied.
type Comparable<T> =
  [T] extends [Date] ? Date | number :
  [T] extends [number] ? number :
  [T] extends [string] ? string : never;

Is this true? The statement However, as [T] is a tuple now and not bare T, therefore the rule of distributing over union is not applied.?
Or i misunderstand it?
In this case the T is substituted with Date | string and we get [Date | string] extends [Date] ? .... getting never?

hard otter
#

i can't tell if you want distribution to happen or not, but yes it seems like you understand it properly

#

your later definition of Comparable is what fixes the // ok, but should be an error issue. are there other issues you have with that definition?

tranquil valve
#

No, just to get the confirmation that the rule is something like "when type is bare type T, then it distributes over union, otherwise, when it is [T], T['length'], etc.., it will not and will be matched literally.

hard otter
#

yep, that's correct

tranquil valve
#

thank you

hard otter
#

note that wrapping T in a one-element tuple is just one way to avoid distribution. there's nothing special about tuples though; using anything besides a bare T would have the same effect