Skip to content

Commit f65ba8d

Browse files
committed
refactor(type): improved attributes-to-props and utilities type
Release-As: 5.0.4
1 parent ba161c8 commit f65ba8d

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/attributes-to-props.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ const valueOnlyInputs = {
2222
submit: true,
2323
} as const;
2424

25-
export type Attributes = Record<string, string>;
25+
export type ValueOnlyInputsKeys = keyof typeof valueOnlyInputs;
2626

27-
export type Props = Record<string, string | boolean> & {
27+
export type Attributes = Record<PropertyKey, string>;
28+
29+
export type Props = Record<PropertyKey, string | boolean> & {
2830
dangerouslySetInnerHTML?: {
2931
__html: string;
3032
};
3133
key?: string | number;
32-
style?: Record<string, string>;
34+
style?: Record<PropertyKey, string>;
3335
};
3436

3537
/**
@@ -46,8 +48,7 @@ export default function attributesToProps(
4648
const props: Props = {};
4749

4850
const isInputValueOnly = Boolean(
49-
attributes.type &&
50-
valueOnlyInputs[attributes.type as keyof typeof valueOnlyInputs],
51+
attributes.type && valueOnlyInputs[attributes.type as ValueOnlyInputsKeys],
5152
);
5253

5354
for (const attributeName in attributes) {

src/utilities.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const RESERVED_SVG_MATHML_ELEMENTS = new Set([
1313
'font-face-format',
1414
'font-face-name',
1515
'missing-glyph',
16-
]);
16+
] as const);
17+
18+
type ReservedSvgMathmlElements =
19+
typeof RESERVED_SVG_MATHML_ELEMENTS extends Set<infer T> ? T : never;
1720

1821
/**
1922
* Check if a tag is a custom component.
@@ -26,7 +29,7 @@ const RESERVED_SVG_MATHML_ELEMENTS = new Set([
2629
*/
2730
export function isCustomComponent(
2831
tagName: string,
29-
props?: Record<string, any>,
32+
props?: Record<PropertyKey, any>,
3033
): boolean {
3134
if (tagName.indexOf('-') === -1) {
3235
return Boolean(props && typeof props.is === 'string');
@@ -36,7 +39,7 @@ export function isCustomComponent(
3639
// We don't mind this whitelist too much because we expect it to never grow.
3740
// The alternative is to track the namespace in a few places which is convoluted.
3841
// https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
39-
if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName)) {
42+
if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName as ReservedSvgMathmlElements)) {
4043
return false;
4144
}
4245

@@ -88,7 +91,10 @@ export const ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
8891
'head',
8992
'html',
9093
'frameset',
91-
]);
94+
] as const);
95+
96+
type ElementsWithNoTextChildren =
97+
typeof ELEMENTS_WITH_NO_TEXT_CHILDREN extends Set<infer T> ? T : never;
9298

9399
/**
94100
* Checks if the given node can contain text nodes
@@ -97,7 +103,7 @@ export const ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
97103
* @returns - Whether the node can contain text nodes.
98104
*/
99105
export const canTextBeChildOfNode = (node: Element) =>
100-
!ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name);
106+
!ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name as ElementsWithNoTextChildren);
101107

102108
/**
103109
* Returns the first argument as is.

0 commit comments

Comments
 (0)