Typescript lets me set an index type on a method signature like so:
function myFunc(dict: { [x: string]: string }) {
...
}
so this function will only accept objects that have string
indicies and string
values - akin to Dictionary<string, string>
in C#. Can I restrict this further so that only objects that have a single string key with a string value are accepted?
i.e. so that
const a = {
x: 'val'
};
is valid, but
const b = {
x: 'val1',
y: 'val2'
};
is not?
Obviously I can check the length of Object.keys()
but it would be nice if the compiler could enforce it.