#Filter completions based on existing props

12 messages · Page 1 of 1 (latest)

tidal hullBOT
#

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

thefrenchpoulp#0

Preview:```ts
type CommonProps = {className?: string}

type Props = CommonProps &
(
| {a: "aone" | "atwo"; barSpecific: number}
| {a: "athree" | "afour"; fooSpecific: string}
)

declare function Komponent(_: Props): unknown

Komponent({a: "", barSpecific: 123})```

split narwhal
#

Apparently it works the way I want it to with literal

type Props = CommonProps &
  (
    | { a: 'aone' | 'atwo'; b: 'bone' | 'btwo' }
    | { a: 'athree' | 'afour'; b: 'bthree' | 'bfour' }
  );

Then only the appropriate values are offered in completions

clear jungle
#

Yeah, @split narwhal if you use a literal then it's a discriminated union, which TS narrows more aggressively

#

And your original case is a discriminated union, but the discriminant is a, so you don't get the discriminated iunion behavior until you specify a.

split narwhal
#

Why shouldn't it be able to discriminate against allowed props? I tried with barSpecific?: never in the other half for instance but that didn't help

clear jungle
#

Unions in TS aren't exclusive, unless they're discriminated with literal values.

#
type AB = { a: "A" } | { b: "B" }
const ab: AB = { a: "A", b: "B" } // okay
split narwhal
#

Got it, but never would make it exclusive right? From a user perspective it feels like it should behave the same
But I can easily use literals in my case anyway

clear jungle
#

Sticking ?: never (or ?: undefined) is kind of a user-level hack to make a union exclusive, but it isn't going to help the intellisense behavior because it doesn't make it discriminated.

split narwhal
#

Alright thanks👌