【问题标题】:How to call the method from javascript file inside from the typescript如何从打字稿中调用javascript文件中的方法
【发布时间】:2020-02-25 13:07:17
【问题描述】:

这是我的 javascript 类:TestClass.js

/**
 * http://usejsdoc.org/
 */
export class TestClass{
    constructor(){

    }

    echo(){
        alert("Echo")
    }
}

这是我的定义文件:test.d.ts

/**
 * http://usejsdoc.org/
 */

declare class TestClass{
     public echo():void;
}

这是我的 ts 主文件:entry.ts

import * as go from "gojs";
import * as test from "./TestClass"    

window.addEventListener( "load", delay );

function delay() {

    var testClass=new test.TestClass()
    TestClass.echo();

}

当我尝试使用 npm run build 构建它时

ERROR in /home/voffka/Documents/projects/typescript-tutorial/r-diagram1/r-diagram/src/entry.ts(9,15)
TS2339: Property 'echo' does not exist on type 'typeof TestClass'.

如何解决这个问题?

【问题讨论】:

  • 您的意思是打电话给testClass.echo()吗?否则,您将声明一个变量,然后从不使用它,并尝试静态调用该方法。
  • 是的,我需要使用非静态方法

标签: javascript typescript type-definition


【解决方案1】:

尝试这样做:

import * as go from "gojs";
import * as test from "./TestClass"    

window.addEventListener( "load", delay );

function delay() {

    var testClass=new test()
    test.echo();

}

【讨论】:

    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多