Skip to content

Commit c3d0e82

Browse files
committed
Merge branch 'main' into node-component
2 parents 930213f + 93134fd commit c3d0e82

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/Component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type ElementToTagName<T extends HTMLElement> = {
2020
[K in keyof HTMLElementTagNameMap]: HTMLElementTagNameMap[K] extends T ? K : never
2121
}[keyof HTMLElementTagNameMap];
2222

23+
type HtmlTagString<T extends HTMLElement> =
24+
`<${{ [K in keyof HTMLElementTagNameMap]: HTMLElementTagNameMap[K] extends T ? K : never }[keyof HTMLElementTagNameMap]}>${string}`
25+
| `<${{ [K in keyof HTMLElementTagNameMap]: HTMLElementTagNameMap[K] extends T ? K : never }[keyof HTMLElementTagNameMap]} ${string}`;
26+
2327
/**
2428
* An {@link !HTMLElement} component.
2529
*
@@ -44,7 +48,7 @@ export class Component<T extends HTMLElement = HTMLElement> extends ElementCompo
4448
*
4549
* Note: only the first child of the HTML code will be used.
4650
*/
47-
public static from<T extends HTMLElement = HTMLElement>(html: `<${ElementToTagName<T>}${">" | " "}${string}`) {
51+
public static from<T extends HTMLElement = HTMLElement>(html: HtmlTagString<T>) {
4852
return new Component<T>(document.createRange().createContextualFragment(html).children[0] as T);
4953
}
5054

src/ElementComponent.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ import {NodeComponent} from "./NodeComponent.js";
1919
/**
2020
* Non-readonly non-method keys
2121
*/
22-
type WritableKeys<T> = Extract<
23-
{
24-
[Prop in keyof T]: (
25-
(<G>() => G extends Pick<T, Prop> ? 1 : 2) extends
26-
(<G>() => G extends Record<Prop, T[Prop]> ? 1 : 2)
27-
? true
28-
: false
22+
type WritableKeys<T> = {
23+
[Prop in keyof T]: (
24+
(<G>() => G extends Pick<T, Prop> ? 1 : 2) extends (
25+
<G>() => G extends Record<Prop, T[Prop]> ? 1 : 2
26+
) ? true : false
2927
) extends false
3028
? never
31-
: Prop;
32-
}[keyof T],
33-
{
34-
[K in keyof T]: T[K] extends Function ? never : K;
35-
}[keyof T]
36-
>;
29+
: (T[Prop] extends Function | null | undefined ? never : Prop);
30+
}[keyof T];
31+
32+
/**
33+
* Non-method keys
34+
*/
35+
type ReadableKeys<T> = {
36+
[Prop in keyof T]: T[Prop] extends Function | null | undefined ? never : Prop;
37+
}[keyof T];
3738

3839

3940
/**
@@ -147,7 +148,7 @@ export abstract class ElementComponent<T extends Element> extends NodeComponent<
147148
* Get element property
148149
* @param name property name
149150
*/
150-
public get<K extends WritableKeys<T>>(name: K): T[K] {
151+
public get<K extends ReadableKeys<T>>(name: K): T[K] {
151152
return this.node[name];
152153
}
153154

0 commit comments

Comments
 (0)