简体   繁体   中英

TypeScript destructuring alias import?

I use namespaces/modules for my application. We have a large application broken into several smaller "modules". They are all compiled together, so we have all references at all times. If i need to access module xy from module z I can simply by using xy . Howevever, I don't want to keep referencing x . Is there anyway I can destructure x into aliased names? For example (this doesn't work, hence my question):

import {y,a,b} = x;

instead of:

import y = x.y;
import a = x.a;
import b = x.b;

Is there something similar I can do to simplify my import aliasing of internal modules?

Is there something similar I can do to simplify my import aliasing of internal module

If the members of the module you are importing are not modules (or a type) import will not work. Use var instead:

module x{
 // stuff
}
var {y,a,b} = x;

I don't believe this exists at this time, however I just opened a proposal on the TypeScript repo asking for type destructuring from a namespace.

https://github.com/Microsoft/TypeScript/issues/13816 https://github.com/Microsoft/TypeScript/issues/13135

Please go comment on these issues to get this in the language!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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