引用具有相似模块名称的文件时出现打字稿错误 [英] Typescript errors when referencing files with similar module names

查看:94
本文介绍了引用具有相似模块名称的文件时出现打字稿错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one typescript class like this;

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

另一个这样;

module com.my.component {

   import MyService = my.services.MyService;

   export class MyComponent {
      ...
   }
}

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

But in the 2nd class I get a Typescript error saying

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

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

What is the correct way to reference MyService in this case?

推荐答案

如果我们在这里看看有关"ABC"之类的名称空间的规范说明:

If we will take a look at what specification says about namespaces like 'A.B.C' here: namespace declarations it is easy to see why you get the error. Your 'com.my.component' namespace declaration is effectively:

namespace com
{
    export namespace my
    {
        export namespace component 
        {
            import MyService = my.services.MyService;

            export class MyComponent extends MyService
            {

            }
        }
    }
}

因此,任何尝试引用以"my ...."开头的"my"声明中的内容的尝试都会尝试在当前"com.my"命名空间中进行搜索.

And therefore any attempt to reference anything inside 'my' declaration starting with 'my....' will attempt to search for it inside current 'com.my' namespace.

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

To fix this you can move import outside 'my' namespace declaration:

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
    {

    }
}

这篇关于引用具有相似模块名称的文件时出现打字稿错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆