Skip to content
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

Problem with type inference. Caching? #3293

Closed
d0ruk opened this issue May 28, 2015 · 2 comments
Closed

Problem with type inference. Caching? #3293

d0ruk opened this issue May 28, 2015 · 2 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@d0ruk
Copy link

d0ruk commented May 28, 2015

Hello,

When I start a new .ts file and do;

var name:string;
name = "foo";

It compiles as expected.

Then I change name's type;

var name:string[];
name = ["foo","bar","baz"];

Compiler complains thus;

test.ts(1,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'name' must be of type 'string', but here has type 'string[]'.
test.ts(2,1): error TS2322: Type 'string[]' is not assignable to type 'string'.

This stops when I change the name to something else;

var spam:string[];
name = ["foo","bar","baz"];

When I try to reproduce it with different names other than name, problem seems to go away. I think this is either related to the var name (reserved word?) or some caching.

-d

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label May 28, 2015
@RyanCavanaugh
Copy link
Member

The global name is exposed in TypeScript.

https://developer.mozilla.org/en-US/docs/Web/API/Window/name

When you write var name at top-level, that doesn't create a new variable:

var name: string;
console.log(name); // Expected: undefined, actual: "" (depending)

We don't let you write var name: string[] because that identifier already has a meaning at global scope.

@mhegazy
Copy link
Contributor

mhegazy commented May 28, 2015

This has been a common source if confusion. issue #1351 is tracking removing the definition of "name" and "length" from the library to avoid these issues.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants