gpt4 book ai didi

angular - 错误 TS2345 : Argument of type 'Menu' is not assignable to parameter of type

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:08 24 4
gpt4 key购买 nike

美好的一天。我是使用 VSCode 的 Type Script 新手。

出现以下错误:

error TS2345: Argument of type 'Menu' is not assignable to parameter of type '{ state: string; name: string; type: string; icon: string;
badge?: undefined; children?: undefine...'

代码:

import { Injectable } from '@angular/core';

export interface BadgeItem {
type: string;
value: string;
}

export interface ChildrenItems {
state: string;
name: string;
type?: string;
}

export interface Menu {
state: string;
name: string;
type: string;
icon: string;
badge?: BadgeItem[];
children?: ChildrenItems[];
}

const MENUITEMS = [
{
state: '/',
name: 'HOME',
type: 'link',
icon: 'explore'
},
{
state: 'account',
name: 'ACCOUNT',
type: 'sub',
icon: 'explore',
badge: [
{type: 'purple', value: 'new'}
],
children: [
{state: 'users', name: 'USERS'},
]
}
];

@Injectable()
export class MenuService {
getAll(): Menu[] {
return MENUITEMS;
}

add(menu: Menu) {
MENUITEMS.push(menu);
}
}

我们将不胜感激任何帮助。

最佳答案

只需像下面这样指定MENUITEMS的类型,警告就会消失

const MENUITEMS : Menu[] = [
{
state: '/',
name: 'HOME',
type: 'link',
icon: 'explore'
},
{
state: 'account',
name: 'ACCOUNT',
type: 'sub',
icon: 'explore',
badge: [
{type: 'purple', value: 'new'}
],
children: [
{state: 'users', name: 'USERS'},
]
}
];

关于angular - 错误 TS2345 : Argument of type 'Menu' is not assignable to parameter of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com