Created
October 22, 2019 13:37
-
-
Save sandrosc/49aeb3638bb5b8072b3850c0152daa3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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