Replies: 3 comments 2 replies
-
While explicitly passing the generic argument is not supported, you can allow the component to infer it using one of the props. See the documentation for the Polymorphic for an example: https://docs.astro.build/en/guides/typescript/#polymorphic-type I'm curious if there's a use case where this isn't sufficient. |
Beta Was this translation helpful? Give feedback.
-
Hello @lilnasy, here is my use case to help you. I built a Table astro element : ---
import TableHeader from "./table-header.astro";
import TableHead from "./table-head.astro";
import TableBody from "./table-body.astro";
import TableCell from "./table-cell.astro";
import TableRow from "./table-row.astro";
type TRow = {
[key: string]: string;
};
interface THeader<T extends TRow> {
key: keyof T;
label: string;
}
interface Props<T extends TRow> {
headers: THeader<T>[];
rows: T[];
}
const { headers, rows } = Astro.props as Props<TRow>;
if (!headers || !rows) {
throw new Error("Table component requires headers and rows");
}
---
<div class="rounded-md border bg-background">
<div class="w-full overflow-auto">
<table class="w-full text-sm">
<TableHeader>
<TableRow>
{headers.map((header) => <TableHead>{header.label}</TableHead>)}
</TableRow>
</TableHeader>
<TableBody>
{
rows.map((row) => (
<TableRow>
{headers.map((header) => (
<TableCell>{row[header.key]}</TableCell>
))}
</TableRow>
))
}
{
rows.length === 0 && (
<TableRow>
<TableCell colspan={headers.length} class="h-24 text-center">
No results.
</TableCell>
</TableRow>
)
}
</TableBody>
</table>
</div>
</div> And now I can't type it properly ---
import Table from "@/components/table.astro";
import { database, userTable } from "database";
const users = await database.select().from(userTable);
const headers = [
{
key: "name",
label: "Name",
},
{
key: "email",
label: "Email",
},
];
---
<Table headers={headers} rows={users} /> I got
|
Beta Was this translation helpful? Give feedback.
-
Im trying to do this same sort of thing. Cant use interface Props<T extends string> {
href: T;
openInNewTab?: T extends `#${string}` ? never : boolean;
} Attempting to use this component gives:
|
Beta Was this translation helpful? Give feedback.
-
Body
Summary
I want for there to be support for passing in generic argument's inside components.
Background & Motivation
I recently found out that in Astro Generic arguments in components were not supported.
In other words you can't do.
For this
I talked about this in this issue but instead. Nate said that this was not planned. I know that Astro components are not supposed to be serious components. But this limitation does not make them as useful as they could be.
Goals
Beta Was this translation helpful? Give feedback.
All reactions