Skip to content

Commit

Permalink
Refactor to use module augmentation instead of @ts-expect-error
Browse files Browse the repository at this point in the history
Closes GH-5.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing authored Feb 19, 2024
1 parent ebf8926 commit 5ae98d2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
8 changes: 1 addition & 7 deletions example/create-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

/* eslint-env browser */

// @ts-expect-error: TS in VS Code doesn’t seem to infer this.
import * as hastscript from 'https://esm.sh/hastscript@8?dev'

/** @type {typeof import('hastscript').h} */
const h = hastscript.h
/** @type {typeof import('hastscript').s} */
const s = hastscript.s
import {h, s} from 'https://esm.sh/hastscript@8?dev'

export function createTree() {
return h('div', [
Expand Down
15 changes: 9 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import {renderToStaticMarkup} from 'react-dom/server'
// @ts-expect-error: ESM types are wrong.
const Sval = sval.default

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

/** @type {{Fragment: Fragment, jsxDEV: JsxDev}} */
// @ts-expect-error: the react types are missing.
const development = {Fragment: dev.Fragment, jsxDEV: dev.jsxDEV}
const development = /** @type {{Fragment: Fragment, jsxDEV: JsxDev}} */ ({
Fragment: dev.Fragment,
jsxDEV: dev.jsxDEV
})

test('core', async function (t) {
await t.test('should expose the public api', async function () {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"target": "es2022"
},
"exclude": ["coverage/", "node_modules/"],
"include": ["**/**.js", "lib/components.d.ts"]
"include": ["**/**.js", "lib/components.d.ts", "types.d.ts"]
}
32 changes: 32 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// To do: remove when landed in DT
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68600
declare module 'react/jsx-runtime' {
import {ElementType, Fragment, Key, ReactElement} from 'react'

function jsx(type: ElementType, props: unknown, key?: Key): ReactElement

export {Fragment, jsx, jsx as jsxs}
}

// To do: remove when landed in DT
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68600
declare module 'react/jsx-dev-runtime' {
import {ElementType, Fragment, Key, ReactElement} from 'react'
import {Source} from 'hast-util-to-jsx-runtime'

function jsxDEV(
type: ElementType,
props: unknown,
key: Key | undefined,
isStatic: boolean,
source?: Source,
self?: unknown
): ReactElement

export {Fragment, jsxDEV}
}

// Support loading hastscript from https://esm.sh
declare module 'https://esm.sh/hastscript@8?dev' {
export * from 'hastscript'
}

0 comments on commit 5ae98d2

Please sign in to comment.