Skip to content

Using getTypeAtLocation on a const type reference creates a TS2304 diagnostic #34913

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

Closed
ark120202 opened this issue Nov 5, 2019 · 1 comment · Fixed by #36741
Closed

Using getTypeAtLocation on a const type reference creates a TS2304 diagnostic #34913

ark120202 opened this issue Nov 5, 2019 · 1 comment · Fixed by #36741
Labels
Bug A bug in TypeScript
Milestone

Comments

@ark120202
Copy link

TypeScript Version: 3.8.0-dev.20191102

Search Terms:

Code

const ts = require('typescript');

const content = '0 as const';

const host = ts.createCompilerHost({})
const originalReadFile = host.readFile;
host.readFile = (fileName) => fileName === 'main.ts' ? content : originalReadFile(fileName);

const program = ts.createProgram({ host, rootNames: ['main.ts'], options: { types: [] } });
const typeChecker = program.getDiagnosticsProducingTypeChecker();
const sourceFile = program.getSourceFile('main.ts');

typeChecker.getTypeAtLocation(sourceFile.statements[0].expression.type)

console.log(program.getSemanticDiagnostics());

Expected behavior:

getSemanticDiagnostics to return an empty array

Actual behavior:

TS2304: Cannot find name 'const'.

Because of caching, it has noticeable effects only when getSemanticDiagnostics is called after getTypeAtLocation.

It happens only when internal getDiagnosticsProducingTypeChecker method is used, but I think it still might be undesirable and would be an issue if #28584 would be merged.

Playground Link:

Related Issues:

@RyanCavanaugh
Copy link
Member

I was expecting this to be a problem of not calling getTypeFromTypeNode instead, but it isn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants