From 7a755d2274c3d49aad553bd178d06d620c042081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 26 Jun 2023 12:29:35 +0200 Subject: [PATCH] Display write type for property accesses in write locations --- 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 ee86ae50306c7..2259632654456 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11484,7 +11484,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 ? @@ -27645,7 +27645,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 0000000000000..81299002a12e5 --- /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 0000000000000..02babc99c2250 --- /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 0000000000000..6b2e7092c33b2 --- /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 0000000000000..acf55817c39ee --- /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 0000000000000..06ea4ba9166d3 --- /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');