Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Stencil Version
4.12.0
Current Behavior
It appears that the TypeScript definitions for JSX support in Stencil are not completely correct, and are causing issue when used with third-party tools (like @typescript-eslint)
Expected Behavior
JSX.Element type is resolved correctly
System Info
System: node 20.11.0
Platform: darwin (23.2.0)
CPU Model: Apple M1 Pro (10 cpus)
Compiler: /Users/mak13180/site/esri/arcgis-web-components/node_modules/@stencil/core/compiler/stencil.js
Build: 1705333241
Stencil: 4.10.0 🍪
TypeScript: 5.3.3
Rollup: 2.42.3
Parse5: 7.1.2
jQuery: 4.0.0-pre
Terser: 5.26.0Steps to Reproduce
I made a small reproduction case based on the JSX typings in Stencil: source
See the error on line 16
Further, see that see that typescript compiler resolves the type as "error":
out.mp4
This is because TypeScript is hardcoded to look for Element type/interface in the JSX namespace
In the above demo, uncomment line 8 (not present in Stencil's typings), and see how the typescript compiler now correctly resolves the JSX type, and also the typescript-eslint error is resolved
Code Reproduction URL
- Initial problem: ESLint Playground - Example 1: "Unsafe return of an
anytyped value." - After setting
type Element = VNodein JSX namespace: ESLint Playground - Example 2: "'Fragment' cannot be used as a JSX component.
Its return type 'VNode | VNode[]' is not a valid JSX element." - Stencil Component Starter Reproduction
Additional Information
For reference, here are examples of Elements interface/type being included in the JSX element in other popular libraries:
- React - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6d0e970598392a8c1c62019150e4f197fadf301c/types/react/jsx-runtime.d.ts#L5
- PReact - https://github.com/preactjs/preact/blob/49433b3a16a629db3a9a39df3f9f81ff703078bb/src/jsx.d.ts#L41
- dom-expressions (used by SolidJS and others) - https://github.com/ryansolid/dom-expressions/blob/eb9a6bcf640b04c1d1c889d9983914a2776093fe/packages/dom-expressions/src/jsx.d.ts#L12
- Qwik - https://github.com/BuilderIO/qwik/blob/408576e8a2e1d11fabf030f37b5941c0a3274669/packages/qwik/src/core/render/jsx/types/jsx-qwik.ts#L7
After debugging typescript-eslint and reading TypeScript source code, it appears that TypeScript is hardcoded to look for JSX.Element type/interface, rather than look at the return type of the jsxFactory:
function getJsxElementTypeAt(location: Node): Type {
return getJsxType(JsxNames.Element, location);
}
Activity
maxpatiiuk commentedon Jan 31, 2024
These types appear incorrect too:
because FunctionalComponent is defined as returning VNode | VNode[], but TypeScript expects Fragment to be equal to JSX.Element (thus just
VNode, notVNode | VNode[])otherwise, the user of Stencil would be forced to specify return type as
VNode | VNode[]everywhere in there application (i.e asrender()return type)and you get this TypeScript error:
something like this fixes it:
fix(declarations): use propper JSX typings
rwaskiewicz commentedon Jan 31, 2024
@maxpatiiuk Thanks! Someone will take a look at this in a bit.
Can you do me a favor and split #5306 (comment) into its own issue? I'd like to make sure we're not trying to understand two different Stencil issues in the context of one GitHub issue. Thanks!
maxpatiiuk commentedon Jan 31, 2024
unfortunately, these fixes have to be in the same PR.
reason:
The problem is that adding the
type Element = VNode;uncoverts the typing issues with Fragment/Host return type, causing TypeScript errors:basically, we defined that JSX.Element is
VNode, but return type of Fragment is notVNodebutVNode | VNode[], so TypeScript complains. The other solution is to definetype Element = VNode | VNode[];but that is a bad idea (would force users of Stencil to always type the returns of their render() functions asVNode | VNode[]instead of justVNode)(this is a limitation of TypeScript, see DefinitelyTyped/DefinitelyTyped#20544 and microsoft/TypeScript#21699)
so, while I could separate the PRs, they would have to be merged at the same time to avoid issues
rwaskiewicz commentedon Jan 31, 2024
Hey! So while that context is helpful, what I was looking for a actually a separate issue, rather than PR. Specifically, I am looking for steps to reproduce the statements that
HostandFunctionalComponentare incorrect. I'd like to understand where/how the errors you're showing in this comment are coming from (be it the output ofstencil build, in an editor, a combination of both, etc.).Prior to your second comment, it wasn't clear these were related. I still haven't dug in, but if you can let me know where how you're seeing these errors after applying that
type Element = VNodechange, that'd be a big help 👍maxpatiiuk commentedon Feb 1, 2024
Ah, thank you for the clarification - I misread your comment
Opened an issue, with the smallest reproducible code sample and a suggested fix - #5311
rwaskiewicz commentedon Feb 1, 2024
Thanks! In #5311 I asked for a reproduction case that doesn't use the ESLint playground. I don't think that'll be necessary here, as I think I can use the same one for this issue (I may update the Issue Summary to link to it when it's available). If you don't think I could (reuse the reproduction case), please LMK!
rwaskiewicz commentedon Feb 2, 2024
I've merged #5311 into this issue now that I understand the problem space a bit better.
For folks running into this, the current plan is to revisit the the PR that fixes this as a part of Stencil v5. In the interim, I suggest adding an
anyreturn type to your render functions, as that appears to appease the ESLint ruleno-unsafe-returnand aligns with the currently typed return value ofrendermaxpatiiuk commentedon Feb 2, 2024
Thanks - this sounds good.
We are not allowed to use
anyin our codebase, but there is a nice workaround:Create a
declarations.d.tsfile like this:(even though the problem is with Host/Fragment types, typescript doesn't let us override those due to a
Subsequent variable declarations must have the same type.error, so I am overridingFunctionalComponentreturn type instead - this might cause issues in some codebases, but worked fine in our's)Then in your tsconfig.json, in the "files" array, add this file
cl10k commentedon Apr 22, 2024
@maxpatiiuk
Thanks for your workaround! Unfortunately type inference of props in FunctionalComponents stops working with this.
The simple example below is taken form the official docs:
nameshould be a string:I don't understand how changing the return type of the FuntionalComponent Interface (
VNode|VNode[]->VNode) results in this behavior.Any ideas on how to get my types back?
andluchian commentedon May 7, 2024
facing the same issue which is related to this as well -> https://forum.ionicframework.com/t/adding-ionic-components-to-custom-stencil-components/223091/2
Trying to use @stencil/core and load it via an app.ts file. This works well until I try to build a react version of the library. Generated typings for The @ionic/core components fail.
export const IonAccordion = /*@__PURE__*/createReactComponent<JSX.IonAccordion, HTMLIonAccordionElement>('ion-accordion'); export const IonAccordionGroup = /*@__PURE__*/createReactComponent<JSX.IonAccordionGroup, HTMLIonAccordionGroupElement>('ion-accordion-group'); export const IonActionSheet = /*@__PURE__*/createReactComponent<JSX.IonActionSheet, HTMLIonActionSheetElement>('ion-action-sheet'); ...exports like
JSX.IonAccordionfail which causes the react lib build to fail.Any ideas on how to fix this?