简体   繁体   English

如何缩短打字稿中的界面名称?

[英]How can i shorten name of interface in typescript?

suppose i imported a namespace in typescript , lets call it ui , and it has one more namespace called dates and then there is one interface called IDateFormat , but only ui is exported 假设我在typescript中导入了一个名称空间,让我们称之为ui ,它还有一个名为dates名称空间,然后有一个名为IDateFormat接口,但只导出了ui

so if I have to consume the interface i have to do 所以,如果我必须消耗我必须做的接口

import {ui} from '..pathToFile'

//to use interface i have to do
const format : ui.dates.IDateFormat

so basically i have to write ui.dates.IDateFormat every time 所以基本上我每次都要写ui.dates.IDateFormat

can i shorten it by assigning to a variable. 我可以通过分配变量来缩短它。

like 喜欢

const intrface : < what's the type> = ui.dates.IDateFormat; // will this work

and use it 并使用它

but i was thinking what will be the type of such variable and is there any other way to do it? 但我在想什么是这种变量的类型,还有其他方法吗?

You can just export it from your own module like this: 你可以从你自己的模块中导出它,如下所示:

export type shortType = ui.dates.IDateFormat;
export interface shortInterface extends ui.dates.IDateFormat {

}

What you are looking for is a type alias . 您正在寻找的是类型别名 It doesn't use a const declaration: 它不使用const声明:

type Intrface = ui.dates.IDateFormat;

const format : Intrface = …;

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

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