Skip to content

Commit bf76663

Browse files
fix(tsx): add MergeUnion util to automatically inferred Props (#881)
* fix(tsx): add MergeUnion util to automatically inferred Props * Update large-geese-kick.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent b99bc71 commit bf76663

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

.changeset/large-geese-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
[TSX] Add `ASTRO__MergeUnion` util to allow destructuring from automatically inferred union Prop types

internal/printer/print-to-tsx.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func renderTsx(p *printer, n *Node) {
141141
if hasGetStaticPaths {
142142
paramsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>"
143143
if propsIdent == "Record<string, any>" {
144-
propsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>"
144+
propsIdent = "ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>"
145145
}
146146
}
147147

@@ -150,6 +150,7 @@ func renderTsx(p *printer, n *Node) {
150150
p.printf(`type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
151151
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
152152
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
153+
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;
153154
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;%s`, "\n")
154155
}
155156

packages/compiler/test/tsx/props-and-getStaticPaths.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from 'uvu';
33
import * as assert from 'uvu/assert';
44

55
function getPrefix({
6-
props = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>`,
6+
props = `ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>`,
77
component = '__AstroComponent_',
88
params = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>`,
99
}: {
@@ -23,6 +23,7 @@ function getSuffix() {
2323
return `type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
2424
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
2525
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
26+
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;
2627
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;`;
2728
}
2829

@@ -69,7 +70,7 @@ export function getStaticPaths() {
6970
"";<Fragment>
7071
<div></div>
7172
</Fragment>
72-
export default function __AstroComponent_(_props: ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>): any {}
73+
export default function __AstroComponent_(_props: ASTRO__MergeUnion<ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>>): any {}
7374
${getSuffix()}
7475
${getPrefix()}`;
7576
const { code } = await convertToTSX(input, { sourcemap: 'external' });

0 commit comments

Comments
 (0)