#Discriminated unions in arrays have ambiguous member types

6 messages · Page 1 of 1 (latest)

keen axleBOT
#

@junior wagon Here's a shortened URL of your playground link! You can remove the full link from your message.

RyanCaoDev#0428

Preview:```ts
enum I { A, B };

interface T<S extends I> { i: S; j: S extends I.A ? number : string; }

const t = [{
i: I.A,
j: "string also works",
}] satisfies T<I>[];```

agile ivy
#

T.j is linked to S, not linked to T.i

woven hemlock
#

also that's not a discriminated union (it's not a union type at all). this is:

keen axleBOT
#
mkantor#7432

Preview:ts ... type T = { i: I.A; j: number; } | { i: I.B; j: string; } ...

charred igloo
keen axleBOT
#
Jakob#8686

Preview:```ts
enum I { A, B };

type T<S extends I> = S extends any ? { i: S; j: S extends I.A ? number : string; } : never;

const t = [{
i: I.A,
j: "string also works",
}] satisfies T<I>[];

const u = [{
i: I.A,
j: 3
}] satisfies T<I>[];```