import type { ToAttributeValue, ToPropertyValue } from "@knyt/artisan"; import type { AnyProps, AttributeDictionary, ElementBuilder, InferElementProps, View } from "@knyt/weaver"; import { __isKnytElementDefinition } from "./constants.ts"; import type { KnytElement } from "./KnytElement.ts"; export type PropertyName = string | symbol; export type AttributeName = string | undefined | false; export type ContainerRefValue = HTMLElement | DocumentFragment | ShadowRoot | null; export type ReactiveProperty = PropertyDefinition.WithPropertyName>; export type ReactiveProperties = ReactiveProperty[]; /** * @public */ export type PropertyDefinition = { /** * ### Private Remarks * * This property only exists as a declaration and is only used for easier type inference. * * @internal scope: workspace */ Value: PropertyInfo.ToValue; AttributeName: PropertyInfo.ToAttributeName; /** * A function to compare two property values to determine if they are equal. * * Defaults to a strict equality check (`===`) if not provided. */ comparator?: (a?: PropertyInfo.ToValue, b?: PropertyInfo.ToValue) => boolean; attributeName?: PropertyInfo.ToAttributeName; toAttributeValue?: ToAttributeValue>; toPropertyValue?: ToPropertyValue>; }; export declare namespace PropertyDefinition { type ToValue = T extends PropertyDefinition ? PropertyInfo.ToValue : unknown; type ToAttributeName = T extends PropertyDefinition ? PropertyInfo.ToAttributeName : never; namespace ToAttributeName { type Valid = T extends PropertyDefinition ? PropertyInfo.ToAttributeName.Valid : never; } type ToAttributeValue = T extends PropertyDefinition ? PropertyInfo.ToAttributeValue : never; type WithPropertyName = PropertyDefinition & { propertyName: K; }; type FromValue = PropertyDefinition>; } /** * @public */ export type PropertyInfo = { Value: T; AttributeName: A; }; export declare namespace PropertyInfo { type ToValue = T extends PropertyInfo ? V : never; type ToAttributeName = T extends PropertyInfo ? A : never; namespace ToAttributeName { type Valid = T extends PropertyInfo ? A extends string ? A : never : never; } type ToAttributeValue = T extends PropertyInfo ? V extends boolean ? boolean : string | null : never; } export type PropertiesDefinition = { [K in keyof PropInfoDict]: PropInfoDict[K] extends PropertyInfo ? PropertyDefinition : PropInfoDict[K] extends PropertyDefinition ? PropInfoDict[K] : never; }; export declare namespace PropertiesDefinition { type ToAttributes = T extends PropertiesDefinition ? { [K in keyof B as PropertyInfo.ToAttributeName.Valid]: PropertyInfo.ToAttributeValue | undefined; } : T extends Record> ? { [K in keyof T as PropertyDefinition.ToAttributeName.Valid]: T[K] extends PropertyDefinition ? PropertyInfo.ToAttributeValue | undefined : never; } : never; type ToProps = T extends PropertiesDefinition ? { [K in keyof B]: B[K] extends PropertyInfo ? V | undefined : never; } : T extends Record> ? { [K in keyof T]: T[K] extends PropertyDefinition ? PropertyInfo.ToValue | undefined : never; } : never; /** * Infers a `PropertyInfoDictionary` from a given set of props. * * This type is primarily used to verify that a `PropertiesDefinition` is correct. * It should not be used to define a `PropertiesDefinition`, as it does not infer attribute names. * * @remarks * * Useful for resolving `TS7056` errors, where TypeScript encounters: * * > TS7056: The inferred type of this node exceeds the maximum length the * compiler will serialize. An explicit type annotation is needed. * * To resolve: * 1. Define a type for the expected props. * 2. Have the class implement the expected props type. * 3. Use `satisfies` to check if the `PropertiesDefinition` matches the expected props. * * @example * ```ts * type MyProps = { * foo?: string; * }; * * class MyElement extends KnytElement implements MyProps { * static properties = { * foo: define.property().string().attribute("foo"), * } satisfies PropertiesDefinition.FromProps; * * declare foo?: string; * } * ``` */ type FromProps = { [K in keyof T]: PropertyInfo; }; } /** * @internal scope: workspace */ export type HTMLElementConstructor = new () => E; /** * A definition for a custom element. * * @public */ export type ElementDefinition, A extends AttributeDictionary = KnytElement.ToAttributes> = ElementDefinition.Fn

& ElementDefinition.Static; export declare namespace ElementDefinition { /** * @internal scope: workspace * * @returns A builder for declaring the HTML element using properties. */ type Fn

= () => ElementBuilder.DOM

; /** * Static properties of an element definition for a `KnytElement`. * * @internal scope: workspace */ type Static> = BaseStatic; /** * Static properties of an element definition. * * @internal scope: module */ type BaseStatic = { /** * @returns A builder for declaring the HTML element using attributes. */ readonly html: () => ElementBuilder.HTML; /** * The constructor of the custom element. */ readonly Element: TConstructor; /** * The tag name of the custom element. */ readonly tagName: TTagName; /** * @internal scope: workspace */ readonly [__isKnytElementDefinition]: true; }; /** * @internal scope: workspace */ type ToStatic = T extends ElementDefinition ? Static : never; /** * An unknown element definition. * * @internal scope: workspace */ type Unknown = ElementDefinition; type FromPropertiesDefinition> = ElementDefinition, TN>; type ToConstructor> = T extends ElementDefinition ? TConstructor : never; /** * A definition for an element that is not a `KnytElement`. * * @public */ type Arbitrary, A extends AttributeDictionary = AttributeDictionary> = ElementDefinition.Fn

& ElementDefinition.BaseStatic; namespace Arbitrary { /** @internal scope: workspace */ type ToProps> = T extends Arbitrary ? P : never; } } /** * Infers reactive props from either a: * * - `PropertiesDefinition` * - `ElementDefinition` * - `ElementDefinition.Arbitrary` * - `KnytElement` constructor * - `View` * * @remarks * * To clarify, this type only infers reactive properties. * It does not infer all of the implied properties of a * an `HTMLElement`. * * @public */ export type InferProps = T extends View ? P : T extends KnytElement.Constructor.Unknown ? InferProps.ElementConstructor : T extends ElementDefinition ? InferProps.ElementConstructor> : T extends ElementDefinition.Arbitrary ? InferProps.ElementConstructor.Arbitrary : T extends PropertiesDefinition ? Partial> : never; export declare namespace InferProps { /** @internal scope: workspace */ type ElementConstructor = Partial>>; namespace ElementConstructor { /** @internal scope: workspace */ type Arbitrary> = Partial>; } /** @internal scope: workspace */ type HTML = InferElementProps.HTML; /** @internal scope: workspace */ type SVG = InferElementProps.SVG; /** @internal scope: workspace */ type DOM = InferElementProps.DOM; } //# sourceMappingURL=types.d.ts.map