打字稿错误TS2420:类错误地实现了接口

时间:2017-03-24 08:56:44

标签: typescript

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

错误在../src/app/app.service.ts(30,14):Class' AppService'错误地实现了界面' InternalStateType'。   属性' appName'在“AppService'。”

类型中缺少

app.service.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;
  }
}

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

2 个答案:

答案 0 :(得分:3)

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

您必须从班级中删除final class index { public $request; private $loader; private $twig; /** * Constructor. * definition : class preloader with default * symfony component request class * twig template configuration * @param type dependency injection and function */ public function __construct(){ //get request info $this->request=new request(); $this->loader = new \Twig_Loader_Filesystem(root.'/src/declarations/twigTemplate'); $this->twig = new \Twig_Environment($this->loader, array( //'cache' => root.'/src/declarations/twigTemplate/cache', )); } /** * get declaration main function. * definition:index method is defined in a declaration * and it is called as https://ip/company/service/app/service/doc * @param type dependency injection and function * @return array */ public function index(){ //return return $this->twig->render("index.twig",['var'=>'foo']); } 属性。一切都应该工作:)

答案 1 :(得分:0)

appName类型实际上没有AppService属性。您的state输入为InternalStateType且具有appName属性。如果您确实希望AppService实施InternalStateType,请按以下方式对其进行定义:

@Injectable()
export class AppService implements InternalStateType {

  private appName: string;
  // ... define other members of `InternalStateType` here

}

如果您不打算AppService实施InternalStateType,请将其从类声明中删除:

export class AppService {
  // ...
}