Skip to content
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

How to reference the "transformed" type? #2121

Closed
aradalvand opened this issue Aug 8, 2023 · 2 comments
Closed

How to reference the "transformed" type? #2121

aradalvand opened this issue Aug 8, 2023 · 2 comments

Comments

@aradalvand
Copy link

aradalvand commented Aug 8, 2023

Say you have the following in your app.d.ts file:

declare namespace svelteHTML {
   interface SVGAttributes {
        class?: string | { toString(): string };
    }
}

This works. The resulting "transformed" type will have its class overridden such that it accepts either a string or any object with a toString method. And this will be reflected in all <svg> elements throughout the codebase.

But then consider something like this:

import type { ComponentType, SvelteComponent } from 'svelte';
import type { SVGAttributes } from 'svelte/elements';

type SVGComponent = ComponentType<SvelteComponent<SVGAttributes<SVGSVGElement>>>;

The type of SVGComponent's props will not have taken into account the transformation, clearly because SVGAttributes here is imported directly from svelte/elements, which contains the default types.

You could also reference the svelteHTML namespace here like so:

type SVGComponent = ComponentType<SvelteComponent<svelteHTML.SVGAttributes<SVGSVGElement>>>;

But now this will only include the properties defined in our SVGAttributes interface (that is, class), and not any of the others declared in the default SVGAttributes.

How are you supposed to do this sort of thing?

@jasonlyu123
Copy link
Member

You can use ComponentType<SvelteComponent<svelteHTML.IntrinsicElements['svg']>>. But once #2109 is fixed, you can use declare module 'svelte/elements' to define custom typing.

@aradalvand
Copy link
Author

aradalvand commented Aug 9, 2023

@jasonlyu123 Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants