Remove incorrect call to checkTruthinessExpression (#59507)

This commit is contained in:
Ryan Cavanaugh
2024-08-02 12:51:07 -07:00
committed by GitHub
parent e078a9367e
commit 6f646429e0
7 changed files with 48 additions and 25 deletions

View File

@@ -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");

View File

@@ -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.
}

View File

@@ -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);

View File

@@ -37,6 +37,9 @@ while ({} as any) { }
while ({} satisfies unknown) { }
while ((<any>({}))) { }
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);

View File

@@ -61,3 +61,12 @@ while ({} satisfies unknown) { }
while ((<any>({}))) { }
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))

View File

@@ -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
> : ^^^

View File

@@ -34,3 +34,6 @@ while ({} as any) { }
while ({} satisfies unknown) { }
while ((<any>({}))) { }
while ((({}))) { }
// Should be OK
console.log((cond || undefined) && 1 / cond);