TypeScript Types for editor are sparse.

TypeScript Types for editor are sparse.

kbinlkbinl Posts: 7Questions: 3Answers: 0

I have a license to the Editor component, and I am building some field type plugins and would like to build them in TypeScript, but it looks like many of the types are not exported from @datatables.net/editor

Looking through the package folder, I see the types folder and a bunch of types, but most of them are not exported in types.d.ts so I can't use them.

For example, I want to use the Field class as a type. I see it in Field.d.ts

I try to import the type:

import Editor, { Field } from "@datatables.net/editor";

I get this TypeScript error:

Module '"@datatables.net/editor"' has no exported member 'Field'

Would it be possible to get all of the types exported so they can be used? Or maybe there's a way to use them that I am not seeing.

Answers

  • allanallan Posts: 65,162Questions: 1Answers: 10,796 Site admin

    Hi,

    Interesting one, as one of the points of feedback from the survey was that Editor didn't have complete typing. I could really understand that since all the options and API are fully typed. However, I hadn't considered the point you make about using it to create a field type plugin.

    This is how it is done internally (this is the hidden field type):

    import { triggerChange } from './shared';
    import { IOptions } from '../../field/defaults';
    
    export interface IHiddenOptions extends IOptions {
        /** Create a hidden field */
        type: 'hidden';
    
        /** Set HTML attributes on the input element. */
        attr?: { [name: string]: any };
    
        /** @ignore */
        value?: any;
    }
    
    /** Internal properties used on the configuration object for the field */
    export interface IHiddenConf extends IHiddenOptions {
        /** @ignore */
        _input?: JQuery<HTMLElement>;
    
        /** @ignore */
        _val?: any;
    }
    
    export default {
        create(conf: IHiddenConf) {
            conf._input = $('<input/>');
            conf._val = conf.value;
    
            return null;
        },
    
        get(conf: IHiddenConf) {
            return conf._val;
        },
    
        set(conf: IHiddenConf, val: any) {
            let oldVal = conf._val;
    
            conf._val = val;
            conf._input.val(val);
    
            if (oldVal !== val) {
                triggerChange(conf._input);
            }
        }
    };
    

    I can look at making the IOptions interface external, certainly. Is there any other you need? The Field class is available under Editor.Field from the default import.

    Allan

Sign In or Register to comment.