Skip to content

Proposal for typescript definition file #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2020

Conversation

relu91
Copy link
Member

@relu91 relu91 commented Sep 23, 2020

I finally found a solution to have our typescript definition file work both in module and no-module environments. Practically this means that tsc will validate both of those code snippets:

WoT.produce(td).then((thing:WoT.ExposedThing)=>{/*...*/});
import {produce,ExposedThing} from "WoT";
produce(td).then((thing:ExposedThing)=>{/*...*/});

As a example the first one might be written inside WoT scripts (which may not support a module system) and the latter for runtimes who want to use node-wot as a library.

Since I had to format the file it is quite difficult to undestand the change. Essentially I changed the declaration of the namespace from export as namespace WoT; to:

declare namespace WoT {
/* declarations */
}

Finally I exposed every field in the namespace as a module with the following:

declare module "WoT" {
    export = WoT;
}

I created a PR since I think this is the final solution.

Fixes #215 .

Now ts definition file supports both module and no module system
environments.
@danielpeintner danielpeintner merged commit d23b65e into w3c:master Sep 24, 2020
@danielpeintner
Copy link
Contributor

I published it as "0.8.0-SNAPSHOT.2" on npm.

Moreover, I also updated v0.8.x branch of node-wot but I did run into issues like

src/thing-description.ts(17,22): error TS2306: File 'C:/Projects/WoT/thingweb/thingweb.node-wot/packages/td-tools/node_modules/wot-typescript-definitions/index.d.ts' is not a module.

Currently we use the following which does not work anymore.

import * as WoT from "wot-typescript-definitions";

@relu91 Can you let me know how it is assumed to work. Thanks!

Note: The TS definitions still miss the new Subscription (I will add/update once the issues are resolved).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[typescript] Use namespace instead of interface for WoT functions
3 participants