Skip to content

Display write type for property accesses in write locations #54777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts'/>

// @strict: true
// @exactOptionalPropertyTypes: true
//// declare const xx: { prop?: number };
//// xx.prop/*1*/ = 1;

verify.quickInfoAt('1', '(property) prop?: number');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts'/>

// @strict: true
// @exactOptionalPropertyTypes: true
//// declare const xx: { prop?: number };
//// xx.prop/*1*/ += 1;

verify.quickInfoAt('1', '(property) prop?: number');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts'/>

// @strict: true
// @exactOptionalPropertyTypes: true
//// declare const xx: { prop?: number };
//// xx.prop/*1*/ ??= 1;

verify.quickInfoAt('1', '(property) prop?: number');
11 changes: 11 additions & 0 deletions tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts'/>

// @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');
11 changes: 11 additions & 0 deletions tests/cases/fourslash/quickInfoOnPropertyAccessInWriteLocation5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts'/>

// @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');