【问题标题】:Typescript: export interface with different nameTypescript:具有不同名称的导出接口
【发布时间】:2021-12-06 22:50:21
【问题描述】:

我需要导入接口,将属性添加到 id 并导出与原始接口同名的新接口。然后在所有使用这个接口的地方我只能更改导入位置。

import { Routes, Route } from '@angular/router';
interface Route2 extends Route {
    description: string;
}

export declare type Routes = Route2[];

然后我想(虽然不可能)

export Route2 as Route;

【问题讨论】:

    标签: typescript


    【解决方案1】:

    以不同的名称导入原始Route,并将新名称导出为Route

    import { Route as OriginalRoute } from '@angular/router';
    export interface Route extends OriginalRoute {
        description: string;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 export type 来做到这一点,它允许您为接口提供别名。

      一般示例(别名是新名称,SomeInterface 是原始名称):

      export type Alias = SomeInterface;
      

      您的具体情况:

      import { Routes, Route } from '@angular/router';
          interface Route2 extends Route {
              description: string;
          }
      
      export type Route = Route2;
      

      【讨论】:

        猜你喜欢
        • 2018-06-27
        • 1970-01-01
        • 2017-02-19
        • 2021-05-08
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        • 2015-08-23
        相关资源
        最近更新 更多