How could I update the Knockout typing file to be

2019-05-08 02:06发布

I've started to work on modernizing the knockout library's TypeScript declaration file which is provided by the DefinitelyTyped project.

The declaration file uses some deprecated techniques, and because of this, it's inconvenient to consume it in a TypeScript 2.0+ application.

The current declaration file contains an interface called KnockoutStatic. Everything inside this should basically get exported with using the export declare var/function format (if I understand correctly the new declaration file best practices). Then at the end of the file there should be an export as namespace ko; statement to be able to consume it as an ambient module.

But rewriting the whole interface to this format is a lot of boring work, so I'm wondering if there exists any trick or syntax to grab this whole interface, and export it's members like they were on the top-level.

I tried several things, for example something like

declare var ko: KnockoutStatic;
export default ko;

But this of course resulted in having two problems.

  1. If I import using import ko from "knockout", then I can use my ko object just fine, but I don't have access to the interfaces like KnockoutObservable<T>, etc.
  2. If I import using import * as ko from "knockout; then I have access to interfaces via ko.KnockoutObservable<T> which is completely fine, BUT when I want to access the static, runtime ko stuff, I end up having another level of ko object when consuming, like ko.ko.observable, etc.

Another problem is that if I move all the KnockoutStatic stuff to the top level, then one can no longer add methods to ko for example, as one canot augment a module by adding top-level exports to it (at least this was a TypeScript limitation a few months ago inspired by an ES6 limitation). So from this point of view, the interface solution is better.

I's an important note, that this problem is quite common, as typings of lots of popular libraries suffer from this phenomenon (even ones like jQuery). For example jQuery has a JQueryStatic interface too which should be entirely "lifted" up one level IMO.

0条回答
登录 后发表回答