-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options
Description
Bug Report
🔎 Search Terms
v4.9 conditionals unlisted property narrowing in operator #50666
🕗 Version & Regression Information
v4.9+, including nightly
working in v4.8.4
- This changed between versions 4.8.4 and 4.9
⏯ Playground Link
Playground link with relevant code
💻 Code
type HandledResult = {data: any, errors?: any}
type UnhandledResult = {message: string}
const result = null as any as HandledResult | UnhandledResult
const noWork = (): HandledResult => {
if ('data' in result || 'errors' in result) return result // when checking an optional property second, I get an error
return {data: null, errors: [result.message]}
}
const work = (): HandledResult => {
if ('errors' in result || 'data' in result ) return result // reversing the order fixes the problem
return {data: null, errors: [result.message]}
}
🙁 Actual behavior
In the noWork
function, typescript could not narrow down the type to being HandledResult
. When the or clause is switched, it works. Since Boolean(A || B) === Boolean(B || A)
this seems like an error.
🙂 Expected behavior
The ordering of the conditionals should not impact the narrowing of the type.
Metadata
Metadata
Assignees
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options