When I import 'datatables.net-buttons' in a Angular Typescript project it breaks my code.

When I import 'datatables.net-buttons' in a Angular Typescript project it breaks my code.

laucha54321laucha54321 Posts: 3Questions: 1Answers: 0
Error: node_modules/datatables.net-buttons/types/types.d.ts:55:4 - error TS7013: Construct signature, which lacks return-type annotation, implicitly has an 'any' return type.

55    new (dt: Api<any>, settings: boolean | ConfigButtons | Array<string | ButtonConfig>);

This is the error that gets shown.
I use this import syntax:

import 'datatables.net-buttons';

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I've just made the connection between this thread and the GH pull request you sent. The PR makes more sense now!

    However, Typescript should make that inference automatically. What version of Typescript are you using please?

    Allan

  • laucha54321laucha54321 Posts: 3Questions: 1Answers: 0

    I am using version 5.0.4 of Typescript.

  • laucha54321laucha54321 Posts: 3Questions: 1Answers: 0
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Sorry I wasn't able to reply this afternoon (no bump required, sometimes I just can't reply to threads within two hours - indeed I'm out tomorrow for a funeral so probably won't be on).

    The TypeScript documentation says:

    Constructors can’t have return type annotations - the class instance type is always what’s returned

    Perhaps I'm missing something, but new is a constructor so adding a return type isn't needed and indeed would be wrong. any would certainly not be right since it isn't returning an any. If it needs to be typed it would be returning a DataTable.Buttons instance.

    I'll try testing it as soon as I can. Probably on Wednesday.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    While looking into this I realised that the Buttons object was being attached to the wrong DataTables host object. I've corrected that now and something like this will work in Typescript happily now:

    import DataTable from "datatables.net";
    import "datatables.net-buttons";
    
    let table = new DataTable('#test');
    
    new DataTable.Buttons( table, {
        buttons: [
            {
                text: 'Button 1',
                action: function ( e, dt, node, conf ) {
                    console.log( 'Button 1 clicked on' );
                }
            },
            {
                text: 'Button 2',
                action: function ( e, dt, node, conf ) {
                    console.log( 'Button 2 clicked on' );
                }
            }
        ]
    } );
    

    Before it gave an error about Buttons not being defined on the DataTable object.

    I don't think that will fix the error you are seeing though, as I still don't know what is causing that. If you can use codesandbox.io or something to create an example showing the issue that would be really useful.

    Allan

Sign In or Register to comment.