Both subdivision() and country() return an empty object on failure (ie, when they aren't able to find a country / subdivision based on the input).
In normal JavaScript, you could just test any field to know whether your request worked:
let sub = iso3166.subdivision("US-IN");
if (sub.code) {
// ...
}
In TypeScript, this is nearly impossible to do cleanly (since the compiler doesn't want you touching "code", which may or may not be set), and it might cause problems for other JavaScript variants as well.
It would be much cleaner if these functions returned a falsey value (false, null, undefined, ...) on failure. Testing for safety would then look like this:
let sub = iso3166.subdivision("SE-O");
if (sub) {
// ...
}
This change would cause existing code to throw TypeError, but be far cleaner in the long run.
Both subdivision() and country() return an empty object on failure (ie, when they aren't able to find a country / subdivision based on the input).
In normal JavaScript, you could just test any field to know whether your request worked:
In TypeScript, this is nearly impossible to do cleanly (since the compiler doesn't want you touching "code", which may or may not be set), and it might cause problems for other JavaScript variants as well.
It would be much cleaner if these functions returned a falsey value (false, null, undefined, ...) on failure. Testing for safety would then look like this:
This change would cause existing code to throw TypeError, but be far cleaner in the long run.