@ydgp/core-types
v0.0.3-alpha.2
Published
TypeScript primitives for describing and registering web components
Readme
@ydgp/core-types
Small TypeScript primitives for describing and registering web components.
The package currently exports:
YfWebElement: a typed wrapper around a custom element constructor and its tag namedefineWebComponent(...): a safe registration helper that only callscustomElements.define(...)when the tag is not already registeredYfWebElementAttributesandYfWebElementListener: base type aliases for generic typingWritable,DeepWritable,DeepPartial, andOmitNever: utility conditional types for reshaping TypeScript object types
Installation
npm install @ydgp/core-typesUsage
import { YfWebElement, defineWebComponent } from '@ydgp/core-types';
class HelloCard extends HTMLElement {
connectedCallback() {
this.textContent = 'Hello';
}
}
const helloCard = new YfWebElement('hello-card', HelloCard);
defineWebComponent(helloCard);API
YfWebElement
new YfWebElement<A, L>(tagName, element)Stores:
tagName: the custom element tag to registerelement: theCustomElementConstructor
The generic parameters A and L are available for consumers that want to associate attribute and listener typings with a component definition.
defineWebComponent
defineWebComponent(webElement)Registers the custom element if customElements.get(webElement.tagName) does not already return a definition.
Conditional Types
Writable<T>: removesreadonlyfrom the top-level properties ofTDeepWritable<T>: recursively removesreadonlyfrom nested object propertiesDeepPartial<T>: recursively makes all properties optionalOmitNever<T>: removes keys whose value type resolves tonever
Runtime Notes
- This package targets browser environments with the Custom Elements API.
- TypeScript is compiled to ESM output in
dist/. - The package ships type declarations via
dist/index.d.mts.
Development
npm run lint
npm run build