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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42995138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:22:43  来源:igfitidea点击:

Typescript error TS2420 : Class incorrectly implements interface

typescript

提问by Sebastian Sebald

Upon building my app , I get the following error, which seems strange to me as the property appName is stated :

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

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

../src/app/app.service.ts (30,14) 中的错误:“AppService”类错误地实现了“InternalStateType”接口。“AppService”类型中缺少属性“appName”。)

app.service.ts

应用服务.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;
  }
}

Where am I wrong ? thanks for your feedback

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

回答by Sebastian Sebald

The interface you defined does only has "direct properties", but your class that should implement InternalStateTypeall properties of InternalStateTypeare members of the stateproperties.

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

You have to remove the stateproperty from your class. Everything should work then :)

您必须state从班级中删除该属性。一切都应该工作:)

回答by Saravana

You do not actually have the appNameproperty on AppServicetype. Your stateis typed as InternalStateTypeand has appNameproperty. If you actually want AppServiceto implement InternalStateType, define it like below:

您实际上没有类型的appName属性AppService。您state的类型为InternalStateType并具有appName属性。如果你真的想AppService实现InternalStateType,定义如下:

@Injectable()
export class AppService implements InternalStateType {

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

}

If you did not intend for AppServiceto implement InternalStateType, remove it from the class declaration:

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

export class AppService {
  // ...
}