#Help with type narrowing

14 messages · Page 1 of 1 (latest)

sterile lark
astral lakeBOT
#

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

_lofty.dev#0

Preview:```ts
type Union = "a" | "b" | "c" | "d" | "e";
type Something<T extends Union> = T extends "a" ? { f: string } : { g: string };
type Composed<T> = Something<T> & { kind: T }

const config = {
s: { kind: "a", }
} as const satisfies Record<string, Composed<Union>>;```

grave ivy
#

uh, just add the extends Union constraint to Composed?

#

this is a discriminated union btw

#

!hb disc

astral lakeBOT
sterile lark
grave ivy
#

not sure what you mean by that?

#

do you want only f to show up there?

sterile lark
#

What I wanted was the type to be narrowed based on what I passed to T itself..
if I passed kind: "a", yes, I want only f to show up

grave ivy
astral lakeBOT
#
that_guy977#0

Preview:```ts
type Union = "a" | "b" | "c" | "d" | "e";
type Something<T extends Union> = T extends "a" ? { f: string } : { g: string };
type Composed<T extends Union> = T extends T ? Something<T> & { kind: T } : never

const config = {
s: { kind: "a", }
} as const satisfies Record<string, Composed<Union>>;```

sterile lark
#

Well.. that's something...

#

Ah, I see, that's clever, thank you