Skip to content

Instantly share code, notes, and snippets.

@sandrosc
Created October 22, 2019 13:37
Show Gist options
  • Save sandrosc/49aeb3638bb5b8072b3850c0152daa3f to your computer and use it in GitHub Desktop.
Save sandrosc/49aeb3638bb5b8072b3850c0152daa3f to your computer and use it in GitHub Desktop.
interface A {
type: 'A';
j: {
k: number;
a: number;
};
}
interface B {
type: 'B';
j: {
k: number;
b: number;
};
}
interface NewA {
type: 'A';
j: {
a: number;
};
}
interface NewB {
type: 'B';
j: {
b: number;
};
}
type AorB = A | B;
type NewAorB = NewA | NewB;
function removeK(o: AorB): NewAorB {
const {
j: { k, ...j }
} = o;
return {
...o,
j
};
// Type '{ j: { a: number; } | { b: number; }; type: "A"; } | { j: { a: number; } | { b: number; }; type: "B"; }' is not assignable to type 'NewAorB'.
// Type '{ j: { a: number; } | { b: number; }; type: "A"; }' is not assignable to type 'NewAorB'.
// Type '{ j: { a: number; } | { b: number; }; type: "A"; }' is not assignable to type 'NewA'.
// Types of property 'j' are incompatible.
// Type '{ a: number; } | { b: number; }' is not assignable to type '{ a: number; }'.
// Property 'a' is missing in type '{ b: number; }' but required in type '{ a: number; }'.
// ts(2322)
}
function removeK2<T extends AorB>(o: T): NewAorB {
const {
j: { k, ...j }
} = o;
return {
...o,
j
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment