Skip to content

Commit 13367ed

Browse files
Fix type to include each element w/ its properties
Closes GH-623. Closes GH-624.
1 parent 456bb3b commit 13367ed

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/ast-to-react.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ exports.hastChildrenToReact = childrenToReact
8989
* @property {number} [index] Passed when `options.includeElementIndex` is given
9090
* @property {number} [siblingCount] Passed when `options.includeElementIndex` is given
9191
*
92-
* @callback NormalComponent
93-
* @param {ReactBaseProps & ReactMarkdownProps} props
94-
* @returns {ReactNode}
95-
*
9692
* @callback CodeComponent
9793
* @param {ReactBaseProps & ReactMarkdownProps & {inline?: boolean}} props
9894
* @returns {ReactNode}
@@ -136,8 +132,8 @@ exports.hastChildrenToReact = childrenToReact
136132
* @property {TableRowComponent|ReactMarkdownNames} tr
137133
* @property {UnorderedListComponent|ReactMarkdownNames} ul
138134
*
139-
* @typedef {Record<Exclude<ReactMarkdownNames, keyof SpecialComponents>, NormalComponent|ReactMarkdownNames>} NormalComponents
140-
* @typedef {Partial<NormalComponents & SpecialComponents>} Components
135+
* @typedef {{[TagName in keyof IntrinsicElements]: TagName | ((props: IntrinsicElements[TagName] & ReactMarkdownProps) => ReactNode)}} NormalComponents
136+
* @typedef {Partial<Omit<NormalComponents, keyof SpecialComponents> & SpecialComponents>} Components
141137
*/
142138

143139
/**
@@ -245,7 +241,7 @@ function toReact(context, node, index, parent) {
245241
start: {line: null, column: null, offset: null},
246242
end: {line: null, column: null, offset: null}
247243
}
248-
/** @type {NormalComponent|SpecialComponents[keyof SpecialComponents]|ReactMarkdownNames} */
244+
/** @type {NormalComponents[keyof NormalComponents]|SpecialComponents[keyof SpecialComponents]|ReactMarkdownNames} */
249245
const component =
250246
options.components && own.call(options.components, name)
251247
? options.components[name]

test/react-markdown.test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const toc = require('remark-toc')
1818
* @typedef {import('hast').Element} Element
1919
* @typedef {import('hast').Text} Text
2020
* @typedef {import('react').ReactNode} ReactNode
21+
* @typedef {import('../src/ast-to-react').Components} Components
2122
*/
2223

2324
/**
@@ -989,14 +990,22 @@ test('should pass index of a node under its parent to components if `includeElem
989990

990991
test('should be able to render components with forwardRef in HOC', () => {
991992
/**
992-
* @param {Function} Component
993+
* @param {(params: Parameters<Exclude<Components['a'], 'a'>>[0]) => JSX.Element} Component
993994
*/
994995
const wrapper = (Component) => {
995-
return React.forwardRef((props, ref) => <Component ref={ref} {...props} />)
996+
return React.forwardRef(
997+
/**
998+
*
999+
* @param {Parameters<Components['a']>[0]} props
1000+
* @param {import('react').Ref<HTMLAnchorElement>} ref
1001+
* @returns
1002+
*/
1003+
(props, ref) => <Component ref={ref} {...props} />
1004+
)
9961005
}
9971006

9981007
/**
999-
* @param {Object<string, unknown>} props
1008+
* @param {Parameters<Exclude<Components['a'], 'a'>>[0]} props
10001009
*/
10011010
// eslint-disable-next-line react/jsx-no-target-blank
10021011
const wrapped = (props) => <a {...props} />

0 commit comments

Comments
 (0)