In my current project, I am faced with the challenge of writing unit tests in Typescript for an existing library that was originally written in plain JS. Most of our page logic is also written in plain JS. Some functions in this library throw exceptions if incorrect types are passed as input. For example:
Collection.pluck([{"a":1},{"a":2}], "a") // Extracts values from a list of objects
> [1, 2]
The second argument in the above code snippet should be a string representing the property name from which to retrieve the values. The pluck function performs type checking to ensure that an array of objects has been provided, and it throws a TypeError if the check fails.
To write a test ensuring that the correct type of object is passed to the function, I would typically do so in plain JS as follows:
expect(function(){ Collection.pluck([{"a":1},{"a":2}], 0); }).toThrowError(TypeError);
However, my declaration file specifies the function signature like this:
declare namespace Collection {
function pluck(obj: Object, propertyName: string): any;
}
When attempting to write the same test in TypeScript, I encounter a compilation error:
So, my question is: how can I achieve the desired outcome without making changes to the original function? Is there a way to configure TypeScript to ignore this specific issue only within this file?