简体   繁体   English

打字稿:将本地变量名称分配给导入的嵌套名称空间

[英]Typescript: Assigning a local variable name to an imported nested namespace

I'm declaring a JAVA library in a separate file to build out the various classes and namespaces used within it so when I reference this from other files that utilize the JAVA library there will be associated comments and typings. 我在一个单独的文件中声明一个JAVA库,以构建其中使用的各种类和名称空间,因此,当我从其他使用JAVA库的文件中引用该库时,将有相关的注释和类型。 (This is for a NativeScript plugin btw which is why I'm mixing JAVA and JS). (这是针对NativeScript插件btw的,这就是为什么我要混合JAVA和JS)。

javalib.ts javalib.ts

export declare namespace org {
  export namespace fizz {
    export namespace buzz {
      export namespace libA {
        export class libAClassA { }
        export class libAClassB { }
      }

      export namespace libB {
        export class libBClassA { }
        export class libBClassB { }
      }
    }
  }
}

It looks wild, and I could probably shorten it to just export declare namespace org.fizz.buzz { ... } but I'm outling it for now. 它看起来很疯狂,我可能可以将其缩短为仅export declare namespace org.fizz.buzz { ... }但我暂时不介绍它。

In another file that will be utilizing some of these classes (ie, libAClassA or libBClassB) I'd like to of course import these namespaces to have the typings and references follow. 在另一个将利用其中一些类的文件(即libAClassA或libBClassB)中,我当然想导入这些名称空间,以遵循其类型和引用。 I've so far been able to get it working like so: 到目前为止,我已经能够使它像这样工作:

app.ts app.ts

import * as pkg from './javalib';

let aliceClass = new pkg.org.fizz.buzz.libA.libAClassA();
let bobClass = new pkg.org.fizz.buzz.libA.libBClassB();

While this works, it's not ideal as having to write out the entirety of the namespace to reference this definition can become quite bothersome all for the added benefit of code visability while developing. 尽管这可行,但由于必须在开发过程中写出整个命名空间以引用此定义,这会变得很麻烦,这对于代码可视性的其他好处而言,并不是一个理想的选择。 I could instead just do a blanket declare var org: any and be done with it, but in an effort to better my future dev on this project as well as others I'd like to utilize this and set this up. 相反,我可以大胆地declare var org: any可以完成declare var org: any工作,但是为了更好地促进我将来在该项目以及其他我想利用它进行设置的开发工作。

Is there any way to cast this namespace reference to a local var, similar to how in JAVA you can just do import org.fizz.buzz; 有什么方法可以将此命名空间引用转换为本地var,类似于在JAVA中可以import org.fizz.buzz; and suddenly have references and access to everything within that namespace? 突然拥有对该命名空间中所有内容的引用和访问权?

ideal-app.ts 理想-app.ts

let libA = org.fizz.buzz.libA;
let libB = org.fizz.buzz.libB; 

let aliceClass = new libA.libAClassA();
let bobClass = new libB.libBClassB();

It's possible to do as you have in the ideal-app.ts but to make sure it's compatible with Webpack and Snapshot builds, initialise them inside a function call. 可以像在Ideal-app.ts中一样进行操作,但要确保它与Webpack和Snapshot构建兼容,请在函数调用中对其进行初始化。 Something like this, 像这样

let libA, libB;

function initializePackages() {
   if (!libA) {
     libA = org.fizz.buzz.libA;
     libB = org.fizz.buzz.libB; 
   }
} 

// Call it before accessing the variables
initializePackages();
let aliceClass = new libA.libAClassA();
let bobClass = new libB.libBClassB();

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

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