lefetmeofefet / yoffee

Minimal HTML one-way binding library
MIT License
15 stars 1 forks source link

Consider providing d.ts file with function/class delcarations #4

Closed hirako2000 closed 2 years ago

hirako2000 commented 3 years ago

It would add a lot of convenience to add a yoffee.d.ts file containing the type definitions of the functions and class exposed by the Yoffee library.

Why?

Our beloved typescript users would feel right at home and not have to declare the yoffee types in their own project to benefit from, out of the box:

Example, here is some naive developer making silly assumptions, despite the official doc being very clear on parameters to pass in, these things happen:

    let runtimeError = html("blah");
    let htmlFragment = html({some: 'value'});

    createYoffeeElement("html-element-runtime-error", {some: "value"});
    createYoffeeElement("html-element-name", (props: object, element: HTMLInputElement) => {
        props; element
    });
    createYoffeeElement("yoffee-html-element-name", new YoffeeElement({some: "value"}));

The typescript compiler isn't smart enough to warn/error on the incorrect types passed in for each flavor of creating an element. Of course, with plain javascript it's the same problem, got to wait and see things failing at runtime, but with typescript, granted declaration are provided by in the library module, or added it by the external project, things would look like this:

Monosnap 2021-09-29 16-51-17

Highlighting the issues. Along with a nice inline doc:

Monosnap 2021-09-29 16-58-42

Type declarations for this library is rather simple to create, Yoffee doesn't have a whole lot of exports, it looks something like this:

declare module "yoffee" {

    /**
     * Returns a `DocumentFragment` that has a one-way binding to each of the objects.
     * @param {...Object} propsObjects Holds the state of this yoffee element
     * @return a document fragment
     */
    function html(...propsObjects: object[]): any; 

    /**
     * 
     * @param elementName a name for this html web component element
     * @param element an arbitrary `Function`, or a (child of) `YoffeeElement`
     * @throws an error if the second parameter isn't either a `YoffeeElement` subclass or a `Function`
     */
    function createYoffeeElement(elementName: string, element: Function | YoffeeElement): void;

    /**
     * @type {YoffeeElement}
     */
    class YoffeeElement {
        constructor(state: object);
        updateProp(attr: object): void;
        disconnectedCallback(): void;

        /**
         * / This is here for when users call `super.connectedCallback()`. Prevent the crash :)
         */
        connectedCallback(): void

        /**
         * This is here for when users call `super.propUpdatedCallback()`. Prevent the crash. :(
         */
        propUpdatedCallback(): void;
    }
}
lefetmeofefet commented 2 years ago

Good idea! maybe i'll do it this week. I'm really sorry for not responding, I'm gonna dedicate some time now to make yoffee better for users. Are you still using it?

hirako2000 commented 2 years ago

no worries. I'm still using it yes, with types defined locally. intend to do some performance tests at some point but perceived performances is great so far even with frequent changes on large collections.

if you look into providing types, perhaps take a peek at console logging, as the console is getting sent a lot of statements that are just tracing lines about deleted templates when elements get removed.

hirako2000 commented 2 years ago

I'm happy to provide a PR for typings if you would welcome it.

lefetmeofefet commented 2 years ago

I'm happy to provide a PR for typings if you would welcome it.

Of course, that would be awesome!

lefetmeofefet commented 2 years ago

perhaps take a peek at console logging, as the console is getting sent a lot of statements that are just tracing lines about deleted templates when elements get removed.

Thanks I really should 🐸

hirako2000 commented 2 years ago

thanks! I will send a PR by the end of this week.

lefetmeofefet commented 2 years ago

Exciting :)) About the performance, I'm working on adding yoffee to , feel free to conduct your own tests and update me! And about the console loggings, they're removed in the build file. Are you sure you're using yoffee.min.js?

hirako2000 commented 2 years ago

I raised a separate issue for the console statements. #6