How can I inform TypeScript that the code is functionally valid?
It keeps suggesting it could be a string, but I am unsure how that would happen. Is this a bug in my code or am I inputting something wrong?
For example:
const i18nInstance = {
options: {defaultNS: 'common'}
}
const getInitialProps = (req: any, namespaces?: string | string[]) => {
if (!namespaces) {
namespaces = i18nInstance.options.defaultNS
}
if (typeof namespaces === 'string') {
namespaces = [namespaces]
}
const initialI18nStore = req.i18n.languages.reduce((langs, lang) => {
// TypeScript believes namespaces might be a string, when it will always be an array.
langs[lang] = namespaces.reduce((ns, n) => {
ns[n] = (req.i18n.services.resourceStore.data[lang] || {})[ns] || {}
return ns
}, {})
return langs
}, {})
return {
i18n: req.i18n,
initialI18nStore,
initialLanguage: req.i18n.language,
}
}