Skip to content

Commit

Permalink
Fix CompatibleSlotContent type (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Feb 6, 2023
1 parent c9f0027 commit 58c78a1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export interface VersionedSlotMap extends LatestSlotVersionMap {
*/
type Intersection<T, E> = T extends infer O ? O & E : never;

type UnionContent = {
[K in ComponentVersionId]: Intersection<ComponentContent<K>, {_component: K | null}>;
type UnionContent<T = null> = {
[K in ComponentVersionId]: Intersection<ComponentContent<K>, {_component: K | T}>;
};

type UnknownContent = UnionContent[ComponentVersionId] extends never
Expand All @@ -42,7 +42,7 @@ export type SlotVersionId<I extends SlotId = SlotId> = CanonicalVersionId<I, Ver

export type VersionedSlotId<I extends SlotId = SlotId> = VersionedId<I, VersionedSlotMap>;

export type CompatibleSlotContent<T extends ComponentVersionId = ComponentVersionId> = UnionContent[T];
export type CompatibleSlotContent<T extends ComponentVersionId = ComponentVersionId> = UnionContent<never>[T];

export type SlotContent<I extends VersionedSlotId = VersionedSlotId, C extends JsonObject = JsonObject> =
JsonObject extends C ? (string extends I ? UnknownContent : VersionedContent<I>) : C;
8 changes: 4 additions & 4 deletions test/slot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe('Slot typing', () => {

expect(() => compileCode(code)).not.toThrow();

expect(getTypeName(code)).toBe('Banner & {_component: "banner@1" | null;}');
expect(getTypeName(code)).toBe('Banner & {_component: "banner@1";}');
});

it('should export a CompatibleSlotContent type that resolves the slot content type for multiple components', () => {
Expand All @@ -268,9 +268,9 @@ describe('Slot typing', () => {
expect(() => compileCode(code)).not.toThrow();

expect(getTypeName(code)).toBe(
'(Banner & {_component: "banner@1" | null;})'
+ ' | (HorizontalBanner & {_type: \'horizontal-banner\';} & {_component: "hybrid-banner@1" | null;})'
+ ' | (VerticalBanner & {...;} & {_component: "hybrid-banner@1" | null;})',
'(Banner & {_component: "banner@1";})'
+ ' | (HorizontalBanner & {_type: \'horizontal-banner\';} & {_component: "hybrid-banner@1";})'
+ ' | (VerticalBanner & {_type: \'vertical-banner\';} & {_component: "hybrid-banner@1";})',
);
});

Expand Down

0 comments on commit 58c78a1

Please sign in to comment.