I have a Typescript definition file to create for a pre-existing Javascript library. Within this library, there are functions that can act as constructors or factories. How should I structure the typings to accommodate both?
For reference, here are specific examples of the calls outlined in the README:
var streamParser = N3.StreamParser();
var streamParser = new N3.StreamParser();
Currently, my approach only supports the factory method.
function Parser(options?: ParserOptions): N3Parser;
To enable the use of the new
keyword, creating a class seems like the obvious solution.
class Parser {
constructor(options? : ParserOptions);
}
However, these two approaches seem incompatible. The introduction to the deep dive into declaration files suggests compatibility is achievable, but lacks an example.
Note: The following two definitions are compatible, but with limitations...
interface Parser {
new: (options? : ParserOptions) => N3Parser;
}
function Parser(options? : ParserOptions): N3Parser;