Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global JSX to local JSX w/ generics #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
/**
* @typedef {import('./lib/components.js').Components} Components
* @typedef {import('./lib/components.js').Components<JsxElement, JsxElementClass, JsxIntrinsicElements>} Components
* @template JsxElement
* @template JsxElementClass
* @template JsxIntrinsicElements
*/

/**
* @typedef {import('./lib/components.js').ExtraProps} ExtraProps
*/

/**
* @typedef {import('./lib/index.js').CreateEvaluater} CreateEvaluater
*/

/**
* @typedef {import('./lib/index.js').ElementAttributeNameCase} ElementAttributeNameCase
*/

/**
* @typedef {import('./lib/index.js').EvaluateExpression} EvaluateExpression
*/

/**
* @typedef {import('./lib/index.js').EvaluateProgram} EvaluateProgram
*/

/**
* @typedef {import('./lib/index.js').Evaluater} Evaluater
*/

/**
* @typedef {import('./lib/index.js').Fragment} Fragment
* @typedef {import('./lib/index.js').Jsx} Jsx
* @typedef {import('./lib/index.js').JsxDev} JsxDev
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').Props} Props
*/

/**
* @typedef {import('./lib/index.js').Jsx<JsxElement>} Jsx
* @template JsxElement
*/

/**
* @typedef {import('./lib/index.js').JsxDev<JsxElement>} JsxDev
* @template JsxElement
*/

/**
* @typedef {import('./lib/index.js').Options<JsxElement, JsxElementClass, JsxIntrinsicElements>} Options
* @template JsxElement
* @template JsxElementClass
* @template JsxIntrinsicElements
*/

/**
* @typedef {import('./lib/index.js').Props<JsxElement>} Props
* @template JsxElement
*/

/**
* @typedef {import('./lib/index.js').Source} Source
*/

/**
* @typedef {import('./lib/index.js').Space} Space
*/

/**
* @typedef {import('./lib/index.js').StylePropertyNameCase} StylePropertyNameCase
*/

Expand Down
36 changes: 20 additions & 16 deletions lib/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type {Element} from 'hast'
* @returns
* Result.
*/
export type FunctionComponent<ComponentProps> = (
export type FunctionComponent<JsxElement, ComponentProps> = (
props: ComponentProps
) => JSX.Element | string | null | undefined
) => JsxElement | string | null | undefined

/**
* Class component: given props, returns an instance.
Expand All @@ -24,22 +24,22 @@ export type FunctionComponent<ComponentProps> = (
* @returns
* Instance.
*/
export type ClassComponent<ComponentProps> = new (
export type ClassComponent<JsxElementClass, ComponentProps> = new (
props: ComponentProps
) => JSX.ElementClass
) => JsxElementClass

/**
* Function or class component.
*
* You can access props at `JSX.IntrinsicElements`.
* For example, to find props for `a`, use `JSX.IntrinsicElements['a']`.
* You can access props at `JsxIntrinsicElements`.
* For example, to find props for `a`, use `JsxIntrinsicElements['a']`.
*
* @typeParam ComponentProps
* Props type.
*/
export type Component<ComponentProps> =
| ClassComponent<ComponentProps>
| FunctionComponent<ComponentProps>
export type Component<JsxElement, JsxElementClass, ComponentProps> =
| ClassComponent<JsxElementClass, ComponentProps>
| FunctionComponent<JsxElement, ComponentProps>

/**
* Extra fields we pass.
Expand All @@ -49,17 +49,21 @@ export type ExtraProps = {node?: Element | undefined}
/**
* Possible components to use.
*
* Each key is a tag name typed in `JSX.IntrinsicElements`.
* Each key is a tag name typed in `JsxIntrinsicElements`.
* Each value is either a different tag name, or a component accepting the
* corresponding props (and an optional `node` prop if `passNode` is on).
*
* You can access props at `JSX.IntrinsicElements`.
* For example, to find props for `a`, use `JSX.IntrinsicElements['a']`.
* You can access props at `JsxIntrinsicElements`.
* For example, to find props for `a`, use `JsxIntrinsicElements['a']`.
*/
// Note: this type has to be in `.ts` or `.d.ts`, otherwise TSC hardcodes
// react into the `.d.ts` file.
export type Components = {
[TagName in keyof JSX.IntrinsicElements]:
| Component<JSX.IntrinsicElements[TagName] & ExtraProps>
| keyof JSX.IntrinsicElements
export type Components<JsxElement, JsxElementClass, JsxIntrinsicElements> = {
[TagName in keyof JsxIntrinsicElements]:
| Component<
JsxElement,
JsxElementClass,
JsxIntrinsicElements[TagName] & ExtraProps
>
| keyof JsxIntrinsicElements
}
Loading
Loading