Skip to content

Commit 5ae98d2

Browse files
authored
Refactor to use module augmentation instead of @ts-expect-error
Closes GH-5. Reviewed-by: Titus Wormer <[email protected]>
1 parent ebf8926 commit 5ae98d2

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

example/create-tree.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66

77
/* eslint-env browser */
88

9-
// @ts-expect-error: TS in VS Code doesn’t seem to infer this.
10-
import * as hastscript from 'https://esm.sh/hastscript@8?dev'
11-
12-
/** @type {typeof import('hastscript').h} */
13-
const h = hastscript.h
14-
/** @type {typeof import('hastscript').s} */
15-
const s = hastscript.s
9+
import {h, s} from 'https://esm.sh/hastscript@8?dev'
1610

1711
export function createTree() {
1812
return h('div', [

test/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ import {renderToStaticMarkup} from 'react-dom/server'
2525
// @ts-expect-error: ESM types are wrong.
2626
const Sval = sval.default
2727

28-
/** @type {{Fragment: Fragment, jsx: Jsx, jsxs: Jsx}} */
29-
// @ts-expect-error: the react types are missing.
30-
const production = {Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs}
28+
const production = /** @type {{Fragment: Fragment, jsx: Jsx, jsxs: Jsx}} */ ({
29+
Fragment: prod.Fragment,
30+
jsx: prod.jsx,
31+
jsxs: prod.jsxs
32+
})
3133

32-
/** @type {{Fragment: Fragment, jsxDEV: JsxDev}} */
33-
// @ts-expect-error: the react types are missing.
34-
const development = {Fragment: dev.Fragment, jsxDEV: dev.jsxDEV}
34+
const development = /** @type {{Fragment: Fragment, jsxDEV: JsxDev}} */ ({
35+
Fragment: dev.Fragment,
36+
jsxDEV: dev.jsxDEV
37+
})
3538

3639
test('core', async function (t) {
3740
await t.test('should expose the public api', async function () {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2022"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/**.js", "lib/components.d.ts"]
14+
"include": ["**/**.js", "lib/components.d.ts", "types.d.ts"]
1515
}

types.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// To do: remove when landed in DT
2+
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68600
3+
declare module 'react/jsx-runtime' {
4+
import {ElementType, Fragment, Key, ReactElement} from 'react'
5+
6+
function jsx(type: ElementType, props: unknown, key?: Key): ReactElement
7+
8+
export {Fragment, jsx, jsx as jsxs}
9+
}
10+
11+
// To do: remove when landed in DT
12+
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68600
13+
declare module 'react/jsx-dev-runtime' {
14+
import {ElementType, Fragment, Key, ReactElement} from 'react'
15+
import {Source} from 'hast-util-to-jsx-runtime'
16+
17+
function jsxDEV(
18+
type: ElementType,
19+
props: unknown,
20+
key: Key | undefined,
21+
isStatic: boolean,
22+
source?: Source,
23+
self?: unknown
24+
): ReactElement
25+
26+
export {Fragment, jsxDEV}
27+
}
28+
29+
// Support loading hastscript from https://esm.sh
30+
declare module 'https://esm.sh/hastscript@8?dev' {
31+
export * from 'hastscript'
32+
}

0 commit comments

Comments
 (0)