Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Variables referenced indirectly in their initializers are resolved to 'any' #522

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
mhegazy opened this issue Aug 25, 2014 · 4 comments
Closed
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Aug 25, 2014

The compiler already behaves as per spec, and variables referenced directly or indirectly in their initializers resolve to any. The previous implementation did not just assign them to any, by attempted to go deeper one level, here are some examples;

var a = { f: a };

Now:

a : any

Was:

a: { f: any };

var fibonacci = _.memoize(n =>  n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); } );

Now:

fibonacci : any

Was:

fibonacci : (n: any) => any

var n1 = n1++;

Now:

n1 : any

Was:

n1 : number

see tests:

  • tests/cases/compiler/recursiveObjectLiteral.ts
  • tests/cases/compiler/underscoreTest1_underscoreTests.ts
  • tests/cases/conformance/statements/VariableStatements/recursiveInitializer.ts
@DanielRosenwasser DanielRosenwasser changed the title Variables referenced indirectelly in thier initializers resolves to 'any' Variables referenced indirectly in their initializers are resolved to 'any' Aug 25, 2014
@danquirk danquirk added the Bug label Aug 25, 2014
@sophiajt sophiajt self-assigned this Aug 25, 2014
@sophiajt sophiajt added this to the TypeScript 2.0 milestone Aug 25, 2014
@ahejlsberg
Copy link
Member

The new compiler follows the spec here and I think that makes sense. Also, see my reply to #523. I don't think "going one level deep" is a meaningful (or consistently implementable) rule.

@sophiajt
Copy link
Contributor

Just to be clear, this will differ from the 1.0 behavior, will be a breaking change, and also differs from your recommended "witnesses" proposal. It also doesn't appear to be a valuable improvement over the 1.0 behavior. If anything, we seem to be giving the developer less help rather than more.

That said, I recognize that the old behavior had inconsistencies.

Would it be possible to maintain the old behavior and address the inconsistencies?

@mhegazy
Copy link
Contributor Author

mhegazy commented Aug 26, 2014

I agree with Anders that this should not be supported. The previous implementation assigned two types to the same value, which is inconsistent and misleading.. consider this:

var x = ()=> { 
    x.blah; // not error, as x here is any
    return x;
}

x.blah; // Flagged as error as x here is a function ()=>any

I think this is misleading to the user, as they think the compiler understood their intention, where as it did not, and gives the illusion of safety and does not provide it.

A better solution is to make sure that noImplictAny flags theses cases with a message that the variable references itself in its initializer leading to an any type. we have an item for this already #476

@sophiajt sophiajt modified the milestones: TypeScript 2.0, TypeScript 1.2 Sep 2, 2014
@ahejlsberg
Copy link
Member

Fixed in #610.

@RyanCavanaugh RyanCavanaugh added the Fixed A PR has been merged for this issue label Sep 8, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification
Projects
None yet
Development

No branches or pull requests

5 participants