-
-
Notifications
You must be signed in to change notification settings - Fork 202
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
<svelte:element> parameter has an 'any' type with an inline handler #2003
Comments
Remember that you can use the |
The point of |
This is an editor/language server issue. I replicated the same example using Vite and TypeScript and got no errors neither from the language server nor the build. |
I'm getting this issue in tonnes of components and it's quite annoying since it creates all of these unfixable typescript errors everywhere. |
You can fix the error if you type the tag variable as a literal type like let tag: 'a' | 'button' = 'a' |
@dummdidumm could this get some attention please? I'm getting littered with tonnes of these errors across my project I feel like it's a common usecase to use I'm also getting these issues with the
Some related issues: |
The As for the event handler, It seems like TypeScript can't infer it from the union signature. Not sure what we can do about it. But it works if you move it to the script tag with an implicit parameter type. TypeScript will error with the wrong parameter type but can't infer the exact type for an inline handler. Transferring because this can't be fixed in |
href
is an unrecognised property
Thanks. Those are working now, but it would be nice to get the typing of |
Minimum reproducible for the events issue: type EventHandler<E extends Event = Event, T extends EventTarget = Element> =
(event: E & { currentTarget: EventTarget & T}) => any;
type A = {
'on:click'?: EventHandler<Event,HTMLDivElement>;
}
type B = {
'on:click'?: EventHandler<Event,HTMLAnchorElement>;
}
const a: A | B = {'on:click': e => e} |
Closing this because I think is a valid solution let tag: 'a' | 'button' = 'a' |
Reopening because of #2003 (comment) |
@dummdidumm, this also happens with non-standard HTML elements which are also valid HTML, eg: <thread-row on:keypress={ e => ... }>
|
It's most likely because there isn't a type definition for the custom element and unrelated to this issue. You'll need to enhance the typing https://svelte.dev/docs/typescript#enhancing-built-in-dom-types. You can use the |
export interface SvelteHTMLElements {
a: HTMLAnchorAttributes;
abbr: HTMLAttributes<HTMLElement>;
...
[name: string]: { [name: string]: any }; // this is really annoying
} This part really messes with everything, why can't the catch all case simply derive from the base What is a workaround here? |
I had to some wack hacky template string to get it to work interface SvelteHTMLElements {
[name: `${string}${
| "a"
| "b"
| "c"
| "d"
| "e"
| "f"
| "g"
| "h"
| "i"
| "j"
| "k"
| "l"
| "m"
| "n"
| "o"
| "p"
| "q"
| "r"
| "s"
| "t"
| "u"
| "v"
| "w"
| "x"
| "y"
| "z"}`,
]: HTMLAttributes<HTMLElement>
} |
I'm not using "custom elements", i'm just using non-standard HTML elements which are also valid HTML, why can't they by default derive their fields and options off export interface SvelteHTMLElements {
a: HTMLAnchorAttributes;
abbr: HTMLAttributes<HTMLElement>;
...
- [name: string]: { [name: string]: any };
+ [name: string]: HTMLAttributes<HTMLElement>;
} I believe this should be updated in the svelte code to be like so |
That won't work for SVG elements. |
@jasonlyu123 yeah with my workaround, sure, but I don't understand why the fallback case shouldn't be |
No. What I meant is that |
@jasonlyu123, why wouldn't SVG Elements have their own key in |
The |
This is extremely annoying and I feel like the correct behavior in this scenario is to default to As you can see here, on the MDN docs SVG elements still extend from HTML elements, and inherit their interface & properties. So I think turning them to |
They don't extend from HTML elements, the extend from elements. Very different. |
@dummdidumm does it make sense then to do: interface SvelteHTMLElements {
[name: string]: HTMLAttributes<Element>
// or
[name: string]: HTMLAttributes<Element> & { [key: string]: any }
} |
Element is not necessarily a HTMLElement, as such |
Edit: I have hidden the old issue that was solved, but this thread contained two issues
Old issue
Describe the bug
Getting a typescript issue where
<svelte:element>
is erroring when I usehref
attribute on an element that may either be an anchor link or a div.This causes no actual compilation errors, but it does appear as an error in the IDE which is annoying
Reproduction
https://github.com/AlbertMarashi/element-bug-1
Logs
No response
System Info
System: OS: macOS 12.6.2 CPU: (8) arm64 Apple M1 Pro Memory: 88.88 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.17.0 - /usr/local/bin/node npm: 8.15.0 - /usr/local/bin/npm Browsers: Chrome: 108.0.5359.124 Firefox: 105.0.1 Firefox Developer Edition: 109.0 Safari: 15.6.1 npmPackages: svelte: ^3.55.0 => 3.55.0
Severity
annoyance
I'm getting these issues with the
e
being untyped with svelte:elementsParameter 'e' implicitly has an 'any' type
#2003 (comment)
The text was updated successfully, but these errors were encountered: