Fix getTypeAtLocation for as const to not issue a diagnostic (#36741)

* Fix getTypeAtLocation for `as const` to not issue a diagnostic

* use existing helpers for checks

* Fix lint
This commit is contained in:
Wesley Wigham 2020-02-12 13:43:02 -08:00 committed by GitHub
parent 54102336f2
commit 2b64731a97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -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;

View file

@ -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;");