diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c0956390945..702c8850afe 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44186,7 +44186,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { bothHelper(location, body); return; } - const type = location === condExpr ? condType : checkTruthinessExpression(location); + const type = location === condExpr ? condType : checkExpression(location); if (type.flags & TypeFlags.EnumLiteral && isPropertyAccessExpression(location) && (getNodeLinks(location.expression).resolvedSymbol ?? unknownSymbol).flags & SymbolFlags.Enum) { // EnumLiteral type at condition with known value is always truthy or always falsy, likely an error error(location, Diagnostics.This_condition_will_always_return_0, !!(type as LiteralType).value ? "true" : "false"); diff --git a/tests/baselines/reference/nullishCoalescingOperator7.errors.txt b/tests/baselines/reference/nullishCoalescingOperator7.errors.txt deleted file mode 100644 index 008d589a9e7..00000000000 --- a/tests/baselines/reference/nullishCoalescingOperator7.errors.txt +++ /dev/null @@ -1,24 +0,0 @@ -nullishCoalescingOperator7.ts(6,19): error TS2872: This kind of expression is always truthy. -nullishCoalescingOperator7.ts(7,19): error TS2872: This kind of expression is always truthy. -nullishCoalescingOperator7.ts(10,23): error TS2872: This kind of expression is always truthy. - - -==== nullishCoalescingOperator7.ts (3 errors) ==== - declare const a: string | undefined; - declare const b: string | undefined; - declare const c: string | undefined; - - const foo1 = a ? 1 : 2; - const foo2 = a ?? 'foo' ? 1 : 2; - ~~~~~ -!!! error TS2872: This kind of expression is always truthy. - const foo3 = a ?? 'foo' ? (b ?? 'bar') : (c ?? 'baz'); - ~~~~~ -!!! error TS2872: This kind of expression is always truthy. - - function f () { - const foo4 = a ?? 'foo' ? b ?? 'bar' : c ?? 'baz'; - ~~~~~ -!!! error TS2872: This kind of expression is always truthy. - } - \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 4d96f98df22..883ce457098 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -70,4 +70,7 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru while ((({}))) { } ~~~~~~ !!! error TS2872: This kind of expression is always truthy. + + // Should be OK + console.log((cond || undefined) && 1 / cond); \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index c1aebaad42b..4ed29418933 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -37,6 +37,9 @@ while ({} as any) { } while ({} satisfies unknown) { } while ((({}))) { } while ((({}))) { } + +// Should be OK +console.log((cond || undefined) && 1 / cond); //// [predicateSemantics.js] @@ -75,3 +78,5 @@ while ({}) { } while ({}) { } while (({})) { } while ((({}))) { } +// Should be OK +console.log((cond || undefined) && 1 / cond); diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index f92a128bbd4..39ce291427a 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -61,3 +61,12 @@ while ({} satisfies unknown) { } while ((({}))) { } while ((({}))) { } +// Should be OK +console.log((cond || undefined) && 1 / cond); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>cond : Symbol(cond, Decl(predicateSemantics.ts, 0, 11)) +>undefined : Symbol(undefined) +>cond : Symbol(cond, Decl(predicateSemantics.ts, 0, 11)) + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index b75183dc538..b4f418ce339 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -192,3 +192,30 @@ while ((({}))) { } >{} : {} > : ^^ +// Should be OK +console.log((cond || undefined) && 1 / cond); +>console.log((cond || undefined) && 1 / cond) : void +> : ^^^^ +>console.log : (...data: any[]) => void +> : ^^^^ ^^ ^^^^^ +>console : Console +> : ^^^^^^^ +>log : (...data: any[]) => void +> : ^^^^ ^^ ^^^^^ +>(cond || undefined) && 1 / cond : number +> : ^^^^^^ +>(cond || undefined) : any +> : ^^^ +>cond || undefined : any +> : ^^^ +>cond : any +> : ^^^ +>undefined : undefined +> : ^^^^^^^^^ +>1 / cond : number +> : ^^^^^^ +>1 : 1 +> : ^ +>cond : any +> : ^^^ + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index 4211a43ca58..4069c1da6ee 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -34,3 +34,6 @@ while ({} as any) { } while ({} satisfies unknown) { } while ((({}))) { } while ((({}))) { } + +// Should be OK +console.log((cond || undefined) && 1 / cond);