Transitioning a JavaScript project to TypeScript has been challenging for me, especially when it comes to establishing a solid design pattern for the library's modularity.
Main Concept
The core functionality of my library is minimal. For instance, it may provide browser specifications as an object:
{
browser:"firefox",
version:"10.1.5",
platform:"linux",
screen-width:"1920",
// .. etc
}
This library will be installed through NPM. However, to fully utilize its capabilities, users must install "modules" that process the above-mentioned object in unique ways.
For example, one module might identify which CSS features are unsupported in a particular browser version:
{
unsupportedFeatures:[
"x",
"y",
"z",
// ... etc
]
}
Another module could take both the core library result and the previous module’s outcome to analyze the DOM for any unsupported CSS properties defined.
{
notSupported:[
"x",
"y",
// ... etc
]
}
Note: The mentioned functionalities are hypothetical examples illustrating how modules depend on the core library results as well as each other.
My Attempts So Far
I experimented with defining the core library as a function and adding modules as prototypes.
An issue I encountered was the lack of type definitions for both the core library and the added prototype functions.
I also explored defining the core library as a class, but this approach also led to compiler errors.
Lastly, merging namespace interface declarations yielded additional compilation issues.
Despite trying different solutions, none seemed to effectively work within the realm of TypeScript.
How can I tackle this challenge in TypeScript? What would be the ideal design pattern to follow for creating modular and extensible libraries while ensuring type safety and auto-completion?