简体   繁体   English

打字稿:具有不同名称的导出接口

[英]Typescript: export interface with different name

I need to import interface, add property to id and export the new interface with the same name as original one.我需要导入接口,将属性添加到 id 并导出与原始接口同名的新接口。 Then in all places where this interface is used I can only change import location.然后在所有使用此接口的地方我只能更改导入位置。

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

export declare type Routes = Route2[];

And then I'd like to (although it is not possible)然后我想(虽然这是不可能的)

export Route2 as Route;

Import the original Route under a different name and export the new one as Route 以不同的名称导入原始Route ,并将新Route导出为Route

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

You can do it with export type which allows you to provide an alias for the interface.您可以使用导出类型来执行此操作,该类型允许您为接口提供别名。

General example (Alias being the new name and SomeInterface the original):一般示例(别名是新名称,SomeInterface 是原始名称):

export type Alias = SomeInterface;

Your specific scenario:您的具体场景:

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

export type Route = Route2;

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

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