catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.77k stars 218 forks source link

parseNumber type definition doesn't consider invalid case #371

Closed sman591 closed 4 years ago

sman591 commented 4 years ago

I noticed the following in the README for parseNumber():

parseNumber(text, [defaultCountry], [options]) ... If the phone number supplied isn't valid then an empty object {} is returned.

The invalid state here, where "an empty object {} is returned", isn't captured by the current TypeScript definition:

https://github.com/catamphetamine/libphonenumber-js/blob/7cf75a796a5cc11f4510ddb407fa8d025073cae1/custom.d.ts#L54

I'd like to propose changing this type to match the full return type of this function; something like:

export function parseNumber(text: string, metadata: Metadata): ParsedNumber | {};

Is there a reason it isn't typed like this to begin with?

catamphetamine commented 4 years ago

Is there a reason it isn't typed like this to begin with?

Me not using TypeScript.

I'll add the | {}, thx.

sman591 commented 4 years ago

Cool! I wasn't sure if there was any historical reason behind it. Thanks for the quick response!

catamphetamine commented 4 years ago

By the way, parseNumber() has been deprecated for a while, and the new API is parsePhoneNumberFromString().

catamphetamine commented 4 years ago

Should be fixed in the latest version.

sman591 commented 4 years ago

Thank you!