【问题标题】:Cannot resolve interface defined in the same module but in different file无法解析在同一模块中但在不同文件中定义的接口
【发布时间】:2016-03-13 04:36:25
【问题描述】:

代码下方

字段/base.ts

/// <reference path="../_all.ts" />

module SomeApp.fields {
  export interface IField {}
  export class Field implements IFields {}
}

在字段/CharField.ts

/// <reference path="../_all.ts" />

module SomeApp.fields {

  import IField = SomeApp.fields.IField; 

  export interface ICharField extends IField {}
  export class Field implements IFields {}
}

在 _all.ts 中:

/// <reference path="fields/base.ts" />
/// <reference path="fields/CharField.ts" />

但这不起作用,我从CharField.ts 文件中得到了这个错误:

错误:(7, 39) TS2305: 模块“SomeApp.fields”没有导出成员 'IField'。

那么我的代码有什么问题?

【问题讨论】:

    标签: module namespaces typescript typescript1.7


    【解决方案1】:

    你的问题提出了几个问题:

    1. 你是如何编译这个的?
    2. Wy 是 _all.ts 一个 .ts 文件而不是一个 .d.ts 文件吗?
    3. 为什么要导入 IField(取决于点“1”)?

    这是一个工作示例:

    .
    +-- tsconfig.json
    +-- references.d.ts
    +--  src
    |   +-- myInterface.ts
    |   +-- myClass.ts
    

    tsconfig.json:

    {
        "compilerOptions": {
            "module": "commonjs",
            "noImplicitAny": true,
            "removeComments": true,
            "preserveConstEnums": true,
            "out": "result.js",
            "sourceMap": true
        },
        "files": [
            "src/myClass.ts"
        ]
    }
    

    references.d.ts:

    /// <reference path="src/myInterface.ts" />
    /// <reference path="src/myClass.ts" />
    

    myInterface.ts:

    module MyNameSpace {
    
        export interface IMyInterface {
            // ...
        }
    }
    

    myClass.ts:

    /// <reference path="../references.d.ts" />
    
    module MyNameSpace {
        export class MyClass implements IMyInterface {
            // ...
        }
    }
    

    上面的示例有效,您的示例无效的确切原因可能是上面提到的 1、2 或 3 点中的任何一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2021-08-01
      • 1970-01-01
      相关资源
      最近更新 更多