Closed silveltman closed 8 months ago
The Polymorphic type establishes a relationship between one property ("as") and the rest. When creating intersection, that relationship is lost in the resulting type.
Is there a case where type Props = Base & {}
is necessary?
The example I gave was a simplified version, my actual component looks more like this:
---
import type { Polymorphic } from 'astro/types'
import { buildProps, type BuildProps } from '../../utils'
interface Base
extends Polymorphic<{ as: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' }> {
title?: string
}
type Props = BuildProps<Base, 'title'>
const props = buildProps(Astro, 'title')
const { title, as: Tag = 'h2', class: className, ...rest } = props
---
<Tag
class:list={['title', className]}
{...rest}
>
<Fragment set:html={title} />
</Tag>
I do some modifications to my base Props which I do in every component. For your info, the BuildProps turn the above Base interface into the following:
type Props = Omit<Base, "slot" | "title" | "_title"> & {
title?: string | Base | undefined;
_title?: string | Base | undefined;
}
Then the buildProps function turns it back, allowing me to pass in all props as an object to a key with a specified name. Reason for this is that it allows me have a lot of control over my component from within my content collections. I could for example do the following:
---
title:
_title:
size: large
---
I think this issue comes down to typescript works, not astro's use of it.
You should be able to maintain the relationship between the tag name in "as" and the rest of the props by extending the interface instead.
-type Props = Base & {}
+interface Props extends Base {}
-type Props = BuildProps<Base, 'title'>
+interface Props extends BuildProps<Base, 'title'> {}
Closing as this comes down to typescript limitations. Feel free to reopen if you think that is not the case.
Astro Info
If this issue only occurs in one browser, which browser is a problem?
na
Describe the Bug
I have this component:
Typescript error:
Changing the line to
type Props = Base
will remove the error.Also, chaing the interface to Omit slot will remvoe the error:
Or this:
Or even
slot: string
What's the expected result?
I expect it not to give a ts error
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-8wxgcg?file=src%2Fcomponents%2FComponent.astro
Participation