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
        },
    ] 
    }
  }
}) 
回答:
interface State {    goods: Igoods[]
}
const useShopStore = defineStore('shop', {
  state: (): State => {
    return {
      goods: []
    }
  },
})
回答:
也可以使用 <Type>
const a:any = {};const goods = <Igoods[]>a;
以上是 ts-请问怎么用接口约束这个返回的数据呢? 的全部内容, 来源链接: utcz.com/p/934126.html




