Skip to content

Commit

Permalink
fix(tsx): add MergeUnion util to automatically inferred Props
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Oct 30, 2023
1 parent b99bc71 commit 008b2e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
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

0 comments on commit 008b2e2

Please sign in to comment.