#Why doesn't the ?? operator narrow the type used on the left-hand side?

7 messages · Page 1 of 1 (latest)

hoary harnessBOT
#
bawdyinkslinger#0

Preview:```ts
declare function passString(s: string): Promise<void>

let exactTitle: string | undefined = undefined

async function done(): Promise<boolean> {
exactTitle ?? (await passString(exactTitle))

return true
}```

uncut wedge
#

Argument of type 'undefined' is not assignable to parameter of type 'string'.ts(2345)
expectedDialogCount

#

can't it only call the right hand side if exactTitle is defined?

#

oh duh

#

I inverted what this operator does. I thought it was like && on steroids, but it's || on steroids

#

Okay that answers that.

What I like about ?? is how specific it is. Is there something like !?? that'll only do the right hand side if exactTitle is defined? I know most people in JavaScript would use && for that, but that has bit me in the past; sometimes the left-hand side is a number of 0 or a blank string, and in those cases, I would want to execute the right-hand side.

#

I guess since this all takes place in my local function, I can just check that it !== undefined. So I guess that solves that too.