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

fix(tsx): add MergeUnion util to automatically inferred Props #881

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/large-geese-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

[TSX] Add `ASTRO__MergeUnion` util to allow destructuring from automatically inferred union Prop types
3 changes: 2 additions & 1 deletion internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func renderTsx(p *printer, n *Node) {
if hasGetStaticPaths {
paramsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>"
if propsIdent == "Record<string, any>" {
propsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>"
propsIdent = "ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>"
}
}

Expand All @@ -150,6 +150,7 @@ func renderTsx(p *printer, n *Node) {
p.printf(`type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type ASTRO__MergeUnion<T, K extends PropertyKey = T extends unknown ? keyof T : never> = T extends unknown ? T & { [P in Exclude<K, keyof T>]?: never } extends infer O ? { [P in keyof O]: O[P] } : never : never;
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;%s`, "\n")
}

Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/test/tsx/props-and-getStaticPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test } from 'uvu';
import * as assert from 'uvu/assert';

function getPrefix({
props = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>`,
props = `ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>`,
component = '__AstroComponent_',
params = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>`,
}: {
Expand All @@ -23,6 +23,7 @@ function getSuffix() {
return `type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type ASTRO__MergeUnion<T, K extends PropertyKey = T extends unknown ? keyof T : never> = T extends unknown ? T & { [P in Exclude<K, keyof T>]?: never } extends infer O ? { [P in keyof O]: O[P] } : never : never;
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;`;
}

Expand Down Expand Up @@ -69,7 +70,7 @@ export function getStaticPaths() {
"";<Fragment>
<div></div>
</Fragment>
export default function __AstroComponent_(_props: ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>): any {}
export default function __AstroComponent_(_props: ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>): any {}
${getSuffix()}
${getPrefix()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
Expand Down
Loading