From b8b0d26cb9b9b0573affc6cb2faaa6227db9f49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 15 Aug 2023 21:01:21 +0200 Subject: [PATCH] Display write type for property accesses in write locations (#54777) --- src/compiler/checker.ts | 8 ++++++-- .../quickInfoOnPropertyAccessInWriteLocation1.ts | 8 ++++++++ .../quickInfoOnPropertyAccessInWriteLocation2.ts | 8 ++++++++ .../quickInfoOnPropertyAccessInWriteLocation3.ts | 8 ++++++++ .../quickInfoOnPropertyAccessInWriteLocation4.ts | 11 +++++++++++ .../quickInfoOnPropertyAccessInWriteLocation5.ts | 11 +++++++++++ 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation1.ts create mode 100644 tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation2.ts create mode 100644 tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation3.ts create mode 100644 tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation4.ts create mode 100644 tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 51cee644b17..06c2808a393 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11541,7 +11541,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { getWriteTypeOfSymbolWithDeferredType(symbol) || getTypeOfSymbolWithDeferredType(symbol) : // NOTE: cast to TransientSymbol should be safe because only TransientSymbols can have CheckFlags.SyntheticProperty (symbol as TransientSymbol).links.writeType || (symbol as TransientSymbol).links.type! : - getTypeOfSymbol(symbol); + removeMissingType(getTypeOfSymbol(symbol), !!(symbol.flags & SymbolFlags.Optional)); } if (symbol.flags & SymbolFlags.Accessor) { return checkFlags & CheckFlags.Instantiated ? @@ -27703,7 +27703,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { location = location.parent; } if (isExpressionNode(location) && (!isAssignmentTarget(location) || isWriteAccess(location))) { - const type = removeOptionalTypeMarker(getTypeOfExpression(location as Expression)); + const type = removeOptionalTypeMarker( + isWriteAccess(location) && location.kind === SyntaxKind.PropertyAccessExpression ? + checkPropertyAccessExpression(location as PropertyAccessExpression, /*checkMode*/ undefined, /*writeOnly*/ true) : + getTypeOfExpression(location as Expression) + ); if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { return type; } diff --git a/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation1.ts b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation1.ts new file mode 100644 index 00000000000..81299002a12 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation1.ts @@ -0,0 +1,8 @@ +/// + +// @strict: true +// @exactOptionalPropertyTypes: true +//// declare const xx: { prop?: number }; +//// xx.prop/*1*/ = 1; + +verify.quickInfoAt('1', '(property) prop?: number'); diff --git a/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation2.ts b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation2.ts new file mode 100644 index 00000000000..02babc99c22 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation2.ts @@ -0,0 +1,8 @@ +/// + +// @strict: true +// @exactOptionalPropertyTypes: true +//// declare const xx: { prop?: number }; +//// xx.prop/*1*/ += 1; + +verify.quickInfoAt('1', '(property) prop?: number'); diff --git a/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation3.ts b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation3.ts new file mode 100644 index 00000000000..6b2e7092c33 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation3.ts @@ -0,0 +1,8 @@ +/// + +// @strict: true +// @exactOptionalPropertyTypes: true +//// declare const xx: { prop?: number }; +//// xx.prop/*1*/ ??= 1; + +verify.quickInfoAt('1', '(property) prop?: number'); diff --git a/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation4.ts b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation4.ts new file mode 100644 index 00000000000..acf55817c39 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation4.ts @@ -0,0 +1,11 @@ +/// + +// @strict: true +//// interface Serializer { +//// set value(v: string | number | boolean); +//// get value(): string; +//// } +//// declare let box: Serializer; +//// box.value/*1*/ = true; + +verify.quickInfoAt('1', '(property) Serializer.value: string | number | boolean'); diff --git a/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation5.ts b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation5.ts new file mode 100644 index 00000000000..06ea4ba9166 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation5.ts @@ -0,0 +1,11 @@ +/// + +// @strict: true +//// interface Serializer { +//// set value(v: string | number); +//// get value(): string; +//// } +//// declare let box: Serializer; +//// box.value/*1*/ += 10; + +verify.quickInfoAt('1', '(property) Serializer.value: string | number');