gcanti / typelevel-ts

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

ObjectOmit doesnt preserve optional keys #12

Closed ccorcos closed 6 years ago

OliverJAsh commented 6 years ago

You may have ran into this TS bug: https://github.com/Microsoft/TypeScript/issues/20722

type Foo =
  | {
      optionalProp?: string;
    }
  | {
      optionalProp?: string;
    };

// `optionalProp` is no longer optional
type Foo2 = Pick<Foo, keyof Foo>;
OliverJAsh commented 6 years ago

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

https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-378589263

gcanti commented 6 years ago

AFAIK In v0.3 Omit does preserve optional props

interface A {
  a: string
  b?: number
  c: boolean
}

type B = Omit<A, 'a'>
/*
type B = {
    b?: number | undefined;
    c: boolean;
}
*/
OliverJAsh commented 6 years ago

Oops, I was supposed to post that comment in https://github.com/gcanti/typelevel-ts/issues/11.