我有一个项目列表,我可以点击这些项目来转到不同的页面.这些项存储在数组中.它们的形状如下:
type Item = {
label: string
path: ItemPath
}
type ItemPath = "/item/one" | "/item/two" | "/item/three"
其中ItemPath
是类型I,声明了每个项目的所有可能路由.
然后,我try 使用Link
组件,如下所示:
import { Link } from 'expo-router'
const items: Item[] = [
{label: 'one', path: '/item/one'},
{label: 'two', path: '/item/two'},
{label: 'three', path: '/item/three'}
]
export default function Page() {
// ...
return (
<View>
{items.map(item => (
<Link href={item.path} key={item.label}>
{item.label}
</Link>
)}
</View>
)
}
然而,href属性给了我一个打字错误:
类型‘ItemPath’不可赋值给类型‘StaticRoutes|RelativePathString|
http${string}
’.
我试过用<Link href={item.path as RelativePathString} />
,但我找不到那种类型.我也花了一些时间寻找信息,但我找不到任何与我的问题相关的信息.
我希望TypeScrip能理解我的ItemPath类型是应用程序中所有路径的子集.
我该怎么解决这个问题?