diff --git a/.changeset/polite-panthers-tell.md b/.changeset/polite-panthers-tell.md new file mode 100644 index 000000000..e085eb805 --- /dev/null +++ b/.changeset/polite-panthers-tell.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Pass the type of the current component as a type argument to the AstroGlobal in order to type Astro.self diff --git a/internal/printer/print-to-tsx.go b/internal/printer/print-to-tsx.go index 38868fbaf..155d7e741 100644 --- a/internal/printer/print-to-tsx.go +++ b/internal/printer/print-to-tsx.go @@ -142,7 +142,7 @@ func renderTsx(p *printer, n *Node) { * * [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global) */ -declare const Astro: Readonly>`, props.Ident) +declare const Astro: Readonly>`, props.Ident, componentName) } return } diff --git a/packages/compiler/test/tsx/props-and-staticPaths.ts b/packages/compiler/test/tsx/props-and-staticPaths.ts index 2b83c5c7e..dd9cbb818 100644 --- a/packages/compiler/test/tsx/props-and-staticPaths.ts +++ b/packages/compiler/test/tsx/props-and-staticPaths.ts @@ -2,12 +2,12 @@ import { convertToTSX } from '@astrojs/compiler'; import { test } from 'uvu'; import * as assert from 'uvu/assert'; -const PREFIX = `/** +const PREFIX = (component: string = '__AstroComponent_') => `/** * Astro global available in all contexts in .astro files * * [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global) */ -declare const Astro: Readonly>`; +declare const Astro: Readonly>`; test('no props', async () => { const input = `--- @@ -29,7 +29,7 @@ export function getStaticProps() {
export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); diff --git a/packages/compiler/test/tsx/props.ts b/packages/compiler/test/tsx/props.ts index eba6ecfc0..77a53b61d 100644 --- a/packages/compiler/test/tsx/props.ts +++ b/packages/compiler/test/tsx/props.ts @@ -2,12 +2,12 @@ import { convertToTSX } from '@astrojs/compiler'; import { test } from 'uvu'; import * as assert from 'uvu/assert'; -const PREFIX = `/** +const PREFIX = (component: string = '__AstroComponent_') => `/** * Astro global available in all contexts in .astro files * * [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global) */ -declare const Astro: Readonly>`; +declare const Astro: Readonly>`; test('no props', async () => { const input = `
`; @@ -48,7 +48,7 @@ interface Props {} export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -69,7 +69,7 @@ import { Props } from './somewhere'; export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -90,7 +90,7 @@ import { MyComponent as Props } from './somewhere'; export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -111,7 +111,7 @@ import type { Props } from './somewhere'; export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -132,7 +132,7 @@ type Props = {} export default function Test__AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX('Test__AstroComponent_')}`; const { code } = await convertToTSX(input, { filename: '/Users/nmoo/test.astro', sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -153,7 +153,7 @@ interface Props {} export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -174,7 +174,7 @@ interface Props> {} export default function __AstroComponent_>(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -195,7 +195,7 @@ interface Props export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -216,7 +216,7 @@ interface Props ? A : B, P extends string ? { [key: s export default function __AstroComponent_ ? A : B, P extends string ? { [key: string]: any }: never>(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); }); @@ -241,7 +241,7 @@ interface Props extends HTMLAttributes< export default function __AstroComponent_(_props: Props): any {} -${PREFIX}`; +${PREFIX()}`; const { code } = await convertToTSX(input, { sourcemap: 'external' }); assert.snapshot(code, output, `expected code to match snapshot`); });