I am looking to enhance the type-checking for the following object:
interface Config {
fields: Record<string, unknown>;
table: { id: string }[];
}
const config: Config = {
fields: {
id: {}
},
table: [
{ id: 'id' },
{ id: 'name' },
]
};
The challenge I am facing is ensuring that the type-checking validates that the id
in the table
object matches a key in the fields
. If the id
in the table
is not present in the keys of fields
, then it should result in an error. For example:
{ id: 'id' }
should be accepted, as theid
key exists infields
{ id: 'name' }
should trigger an error, as the keyname
does not exist infields
Is there a way to achieve this? If not, I may consider implementing this check at runtime as a Plan B.