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 52 commits into
base: main
Choose a base branch
from

Conversation

nicoburniske
Copy link

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 it's own {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,
    }
} 

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.

2 participants