Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3829,10 +3829,17 @@ func (c *Checker) checkTestingKnownTruthyType(condExpr *ast.Node, condType *Type
if location != condExpr {
t = c.checkExpression(location)
}
if t.flags&TypeFlagsEnumLiteral != 0 && ast.IsPropertyAccessExpression(location) && core.OrElse(c.getResolvedSymbolOrNil(location.Expression()), c.unknownSymbol).Flags&ast.SymbolFlagsEnum != 0 {
// EnumLiteral type at condition with known value is always truthy or always falsy, likely an error
c.error(location, diagnostics.This_condition_will_always_return_0, core.IfElse(evaluator.IsTruthy(t.AsLiteralType().value), "true", "false"))
return
if t.flags&TypeFlagsEnumLiteral != 0 && ast.IsPropertyAccessExpression(location) {
exprSymbol := core.OrElse(c.getResolvedSymbolOrNil(location.Expression()), c.unknownSymbol)
if exprSymbol.Flags&ast.SymbolFlagsAlias != 0 {
exprSymbol = c.resolveAlias(exprSymbol)
}
exprSymbol = c.getExportSymbolOfValueSymbolIfExported(exprSymbol)
if exprSymbol.Flags&ast.SymbolFlagsEnum != 0 {
// EnumLiteral type at condition with known value is always truthy or always falsy, likely an error
c.error(location, diagnostics.This_condition_will_always_return_0, core.IfElse(evaluator.IsTruthy(t.AsLiteralType().value), "true", "false"))
return
}
}
isPropertyExpressionCast := ast.IsPropertyAccessExpression(location) && isTypeAssertion(location.Expression())
if !c.hasTypeFacts(t, TypeFactsTruthy) || isPropertyExpressionCast {
Expand Down
25 changes: 25 additions & 0 deletions testdata/tests/cases/compiler/exportEnumConditionAlwaysTrue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module A {
export enum ExportedEnum {
APP_PAGE = 1,
PAGES = 2,
}
}

import ImportedEnum = A.ExportedEnum;

// Test 1: Exported enum directly
declare const type1: A.ExportedEnum;
const kind1 = type1 === A.ExportedEnum.APP_PAGE || A.ExportedEnum.PAGES ? 'left' : 'right';

// Test 2: Imported enum alias
declare const type2: ImportedEnum;
const kind2 = type2 === ImportedEnum.APP_PAGE || ImportedEnum.PAGES ? 'left' : 'right';

// Test 3: Normal enum (baseline verification)
enum NormalEnum {
A = 1,
B = 2,
}

declare const type3: NormalEnum;
const kind3 = type3 === NormalEnum.A || NormalEnum.B ? 'left' : 'right';