Skip to content

Commit

Permalink
feat: support type constraints for generic types (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Oct 8, 2024
1 parent 0c41bb8 commit e12ca66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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))
: 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

0 comments on commit e12ca66

Please sign in to comment.