Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

Cannot invoke an object which is possibly 'undefined'. using lodash.memoize

$
0
0

I'm using an example from react-native-localize and I want to support Typescript but I'm getting this error from line 36 translate.cache.clear(); the code works but I don't know how to convert memoize to a valid type, I'm new to this stuff, I have lodash.memoize types installed too, and the only way that I can remove this error right now is disabling strict mode in the tsconfig.json, can anyone help me please?

import * as RNLocalize from 'react-native-localize';
import i18n from 'i18n-js';
import memoize from 'lodash.memoize';
import {I18nManager} from 'react-native';

type RequireFunction = {
  (): object;
};

interface TranslationGetters {
  [key: string]: RequireFunction;
}
[];

export const translationGetters: TranslationGetters = {
  // lazy requires (metro bundler does not support symlinks)
  en: () => require('../../locales/en.json'),
  es: () => require('../../locales/es.json'),
};

export const translate = memoize(
  (key: string) => i18n.t(key),
  // (key, config) => i18n.t(key, config),
  // (key, config) => (config ? key + JSON.stringify(config) : key),
);

export const setI18nConfig = () => {
  // fallback if no available language fits
  const fallback = {languageTag: 'en', isRTL: false};

  const {languageTag, isRTL} =
    RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) ||
    fallback;

  // clear translation cache
  translate.cache.clear();
  // update layout direction
  I18nManager.forceRTL(isRTL);

  // set i18n-js config
  i18n.translations = {[languageTag]: translationGetters[languageTag]()};
  i18n.locale = languageTag;
};

Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>