【发布时间】:2022-05-06 15:37:33
【问题描述】:
我正在将一个 React 项目重写为 TypeScript。
包:Next.js、next-i18next、styled-components
编辑:
答案在当前next-i18next 版本中已过时。
请参考: Typescript i18next does not satisfy the constraint 'string | TemplateStringsArray NextJS
快速示例:
{t<string, ItemProps[]>('document.items', {returnObjects: true}).map(({ name, description, index }) => (
<li key={index}>
<strong dangerouslySetInnerHTML={{__html: name}} />
<Paragraph dangerouslySetInnerHTML={{__html: description}} />
</li>
)
)}
我还为此解决方案创建了codesandbox。
原问题:
我在语言 json 文件中使用了一组对象:
{
"websites":
[
{
"slug": "website-a",
"name": "Website A"
},
{
"slug": "website-b",
"name": "Website B"
}
]
}
用 i18next 的{ t } 列出所有这些:
t('websites', {returnObjects: true}).map(({slug, name}) => ( ... ))
TypeScript 下划线map:
Property 'map' does not exist on type 'string'.
用 TypeScript 重写了这个:
type websiteProps = {
map: Function;
};
type itemProps = {
slug: string;
name: string;
};
t<websiteProps>('websites', {returnObjects: true}).map(({slug, name}: itemProps) => ( ... ))
这行得通,但我想知道我是否只是为.map“消除”下划线。
有什么更好的办法吗?
谢谢!
【问题讨论】:
标签: typescript next.js react-i18next