Skip to content

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

Closed
@danquirk

Description

@danquirk
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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issueSpecIssues related to the TypeScript language specification

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions