#How to fix this conditional type?

14 messages · Page 1 of 1 (latest)

short mangoBOT
#
Alii#8184

Preview:```ts
type conditionalParams = {
/**

  • run a callback fn while streaming data
    /
    onStream: (chunkValue: string) => void
    /
    *
  • run a callback fn when streaming is done
    */
    onStreamFinished: (chunkValue: string) => void
    }

interface Re
...```

#
Burrito#6903

Preview:```ts
type ConditionalParams = {
onStream: (chunkValue: string) => void
onStreamFinished: (chunkValue: string) => void
}

type RequiredParams = {
url: string
options: RequestInit
}

type FetcherParams = RequiredParams &
(
| {
...```

opal moat
#

You probably want a DU.

bleak garden
opal moat
#

Discriminated union.

#

The shape of FetcherParams is discriminated by stream field, in your case.

bleak garden
#

I did that!

#
type FetcherParams = RequiredParams["stream"] extends true
  ? RequiredParams & ConditionalParams
  : RequiredParams;```
isn't that what you mean?
opal moat
#

Yes but that wouldn't work, because RequiredParams doesn't change, RequiredParams["stream"] is always boolean, and boolean extends true is always true.

bleak garden
opal moat
#

Because that's not a DU

#

That whole thing just collapses to always the true branch.

bleak garden
opal moat