Calling JavaScript directly from TypeScript

前端 未结 4 931
Happy的楠姐
Happy的楠姐 2020-12-06 16:21

I have just downloaded the TypeScript documentation. I have a some JavaScript classes and I would like to create and use these class in a TypeScript test application

相关标签:
4条回答
  • 2020-12-06 16:45

    You just do it. TypeScript won't stop you. You will see warnings in the compiler output but tsc will generate your JS file just fine.

    0 讨论(0)
  • 2020-12-06 16:48

    If you want to stop the errors without doing much else extra work, you can 'declare' the objects from your JS code:

    declare var w; // implicit type here is 'any'
    // (later, anywhere in your file...)
    var x = new w(); // you can do whatever you want with w now without getting errors
    w.x = 4; // etc.
    
    0 讨论(0)
  • 2020-12-06 16:54

    There is a better solution. Just cast the jQuery logonDlg to any like this:

    (<any>logonDlg).kendoWindow(logOnParams);
    

    The code will be a bit different but will work the same.

    1. Without the cast th generated code is like this: logonDlg.kendoWindow(logOnParams);
    2. With the cast will be like this: (logonDlg).kendoWindow(logOnParams);

    Both work OK.

    Regards

    0 讨论(0)
  • 2020-12-06 16:54

    In this particular case you could just include the Kendo UI TypeScript definitions that Telerik provides.

    (I realize this question was asked back when the TS definitions probably didn't exist yet, but wanted to add this detail for people who come across this question in the future.)

    0 讨论(0)
提交回复
热议问题