Mitosis JSX types are not properly mapped
See original GitHub issueOriginally from https://github.com/BuilderIO/mitosis/issues/372#issuecomment-1150135164
For this input:
import { HTMLAttributes } from "react";
export const headingSizes = [1, 2, 3, 4, 5, 6];
export interface HeadingProps extends HTMLAttributes<HTMLHeadingElement> {
size: typeof headingSizes[number];
}
export default function Heading(props: HeadingProps) {
return <div />
}
the output does not preserve the import { HTMLAttributes } from "react";
import
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:16 (6 by maintainers)
Top Results From Across the Web
BUG: Mitosis does not camelCase HTML attributes in React
We want to go to the blockToReact function, and identify blocks that are native HTML elements, and rename any json.bindings or json.properties ...
Read more >Intro to Mitosis: The universal reactive transformer - InfoWorld
Mitosis is the JavaScript compiler that lets developers "write once, run anywhere," then compile to Angular, Vue, Svelte, and more.
Read more >Documentation - JSX - TypeScript
JSX is an embeddable XML-like syntax. It is meant to be transformed into valid JavaScript, though the semantics of that transformation are ...
Read more >Mitosis - Molecular Biology of the Cell - NCBI Bookshelf - NIH
The segregation of the replicated chromosomes is brought about by a complex cytoskeletal machine with many moving parts—the mitotic spindle.
Read more >Typescript type issue in map function of mixed types react
hmmm, obviously I appreciate the help, but I am not sure I really like either of these solutions. The array will always contain...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I saw @kylecordes use this. can we update the imports to remove the need for
dist/src
Thanks @R-Bower, that’s informative. You’re right, we need a way to map the Mitosis JSX types to the correct framework types, or folks will run into many type-related issues.
There is definitely a way to transform the import to that of React’s JSX types, it will look something like this:
Step 1 - Identify every place where the JSX types are used
in the JSX parser, we will need to update the logic that parses import declarations here: https://github.com/BuilderIO/mitosis/blob/3717ef34ace920f725f0ceebb61b26c5e61b661a/packages/core/src/parsers/jsx/imports.ts#L7-L44
such that: if we see a
JSX
import coming from@builder.io/mitosis
or@builder.io/mitosis/jsx-runtime
, we want to store that import (currently we ignore all mitosis imports). Might be good to normalize all possible variations (regular import, type import, import from different paths) into 1 single variant:import type { JSX } from '@builder.io/mitosis/jsx-runtime'
;We will also need to
Step 2 - transform imports
This might be the best place to make such a change: https://github.com/BuilderIO/mitosis/blob/3717ef34ace920f725f0ceebb61b26c5e61b661a/packages/cli/src/build/helpers/transpile.ts#L15-L30
This import transform code will run for both mitosis components (
.lite.tsx
) and plain JS/TS files. If will want to replace the normalizedimport type { JSX } from '@builder.io/mitosis/jsx-runtime';
import statement to whatever makes sense for each given framework. For React, It will be easiest to convert the import to:This will avoid the need to replace every
JSX.*
within the code withReact.
. This assumes thatimport React from 'react';
already exists somewhere in the code (which it does as we always add it to react generators). But if it somehow doesn’t, we could add a check and add it ourselves.The solution will be different for each framework, but should roughly follow this same pattern of changes.