【问题标题】:Using an import statement breaks other class references?使用 import 语句会破坏其他类引用?
【发布时间】:2016-12-31 20:19:05
【问题描述】:

作为my previous question 关于使用bowserDefinitelyTyped 定义文件的后续行动,我按照该问题的答案实施了import 语句,并取得了进一步进展。然而,现在 TypeScript 编译器抱怨在所有 Bowser 导入废话之前编译得非常好。

假设我有 MyBowserClass.ts:

import bowser = require('bowser');

namespace MyNamespace {
    export class MyBowserClass {
        constructor() {
            var isIos = (typeof bowser.ios === 'undefined') ? false : bowser.ios;
            alert(isIos);

            var myInstance = new MyNamespaceTwo.MyOtherClass(); // typescript compiler complains: Property 'MyOtherClass' does not exist on type 'typeof MyNamespaceTwo'.
        }
    }
}

然后我有 MyOtherClass.ts:

namespace MyNamespaceTwo {
    export class MyOtherClass {
        constructor() {
            alert('otherclass ctor');
        }
    }
}

编译器在这里给我一个错误:

var myInstance = new MyNamespaceTwo.MyOtherClass();

Property 'MyOtherClass' does not exist on type 'typeof MyNamespaceTwo'.

所以我想这可能意味着我还需要导入MyOtherClass

我通过更新我的两个文件来实现这一点:

import bowser = require('bowser');
import otherClass = require('MyOtherClass'); // NEW IMPORT

namespace MyNamespace {
    export class MyBowserClass {
        constructor() {
            var isIos = (typeof bowser.ios === 'undefined') ? false : bowser.ios;
            alert(isIos);

            var myInstance = new otherClass.MyNamespaceTwo.MyOtherClass(); // changed this to prefix with 'otherClass'
        }
    }
}

export namespace MyNamespaceTwo { // made this EXPORT
    export class MyOtherClass {
        constructor() {
            alert('otherclass ctor');
        }
    }
}

这似乎完全是混乱/疯狂。我在这里想念什么?为什么bowser 定义文件无论如何都应该是一个模块(当它包含一个全局/基本上静态的方法名称时??)任何指导/帮助将不胜感激。

【问题讨论】:

  • 您能否更新问题,而不是试图描述您所做的更改,请在更改后发布代码?它会让它更容易理解。
  • @NitzanTomer 你去。希望有帮助。

标签: typescript typescript-typings definitelytyped


【解决方案1】:

您似乎正在将文件从全局声明文件更改为模块声明文件。

  • 全局声明文件使在其中声明的类型可以在整个项目中访问,而无需导入任何内容。全局声明文件永远不能从另一个文件导入。它们也从不导出,因为其中声明的类型随处可用。

    例如。使用redux 的项目可以声明一个SpecialAction,然后可以在项目的任何地方使用:

    // index.d.ts
    interface SpecialAction {
      type: string
      isSpecial: boolean
    }
    
    // src/app.ts
    let theAction: SpecialAction = {
      type: 'SPECIAL',
      isSpecial: true
    }
    
  • 模块声明文件将特定类型导出到模块,因此可以在项目的其他地方导入导出。只要在声明文件中导入或导出,它就会成为模块声明文件。

    // index.d.ts
    import { Action } from 'redux'
    export interface SpecialAction extends Action {
      isSpecial: boolean
    }
    
    // src/app.ts
    import { SpecialAction } from '../index'
    let theAction: SpecialAction = {
      type: 'SPECIAL',
      isSpecial: true
    }
    

我希望这会有所帮助吗? ¯\_(ツ)_/¯

【讨论】:

  • 我想这有助于回答我的问题:As soon as you import or export in a declaration file, it becomes a module declaration file. 我的import bowser = require('bowser'); 行正在使我的文件成为“模块声明”。这是我没想到的。我对这一切感到很愚蠢,猜猜我该读一读了。
  • 这就是正确的意思?在我最终得到它之前,我自己已经失去了无数个小时。我很高兴能帮上忙!
  • 所以也感谢 Pelle,但我该如何解决我想在我的导出类中访问 moment(这是为了支持 @types)?将import moment = require('moment'); 添加到文件顶部时,我遇到了同样的问题。
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
相关资源
最近更新 更多