[英]Testing i18next locales in Typescript
我有一個 function 返回語言環境如下:
getVal.ts
import i18next from 'i18next';
export const getVal = (val) => {
return i18next.t('value', { val })
}
和一個測試
獲取值.spec.ts
describe('get value', () => {
it("return the value expected", () => {
expect(getVal()).toBe('two')
})})
然而,這會返回一個undefined
的錯誤,我不確定如何解決 go。 任何幫助將不勝感激
您在getVal
上使用的val
變量未定義,因此它可能是導致undefined
錯誤的變量。 嘗試添加類型如下:
import i18next from 'i18next';
export const getVal = (val:any) => {
return i18next.t('value', { val })
}
我選擇 type any
是因為我沒有深入研究 i18next,所以我不知道 function 返回什么類型。 我也嘗試了 type number
並且測試仍然通過,所以我不確定i18next.t
function 返回什么類型。 也可能是因為我在 Angular 上試過這個。您可以查看https://react.i18next.com/latest/typescript以使 function 完全鍵入。 在 React 上似乎完全不同。 讓我知道它是怎么回事!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.