eslint-mdx icon indicating copy to clipboard operation
eslint-mdx copied to clipboard

How can you type check React props with this plugin?

Open vb opened this issue 2 years ago • 4 comments
trafficstars

Initial checklist

Problem

I want to have a system where the author can't render a React component in mdx with the wrong props.

Example

// Component.tsx

export const Component = ({ name, age }: { name: string, age: number }) => {
  return ...
}
import { Component } from './Component'

## Component

<Component name="vb" age={35} /> // ✅
<Component name="vb" />          // ❌ Missing mandatory prop
<Component name={35} age={35} /> // ❌ Wrong type for name

Solution

I'd get an eslint error when I try to render a React component in mdx with bad props

Alternatives

I guess this is done together with plugin:@typescript-eslint?

vb avatar May 22 '23 17:05 vb

Type checking isn't done by ESLint. MDX type checking is available in MDX analyzer instead. (Though it's still early stage)

remcohaszing avatar May 22 '23 19:05 remcohaszing

https://github.com/ota-meshi/typescript-eslint-parser-for-extra-files

This package maybe help, I'll do some research recently.

JounQin avatar Aug 03 '23 19:08 JounQin

Would also love if full type checking was in the toolchain somewhere - checking for the components provided through MDXProvider, as also mentioned in the mdx-analyzer discussion:

  • https://github.com/orgs/mdx-js/discussions/2396#:~:text=for%20the%20components%20we%20provide%20through%20a%20MDXProvider

Our use case: checking whether a value of a particular slug is valid and failing CI if invalid (currently set to a union of string literals in TS):

- 🎥 slides: <LectureSlideDeck slug="js-fundamentals" />

karlhorky avatar Feb 21 '24 22:02 karlhorky

For anyone looking for the current research on CLI TypeScript type checking in *.mdx files, some current research and first implementation experiments can be found here:

  • https://github.com/mdx-js/mdx-analyzer/issues/292
  • https://github.com/volarjs/volar.js/issues/145

Keywords: command line, CLI, terminal, Volar.js, MDX Analyzer

karlhorky avatar Jul 24 '24 08:07 karlhorky