-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TSX] infer props and params from getStaticPaths (#873)
* feat(tsx): automatically infer props and params from getStaticPaths * chore: add changeset * fix: prefix type utils with `ASTRO__`
- Loading branch information
1 parent
7579d7c
commit 09abfe4
Showing
5 changed files
with
124 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/compiler': minor | ||
--- | ||
|
||
Adds ability for TSX output to automatically infer `Astro.props` and `Astro.params` when `getStaticPaths` is used |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { convertToTSX } from '@astrojs/compiler'; | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
|
||
function getPrefix({ | ||
props = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>`, | ||
component = '__AstroComponent_', | ||
params = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>`, | ||
}: { | ||
props?: string; | ||
component?: string; | ||
params?: string; | ||
} = {}) { | ||
return `/** | ||
* Astro global available in all contexts in .astro files | ||
* | ||
* [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global) | ||
*/ | ||
declare const Astro: Readonly<import('astro').AstroGlobal<${props}, typeof ${component}${params ? `, ${params}` : ''}>>`; | ||
} | ||
|
||
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__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;`; | ||
} | ||
|
||
test('explicit props definition', async () => { | ||
const input = `--- | ||
interface Props {}; | ||
export function getStaticPaths() { | ||
return {}; | ||
} | ||
--- | ||
<div></div>`; | ||
const output = | ||
'\n' + | ||
`interface Props {}; | ||
export function getStaticPaths() { | ||
return {}; | ||
} | ||
"";<Fragment> | ||
<div></div> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: Props): any {} | ||
${getSuffix()} | ||
${getPrefix({ props: 'Props' })}`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, `expected code to match snapshot`); | ||
}); | ||
|
||
test('inferred props', async () => { | ||
const input = `--- | ||
export function getStaticPaths() { | ||
return {}; | ||
} | ||
--- | ||
<div></div>`; | ||
const output = | ||
'\n' + | ||
`export function getStaticPaths() { | ||
return {}; | ||
} | ||
"";<Fragment> | ||
<div></div> | ||
</Fragment> | ||
export default function __AstroComponent_(_props: ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>): any {} | ||
${getSuffix()} | ||
${getPrefix()}`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, `expected code to match snapshot`); | ||
}); | ||
|
||
test.run(); |
This file was deleted.
Oops, something went wrong.