Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

withDefaults and unions #29

Open
OliverJAsh opened this issue Sep 17, 2018 · 0 comments
Open

withDefaults and unions #29

OliverJAsh opened this issue Sep 17, 2018 · 0 comments

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Sep 17, 2018

Re. https://github.com/gcanti/typelevel-ts/blob/e1d718db887476f7b6b6e2c88542cc6ce3bd8265/examples/withDefaults.tsx

I have a use case for withDefaults that doesn't appear to work, but I'm not entirely clear why.

type Props =
    | {
          tag: 'a';
          href: string;
      }
    | {
          tag: 'button';
      };

declare const props: Props;

type DefaultProps = {
    tag: 'a';
};

declare const defaultProps: DefaultProps;

declare function withDefaults<Props extends DefaultProps, DefaultProps extends object>(
    props: Props,
    defaultProps: DefaultProps,
): void;

withDefaults(
    // Type '"button"' is not assignable to type '"a"'.
    props,
    defaultProps,
);

My thinking:

  • withDefaults requires DefaultProps (defaultProps) to be a "supertype" of Props (props) (Props extends DefaultProps)
  • In this example, DefaultProps is not a supertype of Props because Props is a union

Any thoughts?

@OliverJAsh OliverJAsh changed the title withDefaults subtype/supertype example withDefaults and unions Sep 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant