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
Typescript error TS2420 : Class incorrectly implements interface
提问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 InternalStateType
all properties of InternalStateType
are members of the state
properties.
您定义的接口只有“直接属性”,但您的类应该实现的InternalStateType
所有属性InternalStateType
是属性的成员state
。
You have to remove the state
property from your class. Everything should work then :)
您必须state
从班级中删除该属性。一切都应该工作:)
回答by Saravana
You do not actually have the appName
property on AppService
type. Your state
is typed as InternalStateType
and has appName
property. If you actually want AppService
to 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 AppService
to implement InternalStateType
, remove it from the class declaration:
如果您不打算AppService
实施InternalStateType
,请将其从类声明中删除:
export class AppService {
// ...
}