From 9cc30dc5ad190ebadabc6487b899ee7b77b31e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sat, 29 Jun 2024 21:08:36 +0200 Subject: [PATCH] Don't avoid caching variable types resolved using `CheckMode.TypeOnly` --- src/compiler/checker.ts | 4 ++-- .../fourslash/quickInfoCircularityInLoop1.ts | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/quickInfoCircularityInLoop1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a965c51d2286b..507ce3dd5ee86 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11985,7 +11985,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return false; } - function getTypeOfVariableOrParameterOrProperty(symbol: Symbol, checkMode?: CheckMode): Type { + function getTypeOfVariableOrParameterOrProperty(symbol: Symbol, checkMode = CheckMode.Normal): Type { const links = getSymbolLinks(symbol); if (!links.type) { const type = getTypeOfVariableOrParameterOrPropertyWorker(symbol, checkMode); @@ -11994,7 +11994,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // to preserve this type. In fact, we need to _prefer_ that type, but it won't // be assigned until contextual typing is complete, so we need to defer in // cases where contextual typing may take place. - if (!links.type && !isParameterOfContextSensitiveSignature(symbol) && !checkMode) { + if (!links.type && !isParameterOfContextSensitiveSignature(symbol) && !(checkMode & ~CheckMode.TypeOnly)) { links.type = type; } return type; diff --git a/tests/cases/fourslash/quickInfoCircularityInLoop1.ts b/tests/cases/fourslash/quickInfoCircularityInLoop1.ts new file mode 100644 index 0000000000000..19f6a8adbd1d2 --- /dev/null +++ b/tests/cases/fourslash/quickInfoCircularityInLoop1.ts @@ -0,0 +1,21 @@ +/// + +// @strict: true + +// @Filename: user.ts +//// declare const line: string; +//// let test: string | null = null; +//// +//// for (let i = 0; i < line.length; i++) { +//// const char = line[i]; +//// if (test === char) {} +//// const [|alsoChar/*1*/|] = char; +//// test = alsoChar; +//// } + +// if this circularity error ever goes awayt a different input code should be used here +verify.getSemanticDiagnostics([{ + code: 7022, + message: "'alsoChar' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.", +}]); +verify.quickInfoAt("1", "const alsoChar: any");