Skip to content

this can be nullish #59761

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

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -39818,6 +39818,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NewExpression:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.YieldExpression:
case SyntaxKind.ThisKeyword:
return PredicateSemantics.Sometimes;
case SyntaxKind.BinaryExpression:
// List of operators that can produce null/undefined:
6 changes: 5 additions & 1 deletion tests/baselines/reference/predicateSemantics.errors.txt
Original file line number Diff line number Diff line change
@@ -73,4 +73,8 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru

// Should be OK
console.log((cond || undefined) && 1 / cond);


function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}
10 changes: 9 additions & 1 deletion tests/baselines/reference/predicateSemantics.js
Original file line number Diff line number Diff line change
@@ -40,7 +40,11 @@ while ((({}))) { }

// Should be OK
console.log((cond || undefined) && 1 / cond);


function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}

//// [predicateSemantics.js]
var _a, _b, _c, _d, _e, _f;
@@ -80,3 +84,7 @@ while (({})) { }
while ((({}))) { }
// Should be OK
console.log((cond || undefined) && 1 / cond);
function foo() {
// Should be OK
return this !== null && this !== void 0 ? this : 0;
}
9 changes: 9 additions & 0 deletions tests/baselines/reference/predicateSemantics.symbols
Original file line number Diff line number Diff line change
@@ -70,3 +70,12 @@ console.log((cond || undefined) && 1 / cond);
>undefined : Symbol(undefined)
>cond : Symbol(cond, Decl(predicateSemantics.ts, 0, 11))

function foo(this: Object | undefined) {
>foo : Symbol(foo, Decl(predicateSemantics.ts, 38, 45))
>this : Symbol(this, Decl(predicateSemantics.ts, 40, 13))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

// Should be OK
return this ?? 0;
>this : Symbol(this, Decl(predicateSemantics.ts, 40, 13))
}
15 changes: 15 additions & 0 deletions tests/baselines/reference/predicateSemantics.types
Original file line number Diff line number Diff line change
@@ -219,3 +219,18 @@ console.log((cond || undefined) && 1 / cond);
>cond : any
> : ^^^

function foo(this: Object | undefined) {
>foo : (this: Object | undefined) => Object | 0
> : ^ ^^ ^^^^^^^^^^^^^^^
>this : Object
> : ^^^^^^

// Should be OK
return this ?? 0;
>this ?? 0 : 0 | Object
> : ^^^^^^^^^^
>this : Object
> : ^^^^^^
>0 : 0
> : ^
}
5 changes: 5 additions & 0 deletions tests/cases/compiler/predicateSemantics.ts
Original file line number Diff line number Diff line change
@@ -37,3 +37,8 @@ while ((({}))) { }

// Should be OK
console.log((cond || undefined) && 1 / cond);

function foo(this: Object | undefined) {
// Should be OK
return this ?? 0;
}