Skip to content

Commit

Permalink
🤖 Pick PR #59761 (this can be nullish) into release-5.6 (#59762)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Cavanaugh <[email protected]>
  • Loading branch information
TypeScript Bot and RyanCavanaugh authored Aug 27, 2024
1 parent 6212132 commit 1a03e53
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39725,6 +39725,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:
Expand Down
6 changes: 5 additions & 1 deletion tests/baselines/reference/predicateSemantics.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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;
}

0 comments on commit 1a03e53

Please sign in to comment.