Skip to content

Commit 56bf2f4

Browse files
committed
Use module augmentation to define missing types
This removes the need for some `@ts-expect-error` comments.
1 parent ebf8926 commit 56bf2f4

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

example/create-tree.js

Lines changed: 1 addition & 7 deletions
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

Lines changed: 9 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

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

0 commit comments

Comments
 (0)