Typescript interfaces exported vs not

Typescript interfaces exported vs not

miken32miken32 Posts: 7Questions: 2Answers: 0

Looking at the source code I see the DataTableDom interface, and it looks like it should be exported. But it's not exported in the built file. Is this expected? Is there a reason not to export everything?

Answers

  • allanallan Posts: 65,856Questions: 1Answers: 10,956 Site admin

    Hi,

    The primarily reason for there being only selected interfaces exported is future backwards compatibility. I don't want to export everything and then be bound by the implicit API contract that makes (my naming consistency for interfaces is fairly poor for example - I'd like to find some time to sort it out at some point, and have the option of cleaning them up in future).

    Any function that uses DataTableDom should allow implicit types (unless you are creating a named function and assigning it?). Can you show me a use case for it perhaps please?

    Thanks,
    Allan

  • miken32miken32 Posts: 7Questions: 2Answers: 0

    Ok I left an answer but after editing it three times it vanished lol Trying again...

    We use a custom object to set up the DataTable that will be drawn on each page. I like to have IDE autocompletion so I made an interface for it, but wasn't able to specify the this context for the callback functions. I ended up making my own DataTableDom since it's only three lines, but there are other more complicated interfaces like ConfigColumnDefs that would be helpful to have available. e.g.:

    import {Api, ColumnsConfig, Dom, Settings} from "datatables.net";
    import {ButtonConfig} from "datatables.net-buttons-bs5";
    
    interface DataTableDom extends Dom<HTMLTableElement> {
            api(): Api;
    }
    
    export interface DataTablesSetup {
            buttons: ButtonConfig[];
            columnDefs: object[]; // not properly typed
            columns: ColumnsConfig[];
            countData: string;
            drawCallback?: (this: DataTableDom, settings: Settings) => void,
            ...
    }
    
  • miken32miken32 Posts: 7Questions: 2Answers: 0

    I don't know if this is a common use pattern or not? I've only been using TS for about a year now and my background is in PHP and JavaScript where we don't have this disconnect between the source and the end product. I may be abusing TS to fit into my notions of how a language "should" behave.

  • allanallan Posts: 65,856Questions: 1Answers: 10,956 Site admin

    Ok I left an answer but after editing it three times it vanished

    The spam queue caught it. I've marked your account so that shouldn't happen again.

    Yes I see. I wonder if extending the Options type that you can get from datatables.net would do the job here:

    export interface DataTablesSetup extends Options {
        countData?: string;
    }
    

    Then you get all the types and options automatically inherited:

    let setup: DataTablesSetup = {
        drawCallback: function (settings) {
            this; // is `DataTableDom`.
        },
        countData: '1'
    }
    

    Allan

  • miken32miken32 Posts: 7Questions: 2Answers: 0

    Well it currently only has a subset of the options, but yeah maybe it's easier to just account for the additional options and do it that way.

  • allanallan Posts: 65,856Questions: 1Answers: 10,956 Site admin

    TypeScript has got you covered there as well. You don't need to just extend from Options, but rather you can use Pick or Omit to selectively include or exclude properties from another interface.

    So if you want to present only specific properties from Options you could use Pick and extend from that, while retaining all the typing of the DataTables exported Options interface.

    Allan

Sign In or Register to comment.