【问题标题】:Typescript warning "Use an interface instead of a type literal"打字稿警告“使用接口而不是类型文字”
【发布时间】:2017-08-15 20:05:29
【问题描述】:

检查我的代码,我在 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 {

【问题讨论】:

    标签: typescript interface


    【解决方案1】:

    对谁可能关心,应该是

    export interface InternalStateType {
    

    没有等号。

    这里是docs

    【讨论】:

      【解决方案2】:

      是的,就是这样。

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

      【讨论】:

        猜你喜欢
        • 2017-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-27
        • 2021-07-05
        • 2017-09-16
        • 2020-05-15
        • 1970-01-01
        相关资源
        最近更新 更多