Skip to content

Fixed syntactic truthy semantics for 0n #60323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44504,9 +44504,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return PredicateSemantics.Sometimes;
}
return PredicateSemantics.Always;
case SyntaxKind.BigIntLiteral:
return (node as BigIntLiteral).text !== "0n" ? PredicateSemantics.Always : PredicateSemantics.Never;
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.ClassExpression:
case SyntaxKind.FunctionExpression:
case SyntaxKind.JsxElement:
Expand Down
23 changes: 21 additions & 2 deletions tests/baselines/reference/predicateSemantics.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ predicateSemantics.ts(33,8): error TS2872: This kind of expression is always tru
predicateSemantics.ts(34,11): error TS2872: This kind of expression is always truthy.
predicateSemantics.ts(35,8): error TS2872: This kind of expression is always truthy.
predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy.
predicateSemantics.ts(47,5): error TS2873: This kind of expression is always falsy.
predicateSemantics.ts(48,6): error TS2873: This kind of expression is always falsy.
predicateSemantics.ts(49,5): error TS2872: This kind of expression is always truthy.
predicateSemantics.ts(50,6): error TS2872: This kind of expression is always truthy.


==== predicateSemantics.ts (11 errors) ====
==== predicateSemantics.ts (15 errors) ====
declare let cond: any;

// OK: One or other operand is possibly nullish
Expand Down Expand Up @@ -77,4 +81,19 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru
function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}
}

// https://github.com/microsoft/TypeScript/issues/60320
if (0n) {}
~~
!!! error TS2873: This kind of expression is always falsy.
if (!0n) {}
~~
!!! error TS2873: This kind of expression is always falsy.
if (1n) {}
~~
!!! error TS2872: This kind of expression is always truthy.
if (!1n) {}
~~
!!! error TS2872: This kind of expression is always truthy.

45 changes: 25 additions & 20 deletions tests/baselines/reference/predicateSemantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ console.log((cond || undefined) && 1 / cond);
function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}
}

// https://github.com/microsoft/TypeScript/issues/60320
if (0n) {}
if (!0n) {}
if (1n) {}
if (!1n) {}


//// [predicateSemantics.js]
var _a, _b, _c, _d, _e, _f;
// OK: One or other operand is possibly nullish
var test1 = (_a = (cond ? undefined : 32)) !== null && _a !== void 0 ? _a : "possibly reached";
const test1 = (cond ? undefined : 32) ?? "possibly reached";
// Not OK: Both operands nullish
var test2 = (_b = (cond ? undefined : null)) !== null && _b !== void 0 ? _b : "always reached";
const test2 = (cond ? undefined : null) ?? "always reached";
// Not OK: Both operands non-nullish
var test3 = (_c = (cond ? 132 : 17)) !== null && _c !== void 0 ? _c : "unreachable";
const test3 = (cond ? 132 : 17) ?? "unreachable";
// Parens
var test4 = (_d = (cond ? (undefined) : (17))) !== null && _d !== void 0 ? _d : 42;
const test4 = (cond ? (undefined) : (17)) ?? 42;
// Should be OK (special case)
if (!!true) {
}
Expand All @@ -64,19 +70,13 @@ while (0) { }
while (1) { }
while (true) { }
while (false) { }
var p5 = (_e = {}) !== null && _e !== void 0 ? _e : null;
var p6 = (_f = 0 > 1) !== null && _f !== void 0 ? _f : null;
var p7 = null !== null && null !== void 0 ? null : null;
var p8 = (/** @class */ (function () {
function foo() {
}
return foo;
}())) && null;
var p9 = (/** @class */ (function () {
function foo() {
}
return foo;
}())) || null;
const p5 = {} ?? null;
const p6 = 0 > 1 ?? null;
const p7 = null ?? null;
const p8 = (class foo {
}) && null;
const p9 = (class foo {
}) || null;
// Outer expression tests
while ({}) { }
while ({}) { }
Expand All @@ -86,5 +86,10 @@ while ((({}))) { }
console.log((cond || undefined) && 1 / cond);
function foo() {
// Should be OK
return this !== null && this !== void 0 ? this : 0;
return this ?? 0;
}
// https://github.com/microsoft/TypeScript/issues/60320
if (0n) { }
if (!0n) { }
if (1n) { }
if (!1n) { }
7 changes: 7 additions & 0 deletions tests/baselines/reference/predicateSemantics.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ function foo(this: Object | undefined) {
return this ?? 0;
>this : Symbol(this, Decl(predicateSemantics.ts, 40, 13))
}

// https://github.com/microsoft/TypeScript/issues/60320
if (0n) {}
if (!0n) {}
if (1n) {}
if (!1n) {}

22 changes: 22 additions & 0 deletions tests/baselines/reference/predicateSemantics.types
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,25 @@ function foo(this: Object | undefined) {
>0 : 0
> : ^
}

// https://github.com/microsoft/TypeScript/issues/60320
if (0n) {}
>0n : 0n
> : ^^

if (!0n) {}
>!0n : true
> : ^^^^
>0n : 0n
> : ^^

if (1n) {}
>1n : 1n
> : ^^

if (!1n) {}
>!1n : boolean
> : ^^^^^^^
>1n : 1n
> : ^^

10 changes: 9 additions & 1 deletion tests/cases/compiler/predicateSemantics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @target: esnext

declare let cond: any;

// OK: One or other operand is possibly nullish
Expand Down Expand Up @@ -41,4 +43,10 @@ console.log((cond || undefined) && 1 / cond);
function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}
}

// https://github.com/microsoft/TypeScript/issues/60320
if (0n) {}
if (!0n) {}
if (1n) {}
if (!1n) {}