繁体   English   中英

打字稿警告“使用接口而不是类型文字”

[英]Typescript warning “Use an interface instead of a type literal”

整理我的代码,我在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在我的应用服务中使用

/**
 * 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,
  };

是不是意味着应该更好地替换:

export type InternalStateType = {

和:

export interface InternalStateType {

对谁可能关心,应该是

export interface InternalStateType {

没有等号。

这是文档

是的,就是这样。

没有理由让 InternalStateType 成为接口上的类,因为它没有自己的行为——它只是一个 DTO 类型的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM