Skip to content

Spec doesn't mention variables witnessing themselves through type annotations #402

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
danquirk opened this issue Aug 8, 2014 · 0 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification

Comments

@danquirk
Copy link
Member

danquirk commented Aug 8, 2014

var x: typeof x;

In section 5.1 Variable Statements we say:

A variable declaration introduces a variable with the given name in the containing declaration space. The type associated with a variable is determined as follows:
· If the declaration includes a type annotation, the stated type becomes the type of the variable. If an initializer is present, the initializer expression is contextually typed (section 4.19) by the stated type and must be assignable to the stated type, or otherwise a compile-time error occurs.
· If the declaration includes an initializer but no type annotation, and if the initializer doesn’t directly or indirectly reference the variable, the widened type (section 3.9) of the initializer expression becomes the type of the property. If the initializer directly or indirectly references the variable, the type of the variable becomes the Any type.
· If the declaration includes neither a type annotation nor an initializer, the type of the variable becomes the Any type.

The second bullet covers cases like:

var x = x;

but not cases like the initial example with no initializer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript 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

4 participants