Skip to content

Typescript type improvements #481

Description

@MartinElsaesser

Hi @Tao-VanJS,
just found your Library VanJS.
What a great, small, dependency free frontend library.
Have been searching for a library like this one for a while.
Thank you for the work you put into it.
Although it was super easy to use, the typescript support, still has ways to go.
After taking a look at your source code, I am confident, that I can improve the typescript types and thus make the developer experience better.

I'd like to improve upon the type definitions for the van.tags props.
From my perspective, we should separate the types of normal van.tags and those with a namespace URI.
The type of the normal van.tags should be the keys and values of the created HTML element.
The values can be the exact typescript value, or wrapped within a State.
The namespace van.tags would stay largely the same.
To illustrate: I am thinking something along those lines for typing the normal van.tags

export type Props = { class?: PropValueOrDerived; is?: string }
export type HTMLProps<ElementType> = Partial<{[K in keyof ElementType]: ElementType[K] | StateView<ElementType[K]>}>
export type HTMLTagFunc<Result> = (first?: Props & HTMLProps<Result> , ...rest: readonly ChildDom[]) => Result

type HTMLTags = {
  [K in keyof HTMLElementTagNameMap]: HTMLTagFunc<HTMLElementTagNameMap[K]>
}

Be aware, that I would have to look a lot deeper into your library to determine, if the types proposed here are correct.
But before investing this effort, I want to make sure, that you are interested in a better typescript experience.

If you are open to a PR, I'd start the work on improving the typescript types in the next couple of days.
I take it, you want to keep the main code in js and have the typescript types in the .d.ts files.
If you ever want to migrate the whole codebase to typescript, I'd be happy to help = ).
In my experience a typescript first code base is more robust, easier to test and more ergonomic to work in.

Let me know, if you'd like me to rework the van.tags props types.

edit: The typescript types also include types for svg and mathML, which means, we could provide type support for those as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions