gcanti / typelevel-ts

Type level programming in TypeScript
https://gcanti.github.io/typelevel-ts
MIT License
357 stars 12 forks source link

`withDefaults` and unions #29

Open OliverJAsh opened 6 years ago

OliverJAsh commented 6 years ago

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:

Any thoughts?