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?