Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Incorrect type of implicitly typed const variables #222

Closed
michaelneu opened this issue Jan 6, 2017 · 2 comments
Closed

Incorrect type of implicitly typed const variables #222

michaelneu opened this issue Jan 6, 2017 · 2 comments

Comments

@michaelneu
Copy link
Contributor

The implicit type of const variables yields the initialization value of the variable:

main.ts

import { getInfoFromString } from "ts-type-info";

const code = `

var a = 10;
let b = 10;
const c = 10;

`;

const info = getInfoFromString(code);

for (const variable of info.variables) {
    console.log(`${variable.declarationType} ${variable.name}: ${variable.type.text};`);
}

Output with [email protected] and [email protected]

var a: number;
let b: number;
const c: 10;

This might be due to changes in the compiler API from TypeScript 2.0.10 to 2.1.1. npm install ts-type-info installed the latest version (2.1.4), which led to the output above. When uninstalling typescript, and manually installing 2.0.10, the output will look as follows:

Output with [email protected] and [email protected]

var a: number;
let b: number;
const c: number;

I don't know what this change might be yet, but if you want, I can take a look into it by the end of next week.

michaelneu pushed a commit to michaelneu/ts-type-info that referenced this issue Jan 6, 2017
@michaelneu michaelneu changed the title Incorrect type of implicitly typed const variables Incorrect type of implicitly typed const variables Jan 6, 2017
@dsherret dsherret mentioned this issue Jan 6, 2017
@dsherret
Copy link
Owner

dsherret commented Jan 6, 2017

This is new behaviour in TS 2.1 outlined here. You can also see the type of 10 if you hover over the variable name here (https://www.typescriptlang.org/play/#src=const%20c%20%3D%2010%3B).

On first glance, I'm going to say we should go with the change the typescript team made.

@michaelneu
Copy link
Contributor Author

Thanks for your quick answer.

You're right, the choice of the typescript team seems logical, thus changing the behaviour here would be odd. Closing as "intended behaviour".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants