I've got the following TS code
type Fruit = { kind: "apple" } | { kind: "grape"; color: "green" | "black" };
type FruitTaste<TFruit extends Fruit> = TFruit["kind"] extends "apple"
? "good"
: TFruit["color"] extends "green"
? "good"
: "bad";
It errors at TFruit["color"] with
Type '"color"' cannot be used to index type 'TFruit'.
but it shouldn't, because we're in the ternary side where TFruit should only be restricted to { kind: "grape"; color: "green" | "black" }, and the color key should exist.
Weirdly enough TS doesn't have to have any issue with the "runtime" version of it:
type Fruit = { kind: "apple" } | { kind: "grape"; color: "green" | "black" };
const fruitTaste = (fruit: Fruit) =>
fruit.kind === "apple" ? "good" : fruit.color === "green" ? "good" : "bad";
Why is that? How can I implement the FruitTaste type?