[英]Typescript warning “Use an interface instead of a type literal”
Linting my code, I get this TS warning on InternalStateType
definition整理我的代码,我在
InternalStateType
定义上收到了这个 TS 警告
[line 8] export type 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,
};
InternalStateType
is used in my App service InternalStateType
在我的应用服务中使用
/**
* App service
*/
@Injectable()
export class AppService {
// 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,
};
Does it mean that it should be better to replace:是不是意味着应该更好地替换:
export type InternalStateType = {
with:和:
export interface InternalStateType {
Yup, that's the case.是的,就是这样。
There's no reason to make InternalStateType a class here over an interface, since it has no behaviour of it's own - it's just a DTO-type thing.没有理由让 InternalStateType 成为接口上的类,因为它没有自己的行为——它只是一个 DTO 类型的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.