I want to define a function that returns one of the strings specified in typescript.

Asked 1 years ago, Updated 1 years ago, 59 views

I am writing in typescript.I would like to write a function to fall back to the default locale when the system returns a different locale because the language (locale) supported by an application is limited.

If /*currentLocale is SupportedLocale, how should I write */?

typeSupportedLocale='ja'|'en';

export function currentLocale():SupportedLocale{
  // Remove locale from the system.
  const currentLocale=Localization.locale.split('-')[0];

  if (/*currentLocale is SupportedLocale */) {
    return currentLocale
  }

  return 'ja';  
}

If you search by type guard, the following description appears, but I don't want to make changes to two places when the number of locale support increases.

export function currentLocale():SupportedLocale{
  const currentLocale=Localization.locale.split('-')[0];

  if(currentLocale=='ja'||currentLocale=='en'){
    return currentLocale;
  }

  return 'ja';  
}

I thought it was typeof, but typeofcurrentLocale is probably string, and 'SupportedLocale' only references to a type, but is being used as a value here.ts(2693) and cannot be placed on the left.

 if(typeof currentLocale===SupportedLocale){
    return currentLocale;
  }

I found a user-defined guard and used it, but in the end, support locale information is scattered in two places.

export type SupportedLocale='ja'|'en';

constisSupportedLocale=(test:unknown): test isSupportedLocale=>{
  if(test=='ja'||test=='en'){
    return true;
  }

  return false;
};

export function currentLocale():SupportedLocale{
  const currentLocale=Localization.locale.split('-')[0];

  if(isSupportedLocale(currentLocale)){
    return currentLocale;
  }

  return 'ja';  
}

Well, all I'm doing is cutting out the type guard that I did in the IF statement into a function.How can I write a list of supported locale in only one place and make a decision?

javascript typescript

2022-09-30 19:28

1 Answers

By generating the Union type from an array of acceptable values, this problem can be solved succinctly.This method will be available in general.

// refer https://stackoverflow.com/a/45257357
const_SupportedLocale = [
  'ja',
  'en'
] as const
typeSupportedLocale=typeof_SupportedLocale [number]

// refer https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
const_SupportedLocaleValues:readonly string[]=_SupportedLocale

function isSupportedLocale(str:string):strisSupportedLocale{
  return_SupportedLocaleValues.includes(str)
}

/* apply it to your code...
const DEFAULT_LOCALE: SupportedLocale='ja'

export function currentLocale():SupportedLocale{
  const currentLocale=Localization.locale.split('-')[0]
  if(isSupportedLocale(currentLocale)){
    return currentLocale
  }
  return DEFAULT_LOCALE
}
*/

// For example...
console.log('ja', isSupportedLocale('ja')))
// true
console.log('en', isSupportedLocale('en')))
// true
console.log('zh', isSupportedLocale('zh')))
// false


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.