0
votes

I'm trying to write some TypeScript declaration file, then found that it is legal to declare a const and assign a value to it.

declare const foo = 1;        // This is legal
declare const bar = 'b';      // This is legal too
declare const baz = () => {}; // ERROR: A 'const' initializer in an ambient context must be a string or numeric literal.

declare var foo1 = 1;      // ERROR: Initializers are not allowed in ambient contexts.
declare let bar1 = 2;      // ERROR: Initializers are not allowed in ambient contexts.
declare function baz1() {} // ERROR: An implementation cannot be declared in ambient contexts.

In my understanding, it should be illegal to assign value in declare statement.

I know in const statement, the type of foo can be infer to 1, However, Isn't declare const foo: 1 a better declaration?

Why does TypeScript allow declare const to be assigned a value?

2

2 Answers

1
votes

I can't tell you for sure why it is the way it is, but here's my understanding. I don't think this is explicitly described in the official documentation but this seems to make the most sense to me. Considering the spec states the assignment should not be possible, it seems like a bug in the compiler to me.

AmbientDeclaration:
   declare AmbientVariableDeclaration

AmbientVariableDeclaration:
   var AmbientBindingList ;
   let AmbientBindingList ;
   const AmbientBindingList ;

AmbientBindingList:
   AmbientBinding
   AmbientBindingList , AmbientBinding

AmbientBinding:
   BindingIdentifier TypeAnnotation_opt

When you declare a variable, you're merely stating to the compiler that some symbol by that name should be assumed to exist. The actual implementation will be provided elsewhere. Nothing will actually be emitted for that declaration.

Usually when you use declare, you would also provide a type. In this context, a number or string may be used since they are both literals, immutable and valid constant values and the compiler can infer what the type the symbol should be. Otherwise the value you provide has no other effect.

I agree this is confusing and it probably would have made a lot more sense if the assignment was disallowed. In the ambient context, you're just providing information about symbols and types that should be available.

As for why the others do not work:

  • baz - you're attempting to assign a non-constant value to a declaration
  • foo1, bar1 - you're attempting to assign non-constant values to non-const variables
  • baz1 - you're declaring a function with some implementation (does nothing). To declare a function, you must use the function syntax with no body, a "prototype"

    declare function baz(): void; // function baz returns void
    
0
votes

Firstly, declare accept variable value, regarding to https://www.typescriptlang.org/docs/handbook/declaration-merging.html

Secondly :1 defines type of foo, not value of foo, so you can't use that because it is to point out that foo can only be 1, but not to assign 1 to foo.

Hope this help