Skip to content
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

Typescript Stubgen Command #503

Open
wants to merge 53 commits into
base: main
Choose a base branch
from

Conversation

nicoburniske
Copy link

@nicoburniske nicoburniske commented Sep 20, 2024

Finally upstreaming what was originally done here #451

Introduces new command stubgen that will create all typescript declaration files from the corresponding wit types. There are rust tests for some of the complex cases

  • WIT exports get a stub interface type
  • Each WIT import interface gets its own {namespace}{package}{interface}.d.ts file

WIT Exports

This simple wit, will generate the following "stub" type for the user to then implement and export from their component

package test:basic;

interface greet {
    say-hello: func(name: string) -> string;
}

world test {
    export greet;
}

test.d.ts

export interface Greet {
    sayHello(name: string): string,
}

export interface TestWorld {
    greet: Greet,
}

So then developers can import the types, and implement them.

import { TestWorld, Greet } from './generated/test';

export const greet: Greet = {
  sayHello: (name: string) => {
    return `Hello, ${name}!`;
  }
};

WIT Imports

package test:types;

interface types {
    type dimension = u32;
    record point {
        x: dimension,
        y: dimension,
    }
}
package test:canvas;

interface canvas {
    use test:types/types.{dimension, point};
    type canvas-id = u64;
    draw-line: func(canvas: canvas-id, origin: point, target: point, thickness: dimension);
}

This would generate two typescript files, one for each interface

interfaces/test-canvas-canvas.d.ts

declare module "test:canvas/canvas" {
    import type { Dimension } from "test:types/types";
    import type { Point } from "test:types/types";
    export type CanvasId = bigint;
    export function drawLine(canvas: CanvasId, origin: Point, target: Point, thickness: Dimension): void;
} 

interfaces/test-types-types.d.ts

declare module "test:types/types" {
    export type Dimension = number;
    export interface Point {
        x: Dimension,
        y: Dimension,
    }
} 

Copy link
Collaborator

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic thanks for working on upstreaming this and providing such comprehensive tests as well. It would be great to provide for ComponentizeJS / StarlingMonkey workflows.

Some initial design space questions:

  • Would it make sense to extend this to full JS boilerplate creation, ie creating the JS files as well for stub implementations to fill in, possibly with // TODO comments or otherwise? It seems the missing piece remains importing the types and setting up the boilerplate and perhaps as we think about increasing automation in dev workflows this is actually what users want?
  • Instead of interfaces/namespace-package-interface.d.ts perhaps we should consider the structure interfaces/namespace/package/interface.d.ts since we can do folder nesting?

I will aim to do a full review soon, thanks for your patience!

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.

3 participants