diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c003090aaf45d..280db2fb93ba6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11337,6 +11337,11 @@ namespace ts { function getTypeFromTypeReference(node: TypeReferenceType): Type { const links = getNodeLinks(node); if (!links.resolvedType) { + // handle LS queries on the `const` in `x as const` by resolving to the type of `x` + if (isConstTypeReference(node) && isAssertionExpression(node.parent)) { + links.resolvedSymbol = unknownSymbol; + return links.resolvedType = checkExpressionCached(node.parent.expression); + } let symbol: Symbol | undefined; let type: Type | undefined; const meaning = SymbolFlags.Type; diff --git a/src/testRunner/unittests/programApi.ts b/src/testRunner/unittests/programApi.ts index 3b71fea7238c1..8b13792d35ea5 100644 --- a/src/testRunner/unittests/programApi.ts +++ b/src/testRunner/unittests/programApi.ts @@ -180,6 +180,17 @@ namespace ts { }); describe("unittests:: programApi:: Program.getDiagnosticsProducingTypeChecker / Program.getSemanticDiagnostics", () => { + it("does not produce errors on `as const` it would not normally produce on the command line", () => { + const main = new documents.TextDocument("/main.ts", "0 as const"); + + const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main], cwd: "/" }); + const program = createProgram(["/main.ts"], {}, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed })); + const typeChecker = program.getDiagnosticsProducingTypeChecker(); + const sourceFile = program.getSourceFile("main.ts")!; + typeChecker.getTypeAtLocation(((sourceFile.statements[0] as ExpressionStatement).expression as AsExpression).type); + const diag = program.getSemanticDiagnostics(); + assert.isEmpty(diag); + }); it("getSymbolAtLocation does not cause additional error to be added on module resolution failure", () => { const main = new documents.TextDocument("/main.ts", "import \"./module\";"); const mod = new documents.TextDocument("/module.d.ts", "declare const foo: any;");