Skip to content

Commit 44f8a23

Browse files
authored
Merge pull request #222 from github/improve-type-definition-for-custom-element-class-instance
improve type definition for custom element class/instance
2 parents 1c20887 + d8a00d0 commit 44f8a23

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/attr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {CustomElement} from './custom-element.js'
1+
import type {CustomElementClass} from './custom-element.js'
22
import {dasherize} from './dasherize.js'
33
import {meta} from './core.js'
44

@@ -82,7 +82,7 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
8282

8383
const attrToAttributeName = (name: string) => `data-${dasherize(name)}`
8484

85-
export function defineObservedAttributes(classObject: CustomElement): void {
85+
export function defineObservedAttributes(classObject: CustomElementClass): void {
8686
let observed = classObject.observedAttributes || []
8787
Object.defineProperty(classObject, 'observedAttributes', {
8888
configurable: true,

src/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {CatalystDelegate} from './core.js'
2-
import type {CustomElement} from './custom-element.js'
2+
import type {CustomElementClass} from './custom-element.js'
33
/**
44
* Controller is a decorator to be used over a class that extends HTMLElement.
55
* It will automatically `register()` the component in the customElement
66
* registry, as well as ensuring `bind(this)` is called on `connectedCallback`,
77
* wrapping the classes `connectedCallback` method if needed.
88
*/
9-
export function controller(classObject: CustomElement): void {
9+
export function controller(classObject: CustomElementClass): void {
1010
new CatalystDelegate(classObject)
1111
}

src/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {register} from './register.js'
22
import {bind, bindShadow} from './bind.js'
33
import {autoShadowRoot} from './auto-shadow-root.js'
44
import {defineObservedAttributes, initializeAttrs} from './attr.js'
5-
import type {CustomElement} from './custom-element.js'
5+
import type {CustomElementClass} from './custom-element.js'
66

77
const symbol = Symbol.for('catalyst')
88

99
export class CatalystDelegate {
10-
constructor(classObject: CustomElement) {
10+
constructor(classObject: CustomElementClass) {
1111
// eslint-disable-next-line @typescript-eslint/no-this-alias
1212
const delegate = this
1313

src/custom-element.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
export interface CustomElement {
2-
new (): HTMLElement
1+
export interface CustomElement extends HTMLElement {
2+
connectedCallback?(): void
3+
attributeChangedCallback?(name: string, oldValue: string | null, newValue: string | null): void
4+
disconnectedCallback?(): void
5+
adoptedCallback?(): void
6+
formAssociatedCallback?(form: HTMLFormElement): void
7+
formDisabledCallback?(disabled: boolean): void
8+
formResetCallback?(): void
9+
formStateRestoreCallback?(state: unknown, reason: 'autocomplete' | 'restore'): void
10+
}
11+
12+
export interface CustomElementClass {
13+
new (): CustomElement
314
observedAttributes?: string[]
15+
formAssociated?: boolean
416
}

src/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {CustomElement} from './custom-element.js'
1+
import type {CustomElementClass} from './custom-element.js'
22
import {dasherize} from './dasherize.js'
33

44
/**
@@ -8,7 +8,7 @@ import {dasherize} from './dasherize.js'
88
*
99
* Example: HelloController => hello-controller
1010
*/
11-
export function register(classObject: CustomElement): CustomElement {
11+
export function register(classObject: CustomElementClass): CustomElementClass {
1212
const name = dasherize(classObject.name).replace(/-element$/, '')
1313

1414
try {

0 commit comments

Comments
 (0)