typescript 错误 TS2420 : Class incorrectly implements interface

标签 typescript

在构建我的应用程序时,我收到以下错误,这对我来说似乎很奇怪,因为属性 appName 已声明:

ERROR in ../src/app/app.service.ts (30,14): Class 'AppService' incorrectly implements interface 'InternalStateType'. Property 'appName' is missing in type 'AppService'.)

应用程序服务.ts

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

interface InternalStateType {
  [key: string]: any;
  appName: string;
  darkMode: boolean;
  defaultLang: string;
  topnavTitle: string;
  messagePanelOpen: boolean;
  sidenavOpen: boolean;
  sidenavMode: string;
  sidenavCollapse: boolean;
  pageFullscreen: boolean;
  pageFooter: boolean;
  initial: boolean
};


/**
 * App service
 */
@Injectable()
export class AppService implements InternalStateType {

  // Set your states default value.
  private state: InternalStateType = {
    appName: 'MyApp',
    darkMode: false,
    defaultLang: 'en',
    topnavTitle: 'MyApp',
    messagePanelOpen: false,
    sidenavOpen: false,
    sidenavMode: 'over',
    sidenavCollapse: true,
    pageFullscreen: false,
    pageFooter: false,
    initial: false
  };

  public cloneState(): InternalStateType {
    return JSON.parse(JSON.stringify(this.state));
  }

  public reloadState(state: InternalStateType) {
    this.state = state;
  }

  public getState(prop?: any): InternalStateType {
    const state = this.state;
    return state.hasOwnProperty(prop) ? state[prop] : state;
  }

  public setState(prop: string, value: any) {
    return this.state[prop] = value;
  }
}

我哪里错了?感谢您的反馈

最佳答案

您定义的接口(interface)只有“直接属性”,但您的类应该实现 InternalStateType InternalStateType 的所有属性是 state 的成员属性。

您必须删除 state你类(class)的属性(property)。一切都应该工作:)

关于 typescript 错误 TS2420 : Class incorrectly implements interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995138/

相关文章:

javascript - Angular 2动态设置输入最大长度

javascript - TypeScript 中导入模块的增强类型

typescript - 如何在 VS Code 上使用 typescript 调试 feathersjs?

angular - Angular2 RC7 和 angular-cli webpack 中的延迟加载

typescript - 在 Typescript 中键入键值对对象并保存对象的键

typescript - 推断类型 '1' 而不是数字

reactjs - 将 i18next 实例传递给 I18nextProvider 时出现类型错误

javascript - TS2349 : Cannot invoke an expression whose type lacks a call signature. 类型 'typeof ... renderIf"' 没有兼容的调用签名

javascript - 如何在 typescript 中使用传单初始化 SVG 层?

reactjs - Next.js api 路由器抛出 404