【问题标题】:How to call classes and function in type definition file?如何在类型定义文件中调用类和函数?
【发布时间】:2017-05-18 18:25:33
【问题描述】:

我是 typescript 的新手,正在尝试使用 typescript 2.2 的类型定义文件,(我正在使用 typescriptlang.org 但无法得到答案)我有以下类型定义文件

export as namespace mydefinition;

export namespace mynamespace {

  interface Myinterface {
     width: number;
     height: number;
  } 

  class MyClass {
    constructor(attributes?: any);
    addCell(cell: Cell): this; 
 }
}

我正在使用以下行导入文件并且它成功

import { mydefinition } from 'definitionfile';

如何调用这个定义文件的类和函数?

【问题讨论】:

  • 你的标题有点误导。您特别不能从定义文件中调用类和函数。它用于定义您的类 &c,而不是调用它们。你最后的问题似乎是一个更好的标题。

标签: javascript angularjs typescript type-definition


【解决方案1】:

我觉得不错。您只是缺少 myclass 的初始化。

import { mydefinition } from './definitionfile';
export class classA implements mydefinition.Myinterface {
    width: number;
    height: number;

    constructor() {
        var test = new mydefinition.MyClass();
        test.addCell("attr");

    }
}

【讨论】:

  • 我得到 ERROR Module not found: Error: Can't resolve 'definitionfile' When calling class
  • 对不起,我忘了 ./ 在定义文件前面。我更改了答案中的代码。所以现在它应该可以工作了。
猜你喜欢
  • 2021-04-15
  • 2020-12-27
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多