【问题标题】:Typescript errors when referencing files with similar module names引用具有相似模块名称的文件时出现打字稿错误
【发布时间】:2016-07-14 23:23:02
【问题描述】:

我有一个这样的打字稿类;

module my.services {
   export class MyService {
      ...
   }
}

还有一个这样的;

module com.my.component {

   import MyService = my.services.MyService;

   export class MyComponent {
      ...
   }
}

但在第二堂课中,我收到一个 Typescript 错误提示

Module 'com.my' has no exported member 'services'

在这种情况下引用 MyService 的正确方法是什么?

【问题讨论】:

    标签: typescript typescript1.7


    【解决方案1】:

    如果我们在此处查看规范中关于“A.B.C”之类的命名空间的说明:namespace declarations,很容易看出为什么会出现错误。您的“com.my.component”命名空间声明有效:

    namespace com
    {
        export namespace my
        {
            export namespace component 
            {
                import MyService = my.services.MyService;
    
                export class MyComponent extends MyService
                {
    
                }
            }
        }
    }
    

    因此,任何引用“my”声明中以“my....”开头的任何内容的尝试都将尝试在当前“com.my”命名空间中搜索它。

    要解决此问题,您可以将导入移到“我的”命名空间声明之外:

    import MyService = my.services.MyService;
    
    namespace com
    {
        export namespace my
        {
            export namespace component 
            {
                export class MyComponent extends MyService
                {
    
                }
            }
        }
    }
    

    或者更短的版本:

    import MyService = my.services.MyService;
    
    module com.my.component 
    {
        export class MyComponent extends MyService
        {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多