Skip to content

feat(types): add SvgComponent type and update SVG module declaration #13651

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-heads-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add a new SvgComponent type and update SVG module declaration
5 changes: 2 additions & 3 deletions packages/astro/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ declare module 'astro:assets' {
}

type ImageMetadata = import('./dist/assets/types.js').ImageMetadata;
type SvgComponent = import('./dist/assets/types.js').SvgComponent;

declare module '*.gif' {
const metadata: ImageMetadata;
Expand Down Expand Up @@ -111,9 +112,7 @@ declare module '*.avif' {
export default metadata;
}
declare module '*.svg' {
type Props = astroHTML.JSX.SVGAttributes;

const Component: ((_props: Props) => any) & ImageMetadata;
const Component: SvgComponent;
export default Component;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/assets/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type * as astroHTML from 'astro/jsx-runtime';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Princesseuh do you think this import could have weird TS implications? IIRC its always a bit tricky to import this one (at least in userland)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import the type from the actual file @florian-lefebvre ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm looking at atm but it's tricky because it's only in a namespace inside a dts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So one way is to do import '../../astro-jsx.d.ts'; (or using a triple slash reference) but then it overrides other type definitions, eg.

error TS5055: Cannot write file '/home/florian/Documents/github/withastro/astro/packages/astro/dist/transitions/events.d.ts' because it would overwrite input file.

error TS5055: Cannot write file '/home/florian/Documents/github/withastro/astro/packages/astro/dist/transitions/types.d.ts' because it would overwrite input file.

error TS5055: Cannot write file '/home/florian/Documents/github/withastro/astro/packages/astro/dist/type-utils.d.ts' because it would overwrite input file.

error TS5055: Cannot write file '/home/florian/Documents/github/withastro/astro/packages/astro/dist/types/public/elements.d.ts' because it would overwrite input file.

error TS5055: Cannot write file '/home/florian/Documents/github/withastro/astro/packages/astro/dist/types/public/view-transitions.d.ts' because it would overwrite input file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of exporting the type from here, personally I'd export it directly in client.d.ts, where it's trivial to import from astro-jsx without bundling issues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want the type to be used in *.svg but also to be exported from "astro". Would that be doable in client.d.ts without making the type global?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah in which case I think maybe it's fine that way

Copy link
Contributor Author

@ADTC ADTC Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Princesseuh the way I did it here won't work, as it causes build errors. (I confirmed that main branch builds successfully.)

I have tried a few different things including "export it directly in client.d.ts" but nothing is working.

What exactly did you mean by "it's trivial to import from astro-jsx without bundling issues"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to fix this?

import type { OmitPreservingIndexSignature, Simplify, WithRequired } from '../type-utils.js';
import type { VALID_INPUT_FORMATS, VALID_OUTPUT_FORMATS } from './consts.js';
import type { ImageService } from './services/service.js';
Expand Down Expand Up @@ -284,3 +285,6 @@ export type RemoteImageProps<T> =
*/
inferSize?: false | undefined;
});

type SvgProps = astroHTML.JSX.SVGAttributes;
export type SvgComponent = ((props: SvgProps) => any) & ImageMetadata;
1 change: 1 addition & 0 deletions packages/astro/src/types/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type {
ImageQuality,
ImageQualityPreset,
ImageTransform,
SvgComponent,
UnresolvedImageTransform,
} from '../../assets/types.js';
export type { AssetsPrefix, SSRManifest } from '../../core/app/types.js';
Expand Down
Loading