Initialiser in ambient context

Topics: Language Specification
Jun 29, 2013 at 3:18 PM
According to specification:
3.7.2.2:
OptionalParameter:
  PublicOrPrivateopt Identifier ? TypeAnnotationopt
  PublicOrPrivateopt Identifier TypeAnnotationopt Initialiser
But if you write:
declare function f(a = 1);
You get "error TS1039: Initializers are not allowed in ambient contexts."
This means that specification is not correct, we should have OptionalParameter and AmbientOptionalParameter which doesn't allow Initialisers.
Jul 17, 2013 at 8:33 PM
Edited Jul 17, 2013 at 8:34 PM
Does somebody know the answer ?
Developer
Jul 17, 2013 at 8:53 PM
The grammar alone is not going to accurately describe any and all legal programs while excluding all illegal ones. The production for VariableDeclaration is:

Identifier TypeAnnotation(opt) Initialiser(opt)

but would you say the specification is not correct since this illegal statement satisfies the grammar?

var x: number = "hello";

There is a point at which additional productions are adding complexity (and maintenance cost) for little gain. As the example above illustrates, there are important implementation systems not captured by the grammar that will always result in code that satisfies the grammar but not the compiler itself. The language specification has numerous additional checks described in prose which are not captured by the grammar.