-
-
Notifications
You must be signed in to change notification settings - Fork 207
Improve DX for extending HTML elements with Typescript props #2016
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
Comments
You can always do |
@PuruVJ good point - I will add this to alternatives considered actually - because that does solve 50% of the issue.. but its still not amazing that you have to type |
Hmm i kinda agree though. I've not used $$Props much, but whenever i have, I've always wished there was a |
@PuruVJ forgive my ignorance.. but would a $$RestProps interface really solve the issue here? That would still result in having to declare interface $$RestProps {
variant: 'primary' | 'secondary'
}
interface $$Props extends HTMLButtonAttributes {}
export let variant: $$RestProps['variant']; Still not the ideal solution in my opinion - unless i'm misunderstanding your proposal |
With $$RestProps you shouldn't need to redeclare any props, it should be used just for extending from html attributes props, in theory |
@PuruVJ of couse - that would be amazing if it could be implemented! |
Inventing another interface convention is not the solution here. The solution here is to make tooling smarter that it knows that |
@dummdidumm I had originally suggested the opposite, ie. the language tools can combine $$Props with everything that is exported from the component by default As opoosed to defining types in $$Props and then having to export them seperately as well. For exampleinterface $$Props extends HTMLButtonAttributes {
variant: 'primary' | 'secondary'
}
// Inferred by the tool chain
export let variant Is more boilerplate than just interface $$Props extends HTMLButtonAttributes {}
export let variant: 'primary' | 'secondary' But arguably its harder to reason about. Either way - I am in favour of your solution for sure, I would be willing to have a look to help push it along! |
Semantically that does not make sense to me, because you are implementing an interface. The interface is what determines what the component can do. I think of interface $$Props {..}
export let foo..; as interface $$Props {..}
const props: $$Props = { foo: .. } |
@dummdidumm you are totally right, it does seem weird |
Excuse me if that is not the same issue, but it is another use case I think. Here is my code: <svelte:element
this={tag}
role="button"
tabindex="0"
{href}
on:click
>
<slot />
</svelte:element> TypeScript doesn't like the
I have tried to extend import type { HTMLAttributes } from 'svelte/elements';
interface ButtonAttributes extends HTMLAttributes<any> {
href?: string;
} I feel there is some documentation missing on how to extend such types and/or the DX should really be improved here. |
Describe the problem
I am finding it quite awful when extending base HTML elements.
For example I want to create a custom
<Button />
that has a single new prop calledvariant
Sofar I can see the only method for doing this is as follows:
I have only ever seen this example given when this question has come up on Discord, and on StackOverflow - I feel like there has to be a better way
Describe the proposed solution
If possible the Svelte compiler could combine the defined
$$Props
definition with everything that is exported from the component - that would completely solve this issueTo extend it would be amazing if you could just do something like
Then all Svelte has to do is say
Alternatives considered
export let variant: $$Props['variant']
- prevents having to type Type defintiions twice, but still not 100% amazing solution, as you still have to type that out for every prop you are addingImportance
nice to have
The text was updated successfully, but these errors were encountered: