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

ObjectOmit does not carry across properties from all variants in union #11

Open
OliverJAsh opened this issue Sep 20, 2017 · 4 comments
Open

Comments

@OliverJAsh
Copy link
Contributor

Is this expected behaviour? Here are two examples using ObjectDiff and ObjectOmit.

import { ObjectDiff, ObjectOmit } from 'typelevel-ts/lib';

{
  type Common = {
    commonProp: string;
  };

  type Full = Common &
    (
      | {
          type: 'foo';
          foo: string;
        }
      | { type: 'bar'; bar: string });
  type Defaults = Pick<Full, 'commonProp'>;

  type Input = ObjectDiff<Full, Defaults>;

  const check = (i: Input) => i;
  check({
    type: 'foo',
    foo: 'foo', // unexpected error
  });
  check({
    type: 'bar',
    bar: 'bar', // unexpected error
  });
}

{
  type Common = {
    commonProp: string;
  };

  type Full = Common &
    (
      | {
          type: 'foo';
          foo: string;
        }
      | { type: 'bar'; bar: string });
  
  type Input = ObjectOmit<Full, 'commonProp'>;

  const check = (i: Input) => i;
  check({
    type: 'foo',
    foo: 'foo', // unexpected error
  });
  check({
    type: 'bar',
    bar: 'bar', // unexpected error
  });
}
@OliverJAsh
Copy link
Contributor Author

Seems like the crux of the issue here is keyof:

type Foo = keyof Full;
// expected "commonProp" | "type" | "bar" | "foo", got "commonProp" | "type"

@gcanti
Copy link
Owner

gcanti commented Sep 20, 2017

yes, keyof a union yields the common fields

@OliverJAsh
Copy link
Contributor Author

Not an issue with this library in that case. Thanks!

To your knowledge, is there any way to omit common properties from a union, and retain the union members?

@OliverJAsh
Copy link
Contributor Author

I believe we can fix this now with conditional types:

type Omit<T, K extends keyof T> = T extends any ? Pick<T, Exclude<keyof T, K>> : never

microsoft/TypeScript#12215 (comment)

@OliverJAsh OliverJAsh changed the title ObjectOmit does not carry across properties from all variants in union ObjectOmit does not carry across properties from all variants in union Sep 17, 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

2 participants