Unexpected token error on typescript components
williamlmao opened this issue · comments
I am getting this error when trying to parse some of my typescript components. The components are definitely valid typescript.
SyntaxError: unknown: Unexpected token (22:50)
20 | */
21 | const Item = React.forwardRef<
> 22 | React.ElementRef<typeof AccordionPrimitive.Item>,
| ^
23 | React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
24 | >(({ className, ...props }, ref) => (
25 | <AccordionPrimitive.Item
....
code: 'BABEL_PARSE_ERROR',
reasonCode: 'UnexpectedToken',
loc: Position { line: 22, column: 50, index: 513 },
pos: [Getter/Setter]
Reproducible example using "react-docgen": "^5.4.3"
const reactDocs = require("react-docgen");
const code = `
import * as React from "react";
import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { cn } from "../utils";
export const Accordion = (
props: React.ComponentProps<typeof AccordionPrimitive.Root>
) => {
return <></>;
};
const Root = AccordionPrimitive.Root;
Root.displayName = AccordionPrimitive.Root.displayName;
Accordion.Root = Root;
/**
* Default Classes: ~ "border-b border-b-line" ~
* sdiofh
*/
const Item = React.forwardRef<
React.ElementRef<typeof AccordionPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
>(({ className, ...props }, ref) => (
<AccordionPrimitive.Item
ref={ref}
className={cn("border-b border-b-line", className)}
{...props}
/>
));
Item.displayName = "Accordion.Item";
Accordion.Item = Item;
`;
console.log(reactDocs.parse(code));
Typescript is only activated when the filename is either .ts
or .tsx
or there is babel config that activates typescript. In your case you have to provide a filename
console.log(reactDocs.parse(code, { filename: 'file.tsx' }));