Your TypeError is a JavaScript runtime error and not (directly) related to TS type system.
When you use declare namespace, you expect a third party library script to provide the namespace/property on the global namespace for you at runtime. declare xxx keyword is only relevant for compile type, e.g. to make global variables known for TS compiler. If there is no global property provided later on, you get above error.
So, if you have a third party namespace you depend on, you'll want to write:
declare namespace Space.NewSpace {
enum TestEnum {
Foo,
Bar
}
}
... and the library would provide the namespace similar to this:
var Space = {
NewSpace: {
// enum simplified here
TestEnum: {"0": "Foo", "1": "Bar", Foo: 0, Bar: 1}
}
};
If you write a namespace for your own code, leave out the declare keyword and export the members.
namespace Space.NewSpace {
export enum TestEnum {
Foo,
Bar
}
}
console.log(Space.NewSpace.TestEnum[0]); // Foo
declare? Unless you're importing the namespace from somewhere else?Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.