microsoft / microsoft/TypeScript
Merging constants with namespaces
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
I wrote this in closed #13536 first, but thought it might get lost there, so filing a new issue.
Merging consts with namespaces really makes sense. For a real-world example, let's take a popular library: TinyMCE, writing proper type definitions for which requires this feature. Namespaces are really convenient in this case because all the classes from the API are attached to the tinymce namespace object or to its subnamespaces. In the API, there are normal classes like tinymce.Editor and so called 'static classes' like tinymce.EditorManager which are just a type (interface) plus an object value of this type attached to the namespace:
declare namespace tinymce {
// normal class
class Editor {
show(): void;
// ...
}
// "static class"
interface EditorManager {
activeEditor: Editor;
// ...
}
const EditorManager: EditorManager;
// ...
}
Nothing unusual so far, but there is a plot twist. The namespace object tinymce itself implements the tinymce.EditorManager interface. It could be easily and beautifully solved by merging a const with the namespace:
declare const tinymce: tinymce.EditorManager;
declare namespace tinymce {
// ... see the previous snippet
}
But unfortunately this isn't allowed. The error message is: Cannot redeclare block-scoped variable 'tinymce'.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the tinymce declaration snippets and reading the declaration-merging behavior that produces the reported block-scoped redeclaration error. Done means the const and namespace can be merged for this example without regressing existing declaration rules.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100