Skip to content

Commit d5bb6b2

Browse files
Add types for FunctionComponent w/o used props
Closes GH-26. Reviewed-by: Titus Wormer <tituswormer@gmail.com> Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Co-authored-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent 39d5178 commit d5bb6b2

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

types/index.d.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Minimum TypeScript Version: 3.4
1+
// Minimum TypeScript Version: 3.8
22

33
import {Transformer} from 'unified'
44
import {Prefix, CreateElementLike} from 'hast-to-hyperscript'
@@ -7,14 +7,20 @@ import {Node} from 'unist'
77
declare namespace rehypeReact {
88
type FragmentLike<T> = (props: any) => T | null
99

10-
interface ComponentProps {
11-
[prop: string]: unknown
12-
node?: Node
10+
type ComponentPropsWithoutNode = Record<string, unknown>
11+
12+
interface ComponentPropsWithNode extends ComponentPropsWithoutNode {
13+
node: Node
1314
}
1415

15-
type ComponentLike<T> = (props: ComponentProps) => T | null
16+
type ComponentProps = ComponentPropsWithoutNode | ComponentPropsWithNode
17+
18+
type ComponentLike<
19+
T,
20+
P extends ComponentPropsWithoutNode = ComponentPropsWithoutNode
21+
> = (props: P) => T | null
1622

17-
interface Options<H extends CreateElementLike> {
23+
interface SharedOptions<H extends CreateElementLike> {
1824
/**
1925
* How to create elements or components.
2026
* You should typically pass `React.createElement`
@@ -27,25 +33,37 @@ declare namespace rehypeReact {
2733
*/
2834
Fragment?: FragmentLike<ReturnType<H>>
2935

30-
/**
31-
* Override default elements (such as `<a>`, `<p>`, etcetera) by passing an object mapping tag names to components
32-
*/
33-
components?: Record<string, ComponentLike<ReturnType<H>>>
34-
3536
/**
3637
* React key prefix
3738
*
3839
* @defaultValue 'h-'
3940
*/
4041
prefix?: Prefix
41-
42-
/**
43-
* Expose HAST Node objects to `node` prop of react components
44-
*
45-
* @defaultValue false
46-
*/
47-
passNode?: boolean
4842
}
43+
44+
type ComponentOptions<H extends CreateElementLike> =
45+
| {
46+
/**
47+
* Override default elements (such as `<a>`, `<p>`, etcetera) by passing an object mapping tag names to components
48+
*/
49+
components?: Record<
50+
string,
51+
ComponentLike<ReturnType<H>, ComponentPropsWithNode>
52+
>
53+
/**
54+
* Expose HAST Node objects to `node` prop of react components
55+
*
56+
* @defaultValue false
57+
*/
58+
passNode: true
59+
}
60+
| {
61+
components?: Record<string, ComponentLike<ReturnType<H>>>
62+
passNode?: false
63+
}
64+
65+
type Options<H extends CreateElementLike> = SharedOptions<H> &
66+
ComponentOptions<H>
4967
}
5068

5169
/**

types/rehype-react-test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {h as virtualDomCreateElement} from 'virtual-dom'
55
import * as hyperscriptCreateElement from 'hyperscript'
66
import Vue from 'vue'
77

8+
const TypedFunctionalComponent: React.FunctionComponent = () => <>example</>
9+
const ConflictingTypedFunctionalComponent: React.FunctionComponent<{
10+
notARealProp: string
11+
}> = () => null
12+
813
// Create element must be provided
914
unified().use(rehypeToReact) // $ExpectError
1015
unified().use(rehypeToReact, {}) // $ExpectError
@@ -43,6 +48,20 @@ unified().use(rehypeToReact, {
4348
}
4449
})
4550

51+
unified().use(rehypeToReact, {
52+
createElement: React.createElement,
53+
components: {
54+
div: TypedFunctionalComponent
55+
}
56+
})
57+
58+
unified().use(rehypeToReact, {
59+
createElement: React.createElement,
60+
components: {
61+
div: ConflictingTypedFunctionalComponent // $ExpectError
62+
}
63+
})
64+
4665
unified().use(rehypeToReact, {
4766
createElement: React.createElement,
4867
passNode: true
@@ -52,7 +71,16 @@ unified().use(rehypeToReact, {
5271
createElement: React.createElement,
5372
passNode: true,
5473
components: {
55-
a: (props: rehypeToReact.ComponentProps) => <a>{props.node}</a>
74+
a: (props: rehypeToReact.ComponentPropsWithNode) => <a>{props.node}</a>
75+
}
76+
})
77+
78+
// $ExpectError
79+
unified().use(rehypeToReact, {
80+
createElement: React.createElement,
81+
passNode: false,
82+
components: {
83+
a: (props: rehypeToReact.ComponentPropsWithNode) => <a>{props.node}</a>
5684
}
5785
})
5886

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy