[英]error TS2345: Argument of type 'Menu' is not assignable to parameter of type
Good day. 美好的一天。 I'm new to Type Script, using VSCode.
我是使用VSCode的Type Script的新手。
Getting following errors : 出现以下错误 :
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...'
the code : 代码 :
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);
}
}
Any help will be highly appreciated. 任何帮助将不胜感激。
Just specify the type of MENUITEMS
like below, the warning will go away 只需指定如下所示的
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'},
]
}
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.