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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

type declarations for typescript #326

Open
benjamine opened this issue Feb 2, 2022 · 2 comments
Open

type declarations for typescript #326

benjamine opened this issue Feb 2, 2022 · 2 comments

Comments

@benjamine
Copy link

benjamine commented Feb 2, 2022

hi 馃憢 , was wondering if there are, or you're interested in adding type declarations for typescript.
this could be created as a .d.ts file here, or added to the https://github.com/DefinitelyTyped/DefinitelyTyped repo.

an incipient example of how this would look

declare module '@observablehq/runtime' {
  export class Inspector {
    constructor(element: unknown);

    pending(...args: unknown[]): unknown;

    rejected(...args: unknown[]): unknown;

    fulfilled(...args: unknown[]): unknown;
  }
  export class Runtime {
    constructor();

    module(
      notebook: unknown,
      handler: (name: string) => Inspector | boolean,
    ): void;

    dispose(): void;
  }
}
@caleb-vear
Copy link

I would be interested in it. I am doing some work using the runtime at the moment and by the time I'm done it is likely I'll have a bunch of type definitions defined. So if it is something the observable team would be interested in accepting I could consider sending through a pull request for it.

@lionel-rowe
Copy link

Here's what I've got currently. Might open a PR at some point unless someone else wants to pick it up.

declare module 'https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js' {
    export class Runtime {
        constructor(builtins?: Builtins, global?: (name: string | symbol) => unknown)

        module(define: NotebookDefine, handler: GetObserver): Module
        dispose(): void
    }

    export class Inspector implements Required<Observer> {
        constructor(element: Element)

        static into(container: Element | string): () => Inspector

        pending(): void
        fulfilled(value: unknown, name: string | null): void
        rejected(error: unknown, name: string | null): void
    }

    export class Library implements Builtins {}
    export class RuntimeError extends Error {}

    class Module {
        variable(observer?: Observer): Variable

        derive(specifiers: string[], source: Module): Module

        define(name: string | null, value: unknown): Variable
        define(inputs: string[], getter: (...inputs: unknown[]) => unknown): Variable
        define(name: string | null, inputs: string[], getter: (...inputs: unknown[]) => unknown): Variable

        import(name: string, module: Module): Variable
        import(name: string, alias: string, module: Module): Variable

        redefine(name: string, value: unknown): Variable
        redefine(name: string, inputs: string[], getter: (...inputs: unknown[]) => unknown): Variable

        value(name: string): Variable
    }

    class Variable {
        define(name: string | null, value: unknown): this
        define(inputs: string[], getter: (...inputs: unknown[]) => unknown): this
        define(name: string | null, inputs: string[], getter: (...inputs: unknown[]) => unknown): this

        import(name: string, module: Module): this
        import(name: string, alias: string, module: Module): this

        delete(): void
    }

    type Observer = Partial<{
        pending(): void
        fulfilled(value: unknown, name: string | null): void
        rejected(error: unknown, name: string | null): void
    }>

    type Builtins = Partial<Record<string, unknown>>
    type NotebookDefine = (runtime: Runtime, observer: GetObserver) => Module
    type GetObserver = (name: string | undefined) => Observer | void
}

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

No branches or pull requests

3 participants