I use TypeScript version 2.3.4. I want to write a function that accepts an object that must have specified fields. But this object should not contain any other fields. How can I achieve this?
Now this works only if I define object inline. But if I use another object with extra fields - the compiler will allow it. What is totally wrong.
Example:
function foo(arg: { a: string }) { // there is tons of fields actually
// ...
}
foo ({a: "qwerty"}); // No Error - ok
foo ({a: "qwerty", b: 123}); // Error - ok
let bar = {
a: "qwerty",
b: 123
};
foo (bar); // No Error - NOT OK !!!
The same code can be writed with interfaces, classes, type declarations - it's the same problem.
Now I have to extract the fields from the object manually to make sure that there are no extra fields. But I can't spread this solution on ~1000 functions (I really need this) all over the code - it's too messy. I creating API wrapper and I need to ensure that there are no additional or wrong fields passed to the server.