diff --git a/internal/checker/checker.go b/internal/checker/checker.go index c4ec0ee225..10532bbb63 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -28999,7 +28999,7 @@ func (c *Checker) getContextualTypeForAssignmentExpression(binary *ast.BinaryExp thisType := c.getTypeOfExpression(expr) if ast.IsPropertyAccessExpression(left) { name := left.Name() - if ast.IsPrivateIdentifier(name) { + if ast.IsPrivateIdentifier(name) && thisType.symbol != nil { symbol = c.getPropertyOfType(thisType, binder.GetSymbolNameForPrivateIdentifier(thisType.symbol, name.Text())) } else { symbol = c.getPropertyOfType(thisType, name.Text()) diff --git a/internal/fourslash/tests/privatePropertyOfUndefinedThis_test.go b/internal/fourslash/tests/privatePropertyOfUndefinedThis_test.go new file mode 100644 index 0000000000..62c425caa0 --- /dev/null +++ b/internal/fourslash/tests/privatePropertyOfUndefinedThis_test.go @@ -0,0 +1,38 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/typescript-go/internal/fourslash" + "github.com/microsoft/typescript-go/internal/lsp/lsproto" + "github.com/microsoft/typescript-go/internal/testutil" +) + +func TestPrivatePropertyOfUndefinedThis(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = ` +this.#a = {}; +export {}; +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyNonSuggestionDiagnostics(t, []*lsproto.Diagnostic{ + { + Code: &lsproto.IntegerOrString{Integer: new(int32(2532))}, + Message: "Object is possibly 'undefined'.", + Range: lsproto.Range{ + Start: lsproto.Position{Line: 0, Character: 0}, + End: lsproto.Position{Line: 0, Character: 4}, + }, + }, + { + Code: &lsproto.IntegerOrString{Integer: new(int32(18016))}, + Message: "Private identifiers are not allowed outside class bodies.", + Range: lsproto.Range{ + Start: lsproto.Position{Line: 0, Character: 5}, + End: lsproto.Position{Line: 0, Character: 7}, + }, + }, + }) +}