Imagine having an interface S
:
interface S {
a: string;
b: number;
c: string;
}
The goal is to establish an incomplete mapping from S
to another type called AllowedMappedType
:
type AllowedMappedType = "red" | "green" | "blue";
const map: Record<keyof S, AllowedMappedType> = {
a: "red",
c: "green",
};
This approach encounters a problem as not all keys of S
are mapped. To resolve this issue, one might consider eliminating type specialization:
const map = {
a: "red",
c: "green",
}; // the type of map becomes { a: string, c: string }, losing the restriction on AllowedMappedType
However, maintaining the constraint of AllowedMappedType
is desired. Using Partial
doesn't help either since the intention is for the type to have non-optional properties.
So, how can this be accomplished?