-
Notifications
You must be signed in to change notification settings - Fork 793
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
feat: allow imports of reusable Enum types #6030
Comments
Hey @MatteoFeltrin 👋 thanks for raising the issue. Exporting interfaces is totally fine as these are being scrapped from the bundled files and only will be seen in the Let me know if you have any further questions. |
@christian-bromann Thanks for clarifying! I was thinking to open a pull request to update the documentation. Do you think it is worth it? Another doubt I have is: import MyEnum from "../../../common/MyEnum ";
// ....
@Component({tag: 'my-component'})
export class MyComponent{
@Prop() inputType!: MyEnum;
// ....
enum MyEnum {
First= '1',
Second = '2',
}
export default MyEnum ; The import from MyEnum is missing inside
I found this issue that is close to this problem: rwaskiewicz has updated the documentation here ionic-team/stencil-site#784 but is not clearly specified whether the "exported type" MUST be inside a component file or not. Anyway Enums are those which cannot be exported inside a Component file, so it would not be possible to use it. |
Yeah .. Enums are not just a type, they represent an actual object in JavaScript which causes this limitation. I am honestly not too familiar with this optimization in Stencil and have experienced the same in the past. At this point, if you think we can improve the docs for this, I would appreciate any contribution to our docs page. |
Ok, I will do that! Do you have any advice on how I can assign to @prop a type that needs to be imported from "common" files that are not components? My use case:
|
This sounds like a valid issue that should work or if not should be supported. I've renamed the issue and will re-open. |
Let me know if there is anything I can do to help with the issue |
Feel free to dive into it. I can't say when/if the Stencil team has time to take a look. |
@christian-bromann I have dived into it and found out that only NamedImports are considered by the I explain it better with an example: enum MyEnum {
Case1= '1',
Case2= '2',
}
export default MyEnum;
import MyEnum from "../MyEnum ";
// ....
@Component({tag: 'my-component'})
export class MyComponent{
@Prop() inputType!: MyEnum;
// .... Inside the generated If on the other hand you use Named exports like this export enum MyEnum {
Case1= '1',
Case2= '2',
}
import { MyEnum } from "../MyEnum ";
// ....
@Component({tag: 'my-component'})
export class MyComponent{
@Prop() inputType!: MyEnum;
// .... Everything works fine and inside I was trying to fix it but it is not an easy one, and what gives me many doubts is that it seems to be developed only for NamedImports on purpose. Thanks! |
Honestly this is impossible to answer given that no one who has started Stencil is around anymore. My guess is that the original authors of the framework wanted to ensure that components can only my exported via named exports to ensure they have a unique id. They may have missed the case where someone would like to export other primitives like enums etc. Furthermore there is also the issue with naming collisions and how to avoid that. That said, I feel confident that we would want to allow default exports. Restricting it seems to create a lot of confusion. Alternatively we could also have the compiler print a warning in case default exports are detected. |
Prerequisites
Stencil Version
4.22
Current Behavior
Inside a component we have this code:
Everything is compiling and working correctly.
This works only with interface, exporting Classes or Functions throws an error.
Is this fine and we can do that, or it may lead to unexpected behaviour and problems with the bundling as documented?
Expected Behavior
As documented here https://stenciljs.com/docs/module-bundling#one-component-per-module I would expect the compiler to throw something like:
System Info
No response
Steps to Reproduce
Export an
interface
from a component entry point.Code Reproduction URL
none
Additional Information
No response
The text was updated successfully, but these errors were encountered: