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

Add store and attrs types as generics (backwards compatible) #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AlistairDavidson
Copy link

Currently the store and attrs are typed as Record<string, any> and Record<string, string> respectively.

This change allows them to be typed more strictly. This PR is the backwards compatible version of the change, defaulting them to their existing types. A strict patch has also been submitted.

// /api/hello.mts
import type { EnhanceApiFn } from "@enhance/types";

export interface HelloStore {
  name: string;
}

export const get: EnhanceApiFn<HelloStore> = async (req) => ({
  json: {
    name: "Alice"
  }
});

// /pages/hello.mts
import { EnhanceElemFn } from "@enhance/types";
import { HelloStore } from "../api/hello.mjs";

const HelloPage: EnhanceElemFn<unknown, HelloStore> =
    ({ html, state: { store: { data} } }) =>
        html`
            <say-hello salutation="hello"></say-hello>
        `;

export default HelloPage;

// /elements/say-hello.mts
import { EnhanceElemFn } from "@enhance/types";
import { HelloStore } from "../api/hello.mjs";

export interface SayHelloAttrs {
  salutation: string;
}

const SayHello: EnhanceElemFn<SayHelloAttrs, HelloStore> =
	({ html, state: { store: { name }, attrs: { salutation } } }) => html`
		<p>${salutation}, ${name}!</p>
	`

export default SayHello;

Default to old existing value of Record <> for backwards compatibility
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.

1 participant