Skip to content

Commit 93134fd

Browse files
authored
Prevent type expansion in docs (#18)
2 parents 0a5917f + ddb3471 commit 93134fd

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/BaseComponent.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
/**
1919
* Non-readonly non-method keys
2020
*/
21-
type WritableKeys<T> = Extract<
22-
{
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
27-
: false
21+
type WritableKeys<T> = {
22+
[Prop in keyof T]: (
23+
(<G>() => G extends Pick<T, Prop> ? 1 : 2) extends (
24+
<G>() => G extends Record<Prop, T[Prop]> ? 1 : 2
25+
) ? true : false
2826
) extends false
2927
? never
30-
: Prop;
31-
}[keyof T],
32-
{
33-
[K in keyof T]: T[K] extends Function ? never : K;
34-
}[keyof T]
35-
>;
28+
: (T[Prop] extends Function | null | undefined ? never : Prop);
29+
}[keyof T];
30+
31+
/**
32+
* Non-method keys
33+
*/
34+
type ReadableKeys<T> = {
35+
[Prop in keyof T]: T[Prop] extends Function | null | undefined ? never : Prop;
36+
}[keyof T];
3637

3738

3839
/**
@@ -167,7 +168,7 @@ export abstract class BaseComponent<T extends Element> {
167168
* Get element property
168169
* @param name property name
169170
*/
170-
public get<K extends WritableKeys<T>>(name: K): T[K] {
171+
public get<K extends ReadableKeys<T>>(name: K): T[K] {
171172
return this.element[name];
172173
}
173174

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 BaseComponen
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

0 commit comments

Comments
 (0)