简体   繁体   English

错误TS2345:“菜单”类型的参数无法分配给类型参数

[英]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.

相关问题 错误TS2345:“对象”类型的参数无法分配给类型的参数 - Error TS2345: Argument of type 'Object' is not assignable to parameter of type TS2345类型的参数…无法分配给类型的参数 - TS2345 Argument of type … Is not assignable to parameter of type TS2345:“HTMLElement”类型的参数不可分配给“ChartItem”类型的参数 - TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' TS2345:“RolesGuard”类型的参数不可分配给“CanActivate”类型的参数 - TS2345: Argument of type 'RolesGuard' is not assignable to parameter of type 'CanActivate' TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' TS2345:“字符串”类型的参数不可分配给“从不”类型的参数 - TS2345: Argument of type 'string' is not assignable to parameter of type 'never' 我收到错误 TS2345:X 类型的参数不可分配给 Y 类型的参数 - I have got error TS2345: Argument of type X is not assignable to parameter of type Y 错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon' Angular 错误 TS2345:“MonoTypeOperatorFunction”类型的参数<event> ' 不可分配给 'OperatorFunction 类型的参数<event, event> '</event,></event> - Angular Error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>' 错误 TS2345:'{ 类型的参数读取:typeof ElementRef; }' 不可分配给类型为 '{ read?: any; 的参数 static:boolean; }' - error TS2345: Argument of type '{ read: typeof ElementRef; }' is not assignable to parameter of type '{ read?: any; static: boolean; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM