From 884d649846df6940b87c3f588069c2b4eb7256d2 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 12 Mar 2024 20:50:46 +0200 Subject: [PATCH] fix(57445): No inlay hints for property declaration types inferred from constructor (#57494) Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> --- src/services/inlayHints.ts | 6 +++++- .../reference/inlayHintsPropertyDeclarations.baseline | 9 +++++++++ tests/cases/fourslash/inlayHintsPropertyDeclarations.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index 6fd10deb353..5fdc91e2dab 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -118,6 +118,7 @@ import { tokenToString, TupleTypeReference, Type, + TypeFlags, unescapeLeadingUnderscores, UserPreferences, usingSingleLineStringWriter, @@ -260,7 +261,10 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] { } function visitVariableLikeDeclaration(decl: VariableDeclaration | PropertyDeclaration) { - if (!decl.initializer || isBindingPattern(decl.name) || isVariableDeclaration(decl) && !isHintableDeclaration(decl)) { + if ( + decl.initializer === undefined && !(isPropertyDeclaration(decl) && !(checker.getTypeAtLocation(decl).flags & TypeFlags.Any)) || + isBindingPattern(decl.name) || (isVariableDeclaration(decl) && !isHintableDeclaration(decl)) + ) { return; } diff --git a/tests/baselines/reference/inlayHintsPropertyDeclarations.baseline b/tests/baselines/reference/inlayHintsPropertyDeclarations.baseline index c3392e6f7c5..b184663bb19 100644 --- a/tests/baselines/reference/inlayHintsPropertyDeclarations.baseline +++ b/tests/baselines/reference/inlayHintsPropertyDeclarations.baseline @@ -6,4 +6,13 @@ "position": 15, "kind": "Type", "whitespaceBefore": true +} + + d; + ^ +{ + "text": ": number | null", + "position": 50, + "kind": "Type", + "whitespaceBefore": true } \ No newline at end of file diff --git a/tests/cases/fourslash/inlayHintsPropertyDeclarations.ts b/tests/cases/fourslash/inlayHintsPropertyDeclarations.ts index bf3fbab2a33..808b1f0df79 100644 --- a/tests/cases/fourslash/inlayHintsPropertyDeclarations.ts +++ b/tests/cases/fourslash/inlayHintsPropertyDeclarations.ts @@ -1,9 +1,18 @@ /// +// @strict: true //// class C { //// a = 1 //// b: number = 2 //// c; +//// d; +//// +//// constructor(value: number) { +//// this.d = value; +//// if (value <= 0) { +//// this.d = null; +//// } +//// } //// } verify.baselineInlayHints(undefined, {