🤖 Pick PR #59761 (this can be nullish) into release-5.6 (#59762)

Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
TypeScript Bot 2024-08-27 10:28:21 -07:00 committed by GitHub
parent 6212132b83
commit 1a03e5340a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 44 additions and 2 deletions

View File

@ -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:

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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))
}

View File

@ -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
> : ^
}

View File

@ -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;
}