wojtekmaj / get-user-locale

A function that returns user's locale as an IETF language tag, based on all available sources.
MIT License
58 stars 9 forks source link

Call could still return null when using fallbackLocale #80

Closed bravo-kernel closed 1 year ago

bravo-kernel commented 1 year ago

Unsure if this is related to this module but I was expecting that defining a fallbackLocale would always return a string. I am trying to do this:

import { getUserLocale } from "get-user-locale"

const locale = getUserLocale({
  fallbackLocale: "en",
  useFallbackLocale: true
})

if(["in", "hi-IN"].includes(locale){
  // do something
}

However, include complains with below type error about possible null so I am guessing it is not detected:

Argument of type 'string | null' is not assignable to parameter of type 'string'.
  Type 'null' is not assignable to type 'string'.

Any pointers on how to solve this, if possible at all?

bravo-kernel commented 1 year ago

It seems I can just add an exclamation mark to locale to get rid of the type error.

if(["in", "hi-IN"].includes(locale!){
  // do something
}

Feel free to close unless you think this should be handled by the module.

wojtekmaj commented 1 year ago

Unless useFallbackLocale was specifically set to false, indeed there's no possibility of getting null back. I fixed that by adding function overloads :)

bravo-kernel commented 1 year ago

Ah, perfect. Much appreciated ❤️