gpt4 book ai didi

typescript 错误 TS2420 : Class incorrectly implements interface

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:54 26 4
gpt4 key购买 nike

在构建我的应用程序时,我收到以下错误,这对我来说似乎很奇怪,因为属性 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com