Skip to content

Make TS Discriminating Unions Props PossibleΒ #9130

@arkmech

Description

@arkmech

Describe the problem

<script context="module" lang="ts">
  export type Props = {
    name: string;
  } & (
    | { iconType?: 'default'; icon?: typeof SvelteComponent }
    | {
        iconType?: 'featured';
        icon?: typeof SvelteComponent;
        featuredIconProps?: FeaturedIconProps;
      }
  );
</script>

<script lang="ts">
  type $$Props = Props;

  export let name: $$Props['name'];
  export let iconType: $$Props['iconType'] = undefined;
  export let icon: $$Props['icon'] = undefined;
</script>

{#if iconType === 'default'}
  <svelte:component this={icon} />
{:else if iconType === 'featured'}
  <FeaturedIcon {icon} {...featuredIconProps}  />
{/if}

It doesn't seem possible to access unique props from discriminated unions

Describe the proposed solution

Attempted to access a unique prop.

<script lang="ts">
  let props: $$Props;
  if (iconType === 'featured') {
    props. // featuredIconProps doesn't show up.
  }
</script>

Alternatives considered

None

Importance

i cannot use svelte without it

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions