-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve Typescript types in output #372
Comments
yeah thats a good point. @samijaber what do you think is the best way to transfer the types over? |
Hey @CKGrafico, that's a very good question. Unfortunately, at the moment, all types get stripped out when the component JSON is generated from the Mitosis source code. Preserving types is definitely an important feature that we want to incorporate into Mitosis though, so let me try to break down what this requires! Our JSX parser already supports TypeScript syntax: mitosis/packages/core/src/parsers/jsx.ts Line 790 in 29b3830
However, we do not store any of these types yet. It will be a lot of work to store TS types, but we can definitely chip away at this problem incrementally. I'm going to lay out what steps I think are needed to accomplish that. Sharing it in the open as notes for myself, but also in case you or anyone else is intrigued and would like to help in some capacity! ExampleIf we were to use this very simple example: You can see:
1. Store type informationWe would need to start storing this information in the Mitosis JSON. We could have root-level
2. Store type referencesNow that we are storing For For the rest of our code (hooks, state properties, etc.), we currently use mitosis/packages/core/src/parsers/jsx.ts Lines 156 to 160 in 29b3830
This would have to change such that we are also storing type information. I am not exactly sure if we can re-purpose this generator, or would have to use a different tool to generate the code (perhaps typescript?) such that it still preserves type information. Ideally, the 3. inject type information in each generatorNow that we have the types, it is fairly easy to go into each generator (vue, react, svelte, etc.) and make sure to also inject that type information into the generated source code: mitosis/packages/core/src/generators/react.ts Line 615 in 29b3830
This would likely look like: (props: ${json.propTypeReference ?? 'any'))` And if we replace the Babel generator with some other TS-based generator, then the code strings we inject in the generators should work as-is. But if we end up storing the types in a separate field, then that will also require more stitching together. NOTEs
mitosis/packages/cli/src/build/helpers/transpile.ts Lines 16 to 40 in 29b3830
This strips away type information. We would need to explore if that can be removed or circumvented |
Hello thanks for your awesome and detailed reply, that was really useful :D |
Very clear explanation. It seems like a changeling issue. If possible, I want to have a try. |
@decadef20 That would be great! This is a critical and complex feature, so if you do take it on, please reach out with any questions as you build it out, so we can make sure you're approaching it the same way we are envisioning it and not waste your time rewriting a lot of code. We're here to help 👍🏽 |
OK。At first,I plan to implement it on react framework. Make sure what I build out is as expected. |
@PatrickJS how soon will this be published to npm? |
@samijaber can you publish for @originswift-sys |
@originswift-sys Released in core version |
Little hiccup... The source: import { HTMLAttributes } from "react";
import { Show } from "@builder.io/mitosis";
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 (
<>
<Show when={props.size === 1}>
<h1 className="font-extrabold text-4xl">{props.children}</h1>
</Show>
<Show when={props.size === 2}>
<h2 className="font-extrabold text-3xl">{props.children}</h2>
</Show>
<Show when={props.size === 3}>
<h3 className="font-extrabold text-2xl">{props.children}</h3>
</Show>
<Show when={props.size === 4}>
<h4 className="font-extrabold text-xl">{props.children}</h4>
</Show>
<Show when={props.size === 5}>
<h5 className="font-extrabold text-lg">{props.children}</h5>
</Show>
<Show when={props.size === 6}>
<h6 className="font-extrabold text-base">{props.children}</h6>
</Show>
</>
);
} generates... export interface HeadingProps extends HTMLAttributes<HTMLHeadingElement> {
size: typeof headingSizes[number];
}
export const headingSizes = [1, 2, 3, 4, 5, 6];
export default function Heading(props: HeadingProps) {
return (
<>
{props.size === 1 ? (
<>
<h1 className="font-extrabold text-4xl">{props.children}</h1>
</>
) : null}
{props.size === 2 ? (
<>
<h2 className="font-extrabold text-3xl">{props.children}</h2>
</>
) : null}
{props.size === 3 ? (
<>
<h3 className="font-extrabold text-2xl">{props.children}</h3>
</>
) : null}
{props.size === 4 ? (
<>
<h4 className="font-extrabold text-xl">{props.children}</h4>
</>
) : null}
{props.size === 5 ? (
<>
<h5 className="font-extrabold text-lg">{props.children}</h5>
</>
) : null}
{props.size === 6 ? (
<>
<h6 className="font-extrabold text-base">{props.children}</h6>
</>
) : null}
</>
);
} All imports are not exported like before. Did I miss something? I was on v0.0.55 and v0.0.16 before. |
I don't understand this statement. What are you expecting to see? The generated output you shared looks good to me. What is it missing? |
It's missing I've been able to avoid getting stuck by using the react global If exporting imports has been removed in recent versions, I'm not aware, but the previous versions I mentioned reexported all my imports. |
Oh, I missed that. Thanks for explaining. Will file a separate issue for this. |
Great. Thanks! |
we also do React.HTMLAttributes for these types for now |
Closing this issue in favour of individual issues per framework. The core engine now supports storing type information in the Mitosis JSON 👍🏽 |
Hello not sure if I'm understanding well the purpose of this library but when I create a component like:
The result is:
or:
If I want to use mitosis to create my own components library, how I can get the types of my components to be able to expose them?
The text was updated successfully, but these errors were encountered: