我定义了一个接口tallyInter,然后想要通过injectionKey将类型传给孙子组件,可以却报以下错误
这是接口
export interface tallyInter{
amount:string,
category:string,
time:string,
type:string,
}
//这是injectionKey
export const tallyKey : InjectionKey = Symbol('')
这是父组件的部分代码
const state = reactive<{tally : tallyInter []}>({
tally:[]
})
onMounted(async ()=>{
await axios.get('http://127.0.0.1:5500/static/json/bills.json').then((res)=>{
state.tally.push(...res.data.bills) //方式2,可以获取,在一开始会初始化进去
})
.catch((err)=>{
console.log(err);
}
)
});
//然后放入provide中
provide(tallyKey,state.tally);
这是孙子组件
export default defineComponent({
name: 'contentitem',
props: {
tally:{
type:Object as ()=> tallyInter
}
},
components: {
},
setup(props, ctx: SetupContext){
//接受从父组件传递过来的数据
const tally = inject(tallyKey);
}
Argument of type '{ amount: string; category: string; type: string; time: string; }[]' is not assignable to parameter of type 'tallyInter'.
Type '{ amount: string; category: string; type: string; time: string; }[]' is missing the following properties from type 'tallyInter': amount, category, type, time
试过将tallyInter的属性设置为可选属性,其他的没变,然后在provide的时候进行类型断言,然后报了其他的错
Conversion of type '{ amount: string; category: string; type: string; time: string; }[]' to type 'tallyInter' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type '{ amount: string; category: string; type: string; time: string; }[]' is missing the following properties from type 'tallyInter': amount, category, type, time
export interface tallyInter{
amount?:string,
category?:string,
time?:string,
type?:string,
}
provide(tallyKey,state.tally as tallyInter);
怎么能够解决这些错误呢,求解答o(╥﹏╥)o