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 (strict) #7

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 strict version of the change, defaulting them to unknown. This is the preferred type as it is correct, however the change will not be backwards-compatible. A backwards-compatible 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 unknown.
EnhancApiFnChain still uses old type of Record<string,any>.
@AlistairDavidson AlistairDavidson changed the title Add store and attrs types as generics Add store and attrs types as generics (strict) Dec 28, 2022
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