Skip to content

5.6.1-rc Incorrectly assumes "this" is not nullish even when declared as suchΒ #59755

Closed
@Knagis

Description

@Knagis

πŸ”Ž Search Terms

ts2869
Right operand of ?? is unreachable because the left operand is never nullish.

πŸ•— Version & Regression Information

Feature introduced in 5.6
reproduced in 5.6.1-rc and v5.7.0-dev.20240826

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.1-rc#code/GYVwdgxgLglg9mABMOcAUUAWMDOAuRAeQCMArAU2kQB9FwATc4GMc+gSkQG8AoRfxACdyUEIKRZciAPzTEABgDcPAL48gA

πŸ’» Code

function foo(this: Object | undefined) {
    return this ?? 0;
}

πŸ™ Actual behavior

Complains that this is never nullish.

image

πŸ™‚ Expected behavior

Since this is explicitly declared as | undefined, the error should not be shown.

In non-strict mode, this indeed could not be nullish, but in strict mode it can.

new Function(`"use strict"
function foo() {
    console.log(this);
}
foo.apply(undefined)`)()

Additional information about the issue

No response

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions