Skip to content

Commit

Permalink
Fix expected types for Element in our JSX definitions (#3958)
Browse files Browse the repository at this point in the history
* Fix expected types for Element in our JSX definitions

* Add changeset
  • Loading branch information
Princesseuh authored Jul 18, 2022
1 parent b282cdb commit 8eba6d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-toes-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix JSX definitions being too strict as to what an element is, which lead to type issues in certain cases (Markdown imports, JSX components etc)
24 changes: 12 additions & 12 deletions packages/astro/astro-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts
*/
declare namespace astroHTML.JSX {
/* html jsx */
export type Child = Node | Node[] | string | number | boolean | null | undefined | unknown;
export type Children = Child | Child[];

Expand All @@ -24,17 +23,18 @@ declare namespace astroHTML.JSX {
children?: Children;
}

type AstroBuiltinProps = import('astro').AstroBuiltinProps;
type AstroBuiltinAttributes = import('astro').AstroBuiltinAttributes;
type AstroDefineVarsAttribute = import('astro').AstroDefineVarsAttribute;
type AstroScriptAttributes = import('astro').AstroScriptAttributes & AstroDefineVarsAttribute;
type AstroStyleAttributes = import('astro').AstroStyleAttributes & AstroDefineVarsAttribute;

// Certain Astro methods returns AstroComponent using AstroComponentFactory
// The language-server does not really like this because it expects Elements to
// all be compatible with HTMLElement so we'll add AstroComponentFactory as a valid element type
type AstroComponent = import('astro').AstroComponentFactory;
type Element = HTMLElement | AstroComponent;
type AstroBuiltinProps = import('./dist/types/@types/astro').AstroBuiltinProps;
type AstroBuiltinAttributes = import('./dist/types/@types/astro').AstroBuiltinAttributes;
type AstroDefineVarsAttribute = import('./dist/types/@types/astro').AstroDefineVarsAttribute;
type AstroScriptAttributes = import('./dist/types/@types/astro').AstroScriptAttributes &
AstroDefineVarsAttribute;
type AstroStyleAttributes = import('./dist/types/@types/astro').AstroStyleAttributes &
AstroDefineVarsAttribute;

// This is an unfortunate use of `any`, but unfortunately we can't make a type that works for every framework
// without importing every single framework's types (which comes with its own set of problems).
// Using any isn't that bad here however as in Astro files the return type of a component isn't relevant in most cases
type Element = HTMLElement | any;

interface DOMAttributes {
children?: Children;
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type { AstroConfigSchema } from '../core/config';
import type { ViteConfigWithSSR } from '../core/create-vite';
import type { AstroComponentFactory, Metadata } from '../runtime/server';
export type { SSRManifest } from '../core/app/types';
export type { AstroComponentFactory } from '../runtime/server';

export interface AstroBuiltinProps {
'client:load'?: boolean;
Expand Down

0 comments on commit 8eba6d9

Please sign in to comment.