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

feat: support type constraints for generic types #257

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions scripts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ function addGenerics(node: Node, elementName: string) {
}

const genericTypeNames = createGenericTypeNames(genericElementInfo.numberOfGenerics);
const typeParameters = genericTypeNames.map((id) => ts.factory.createTypeParameterDeclaration(undefined, id));

const typeParameters = genericTypeNames.map((id, index) => {
const typeConstraint = genericElementInfo.typeConstraints?.[index];
const typeReference = typeConstraint ? ts.factory.createTypeReferenceNode(typeConstraint) : undefined;
// If typeConstraint is provided, use it as constraint and default type for the type parameter.
return ts.factory.createTypeParameterDeclaration(undefined, id, typeReference, typeReference);
});
const typeArguments = genericTypeNames.map((id) => ts.factory.createTypeReferenceNode(id));

const isEventMapGeneric = !genericElementInfo.nonGenericInterfaces?.includes(NonGenericInterface.EVENT_MAP);
Expand Down Expand Up @@ -220,12 +226,12 @@ function addGenerics(node: Node, elementName: string) {
// const events = {
// onActiveItemChanged: "active-item-changed",
// ...
// } as GridEventMap<unknown>;
// } as GridEventMap<any>;
// ^ adding this type argument
return ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(EVENT_MAP),
isEventMapGeneric
? genericTypeNames.map(() => ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword))
? genericTypeNames.map(() => ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword))
Comment on lines -228 to +234
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needed to change the generic type of the Event Map passed to createComponent to any since unknown doesn't satisfy the constraint:

Screenshot 2024-08-26 at 16 47 42

: undefined,
);
}
Expand Down Expand Up @@ -311,13 +317,15 @@ function generateReactComponent({ name, js }: SchemaHTMLElement, { packageName,
const { remove: eventsToRemove, makeUnknown: eventsToBeUnknown } = eventSettings.get(elementName) ?? {};
const hasKnownEvents =
namedEvents?.some(({ name }) => !eventsToRemove?.includes(name) && !eventsToBeUnknown?.includes(name)) || false;
const genericElementInfo = genericElements.get(elementName);

const ast = template(
`
import type { EventName } from "${LIT_REACT_PATH}";
import {
${COMPONENT_NAME} as ${COMPONENT_NAME}Element
type ${COMPONENT_NAME}EventMap as _${COMPONENT_NAME}EventMap,
${[...new Set(genericElementInfo?.typeConstraints || [])].map((constraint) => `type ${constraint}`)}
} from "${MODULE_PATH}";
import * as React from "react";
import { createComponent, type WebComponentProps } from "${CREATE_COMPONENT_PATH}";
Expand Down
1 change: 1 addition & 0 deletions scripts/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum NonGenericInterface {
export type GenericElementInfo = Readonly<{
numberOfGenerics: number;
nonGenericInterfaces?: readonly NonGenericInterface[];
typeConstraints?: string[];
}>;

export const genericElements = new Map<string, GenericElementInfo>([
Expand Down
Loading