Skip to content

Commit 6f64642

Browse files
Remove incorrect call to checkTruthinessExpression (#59507)
1 parent e078a93 commit 6f64642

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44186,7 +44186,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4418644186
bothHelper(location, body);
4418744187
return;
4418844188
}
44189-
const type = location === condExpr ? condType : checkTruthinessExpression(location);
44189+
const type = location === condExpr ? condType : checkExpression(location);
4419044190
if (type.flags & TypeFlags.EnumLiteral && isPropertyAccessExpression(location) && (getNodeLinks(location.expression).resolvedSymbol ?? unknownSymbol).flags & SymbolFlags.Enum) {
4419144191
// EnumLiteral type at condition with known value is always truthy or always falsy, likely an error
4419244192
error(location, Diagnostics.This_condition_will_always_return_0, !!(type as LiteralType).value ? "true" : "false");

tests/baselines/reference/nullishCoalescingOperator7.errors.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/baselines/reference/predicateSemantics.errors.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,7 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru
7070
while ((({}))) { }
7171
~~~~~~
7272
!!! error TS2872: This kind of expression is always truthy.
73+
74+
// Should be OK
75+
console.log((cond || undefined) && 1 / cond);
7376

tests/baselines/reference/predicateSemantics.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ while ({} as any) { }
3737
while ({} satisfies unknown) { }
3838
while ((<any>({}))) { }
3939
while ((({}))) { }
40+
41+
// Should be OK
42+
console.log((cond || undefined) && 1 / cond);
4043

4144

4245
//// [predicateSemantics.js]
@@ -75,3 +78,5 @@ while ({}) { }
7578
while ({}) { }
7679
while (({})) { }
7780
while ((({}))) { }
81+
// Should be OK
82+
console.log((cond || undefined) && 1 / cond);

tests/baselines/reference/predicateSemantics.symbols

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,12 @@ while ({} satisfies unknown) { }
6161
while ((<any>({}))) { }
6262
while ((({}))) { }
6363

64+
// Should be OK
65+
console.log((cond || undefined) && 1 / cond);
66+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
67+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
68+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
69+
>cond : Symbol(cond, Decl(predicateSemantics.ts, 0, 11))
70+
>undefined : Symbol(undefined)
71+
>cond : Symbol(cond, Decl(predicateSemantics.ts, 0, 11))
72+

tests/baselines/reference/predicateSemantics.types

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,30 @@ while ((({}))) { }
192192
>{} : {}
193193
> : ^^
194194

195+
// Should be OK
196+
console.log((cond || undefined) && 1 / cond);
197+
>console.log((cond || undefined) && 1 / cond) : void
198+
> : ^^^^
199+
>console.log : (...data: any[]) => void
200+
> : ^^^^ ^^ ^^^^^
201+
>console : Console
202+
> : ^^^^^^^
203+
>log : (...data: any[]) => void
204+
> : ^^^^ ^^ ^^^^^
205+
>(cond || undefined) && 1 / cond : number
206+
> : ^^^^^^
207+
>(cond || undefined) : any
208+
> : ^^^
209+
>cond || undefined : any
210+
> : ^^^
211+
>cond : any
212+
> : ^^^
213+
>undefined : undefined
214+
> : ^^^^^^^^^
215+
>1 / cond : number
216+
> : ^^^^^^
217+
>1 : 1
218+
> : ^
219+
>cond : any
220+
> : ^^^
221+

tests/cases/compiler/predicateSemantics.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ while ({} as any) { }
3434
while ({} satisfies unknown) { }
3535
while ((<any>({}))) { }
3636
while ((({}))) { }
37+
38+
// Should be OK
39+
console.log((cond || undefined) && 1 / cond);

0 commit comments

Comments
 (0)