bytecodealliance / jco

JavaScript toolchain for working with WebAssembly Components
https://bytecodealliance.github.io/jco/
Apache License 2.0
618 stars 62 forks source link

Question: `jco types` generating namespaces instead of modules? #439

Open karthik2804 opened 4 months ago

karthik2804 commented 4 months ago

I am using jco types to create the typings to build an SDK based on the generated code, the generated types contain namespaces instead of modules which means IDE does not provide intellisense and also tsc fails to compile without //@ts-ignore the wit imports .

I have the following wit

package local:hello;

interface logger {
  log: func(val: string);
}

world hello {
    import logger; 
    export say-hello: func(name: string);
}

Using the following command to generate the types jco types -o types <path to wit> provides the following directory structure

types
├── hello.d.ts
└── interfaces
    └── local-hello-logger.d.ts

And the contents of the files are

// hello.d.ts
import { LocalHelloLogger } from './interfaces/local-hello-logger.js';
export function sayHello(name: string): void;

// local-hello-logger.d.ts
export namespace LocalHelloLogger {
  export function log(val: string): void;
} 

And the following is the guest code

// hello.js
import * as logger from 'local:hello/logger';

export function sayHello(name) {
  logger.log(`Hello ${name}`);
}

I could get around this by manually editing the generated types to be the following

declare module "local:hello/logger" {
  export function log(val: string): void;
}

Please let me know if I am missing something here. Thanks!

guybedford commented 4 months ago

Explicitly declaring the imports sounds like a sensible approach. The current types are primiarly for jco transpile, where the imports don't matter to the end user as they are encapsulated, so they only matter so far as they are reexported.

In this guest generation case, it definitely makes sense to generate a declare module output for all imports. It would be nice to add a new flag for this which outputs this new form. --declare-imports or similar perhaps.

cdata commented 4 months ago

I just ran into this myself. I would be happy to look into contributing the change, if you're looking for help.

guybedford commented 4 months ago

@cdata no one is currently working on this, a PR would be great if you're interested in contributing.

karthik2804 commented 3 weeks ago

@cdata just wanted to check in if you were working on this issue. I would be happy to look into it otherwise