I am having trouble getting the type assertion to work in this specific scenario.
Here is a Playground Link
type Letter = "A" | "B"
type Useless = {}
type Container<T> =
Useless |
{
type: "container"
value: T
}
function transform<X extends Letter, Y extends Letter>(container: Container<X | Y>): asserts container is Container<X> {
// custom logic
}
let container: Container<"A" | "B"> = undefined as any
// Asserts no B
transform<"A", "B">(container)
container // Container<"A" | "B">
// Container<"A"> when Useless is commented
// I want Container<"A">
I want container to be of type Container<"A">
.
Removing type Useless altogether makes it work as expected.
Replacing type Useless with type Useless = { key: string }
results in container being of type Useless instead of Container<"A">
.
How can I achieve the desired type of Container<"A">
?