1

Note: The underlying issue was asked separately. This is a follow-up to a provided solution.


Typescript allows to create an Omit type since 2.8 via Exclude.

I want to use Omit in on an older codebase using typescript@2.3.

On this blog an implementation is shown which is:

type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];  
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;  

Yet this one behaves weirdly for me, as:

inteface MyTarget {
    a: string;
    b: string;
    c: string;
}

type MyPartial = Omit<MyTarget, "a" | "b">;

const foo: MyPartial = {
    c: "foo",
};

will complain that my foo is lacking properties:

src/helper/types/Omit.ts(12,7): error TS2322: Type '{ c: string; }' is not assignable to type 'Pick<MyTarget, "a" | "b" | "c">'.
  Property 'a' is missing in type '{ c: string; }'.

Yet the whole idea is to generate a type that lacks certain fields.

How do I create a proper Omit type in TypeScript without upgrading to a more current typescript version?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347

0 Answers0