Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typings for functions returning undefined #198

Closed
wants to merge 1 commit into from

Conversation

xamgore
Copy link
Contributor

@xamgore xamgore commented Oct 3, 2020

PR updates typings, so that they are typesafe: if garbage is passed in, undefined is returned. And a programmer will have to handle that situation somehow.

export function getAlpha3Code(name: string, lang: string): string;
export function getSimpleAlpha3Code(name: string, lang: string): string;
export function getName(alpha2orAlpha3orNumeric: string | number, lang: string): string | undefined;
export function getNames(lang: string): LocalizedCountryNames | undefined;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNames does not return undefined. It returns an empty object?

Copy link
Contributor Author

@xamgore xamgore Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right.

exports.getNames = function (lang) {
  var d = registeredLocales[lang.toLowerCase()];
  if (d === undefined) {
    return {};
  }
  return d;
};

Well, we can't return {} and object types here, as there will be a lot of cumbersome service code. Though we can return Partial<LocalizedCountryNames>, so the type of getNames('en')['ru'] will be string | string[] | undefined. The other option is lang specification: for predefined lang values return LocalizedCountryNames, for the other return Partial<LocalizedCountryNames>.

At this step, I would prefer just Partial<LocalizedCountryNames>. What do you think?

Copy link
Owner

@michaelwittig michaelwittig Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that makes sense for a TypeScript dev I'm fine with it :) I'm not using TypeScript so I have no opinion and follow what you suggest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ibarrae what would you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will implement Partial<...>.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a problem with using {}. You just need to check if a key is present, for instance

const names = getNames("en");
if ("US" in names) {
  // you can use it here
}

@michaelwittig
Copy link
Owner

@xamgore I think we can merge this. Would you be able to resolve the conflicts?

@xamgore
Copy link
Contributor Author

xamgore commented Oct 26, 2020

Yes, no problems.

@michaelwittig
Copy link
Owner

@xamgore one additional question: Do you have any experience with testing those type definition files? We had an issue last week where the file was "broken" and we did not notice because of the lack of a test. Let me know if you have any ideas in this area.

@xamgore
Copy link
Contributor Author

xamgore commented Oct 26, 2020

There are at least two tools. The first one checks that some code in typescript passes the type check. The other allows to write assertions about types itself, like, assert the returning type of a function is number.

@michaelwittig
Copy link
Owner

@xamgore I see. If you don't mind adding any test that is better than no test I would be very happy :)

@osdiab
Copy link

osdiab commented Sep 29, 2021

I will make a new PR with some refined versions of these changes as the base; I've also written typescript typings test files before so I'll do one of those.

@osdiab
Copy link

osdiab commented Sep 29, 2021

That said i think that a test won't really help whether or not it's "correct", since the code itself isn't written in typescript the test can be just as buggy as the typing. These types aren't complex so I think a test doesn't get us much value.

@michaelwittig
Copy link
Owner

related #259 and #319
I will close this PR because as ai said "getNames does not return undefined. It returns an empty object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants