Skip to content
Merged
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
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;
}