-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Open
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
Distributive types, conditional
π Version & Regression Information
Always
β― Playground Link
π» Code
//
// Basic type definition //
//
type A =
// Base type A0 has ID and Title
A0 &
// Discriminated types(based on 'type' property) from A1 and A2
(A1 | A2)
type A0 = {
ID: string;
Title: string; // <-- I added the "Title" as a measure to ensure that Omit<A,"ID"> won't be an 'any' empty object
}
//
// discrimination types //
//
type A1 = {
type: "a1";
}
type A2 = {
type: "a2";
status: "status";
}
// This example shows that Omit is not working as expected
namespace not_working {
//
// Augmentation of "Type A" //
// It just attempts to rename ID to be id
//
type B = Omit<A, "ID"> & {
id: string;
}
//
// Instance of Type B
//
const b: B = {
Title: "",
type: "a1",
id: "",
};
// Clearly, it is expected that ...other satisfies A without its ID property.
const {
// Destructing to remove "id" property
id,
...other
} = b;
const a: A = { // <-- TypeError
...other,
ID: "",
};
}
// This example shows that without omit it does work
namespace working {
type B = A & {
id: string;
}
const b: B = {
ID: "",
Title: "",
type: "a1",
id: "",
};
const {
id,
ID, // <-- Even when extract A's ID, so ...other should be like Omit<A, "ID" | "_id">
...other
} = b;
const a: A = { // <-- Works as expected
...other,
ID: "",
};
}π Actual behavior
I think that the Omit causes a conditional distributive type to trigger or something.
π Expected behavior
I expect that the code example will work as expected and that the TypeError will not appear in the not_working namespace.
Additional information about the issue
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created