My objective is to establish a custom type resembling a record, where certain keys are designated for specific value types.
The proposed object would look something like this:
const o: TRec = {
text: "abc",
width: 123,
height: 456,
//...any string key with a numerical value
}
In this scenario, "text" must exclusively be associated with a string value, while all other keys should have numeric values.
Despite various attempts, I have been unable to define the TRec
type successfully.
I have experimented with different approaches as shown below, but none of them fulfill the requirements outlined above. The compiler keeps generating the following error message:
Property 'text' is incompatible with index signature. Type 'string' is not assignable to type 'number'.
type TRec = Record<string, number> &{
text: string;
}
type TRec = {
[key: string]: number;
text: string;
}
type TRec = Omit<Record<string, number>, "text"> & {
text: string;
}
Any suggestions on how to resolve this issue?