Skip to content

Commit c048881

Browse files
committed
better typings
1 parent 1be37c7 commit c048881

File tree

6 files changed

+1008
-603
lines changed

6 files changed

+1008
-603
lines changed

README.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,6 @@ function ComponentWithSibling() {
139139
}
140140
```
141141

142-
## Typescript, htmr transform and Web Components
143-
144-
If you're using `htmr` to transform custom elements in a typescript code, you'll get type error because custom element is not defined as valid property. To work around this, you can define the mapping in a separate object, and typecast as `any` while spreading in transform object:
145-
146-
```ts
147-
import { ElementType } from 'react';
148-
import { HtmrOptions } from 'htmr';
149-
150-
const customElementTransform: Record<string, ElementType> = {
151-
'virtual-scroller': VirtualScroller,
152-
};
153-
154-
const options: HtmrOptions = {
155-
transform: {
156-
a: Anchor,
157-
...(customElementTransform as any),
158-
},
159-
};
160-
161-
htmr(html, options);
162-
```
163-
164142
## Use Cases
165143

166144
This library was initially built to provides easy component mapping between HTML

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { HtmrOptions as Options } from './src/types';
2-
import { ReactNode } from 'react';
32

4-
export default function htmr(html: string, options?: Partial<Options>): ReactNode
3+
export default function htmr(
4+
html: string,
5+
options?: Partial<Options>
6+
): JSX.Element;
57

6-
export type HtmrOptions = Partial<Options>
8+
export type HtmrOptions = Partial<Options>;

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
"react": ">=15.6.1"
5858
},
5959
"devDependencies": {
60-
"@babel/core": "^7.12.17",
61-
"@babel/preset-env": "^7.12.17",
62-
"@babel/preset-react": "^7.12.13",
63-
"@babel/preset-typescript": "^7.12.17",
60+
"@babel/core": "^7.18.10",
61+
"@babel/preset-env": "^7.18.10",
62+
"@babel/preset-react": "^7.18.6",
63+
"@babel/preset-typescript": "^7.18.6",
6464
"@testing-library/react": "^11.2.5",
6565
"@types/react": "^17.0.2",
6666
"benchmark": "^2.1.4",
@@ -81,7 +81,7 @@
8181
"rollup-plugin-json": "^4.0.0",
8282
"rollup-plugin-terser": "^5.0.0",
8383
"rollup-plugin-typescript2": "^0.21.0",
84-
"typescript": "^4.1.5"
84+
"typescript": "^4.7.4"
8585
},
8686
"bundlesize": [
8787
{

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function toReactNode(
5858
attribs[key] = decode(attribs[key]);
5959
});
6060

61-
const props = Object.assign(
61+
const props: any = Object.assign(
6262
{},
6363
mapAttribute(name, attribs, preserveAttributes, getPropInfo),
6464
{ key }

src/types.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
import React, { ReactHTML, ReactSVG, ReactNode, ComponentType } from "react";
1+
import {
2+
ReactHTML,
3+
ReactSVG,
4+
ReactNode,
5+
ComponentType,
6+
ComponentProps,
7+
AllHTMLAttributes,
8+
} from 'react';
29

310
export type HTMLTags = keyof ReactHTML;
411
export type SVGTags = keyof ReactSVG;
5-
type AllTags = HTMLTags | SVGTags
12+
type AllTags = HTMLTags | SVGTags;
613

714
type HTMLTransform = {
8-
[tag in AllTags]: AllTags | ComponentType<Omit<React.ComponentProps<tag>, 'ref'>>;
15+
[tag in AllTags]: AllTags | ComponentType<Omit<ComponentProps<tag>, 'ref'>>;
916
};
1017

1118
type DefaultTransform = {
12-
_: <Props>(element: string | AllTags, props?: Props, children?: ReactNode) => ReactNode
13-
}
19+
_: <Props extends AllHTMLAttributes<any>>(
20+
element: string | AllTags,
21+
props?: Props,
22+
children?: ReactNode
23+
) => ReactNode;
24+
};
25+
26+
type CustomElementTransform = {
27+
[key in `${string}-${string}`]: AllTags | ComponentType<unknown>;
28+
};
1429

1530
export type HtmrOptions = {
16-
transform: Partial<HTMLTransform & DefaultTransform>,
17-
preserveAttributes: Array<String | RegExp>,
31+
transform: Partial<HTMLTransform & DefaultTransform & CustomElementTransform>;
32+
preserveAttributes: Array<String | RegExp>;
1833
/** An array of tags in which their children should be set as raw html */
19-
dangerouslySetChildren: HTMLTags[]
34+
dangerouslySetChildren: HTMLTags[];
2035
};

0 commit comments

Comments
 (0)