ts-请问怎么用接口约束这个返回的数据呢?
现在就是我定义了一个接口Igoods,但是我不知道怎么用它来约束下面返回的goods这个数组,老师写的方法是在数组后面加上了as Igoods[],请问还有其他的方法吗?感谢各位大佬!
代码部分:
import {defineStore} from 'pinia'
// 定义的接口
interface Igoods {
name: string,
price: number,
num: number
}
export const useShopStore = defineStore('shop', {
state: () => {
return {
//怎么给下面的goods用接口呢?
goods: [
{
name: '羊肉串',
price:20,
num: 10
},
{
name: '猪肉串',
price:15,
num: 10
},
{
name: '鸡肉串',
price:10,
num: 10
},
]
}
}
})
回复
1个回答

test
2024-07-05
interface State {
goods: Igoods[]
}
const useShopStore = defineStore('shop', {
state: (): State => {
return {
goods: []
}
},
})
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容