5

I wish to add definition for a plugin I love and I wish to know how I can define the type to allow this without use the type any.

I will use the property like this:

    views: {
        'list.view1': {
          dropPagingCap: 20,
          list_infiniteScroll: true,
          list_selectable: 'multi'
        },
        'list.view2': {
          dataSource: function(options, callback){ ... },
          dropPagingCap: 30,
          list_selectable: true
        }
    }

I have tried this, but the library except an object {}, not an array []

interface IFuelUxRepeaterViews {
    [index: string]: IFuelUxRepeaterParametersBase | IFuelUxRepeaterListParameter | IFuelUxRepeaterTumbnailParameter | IFuelUxRepeaterAllParameter;
}

I don't know how to name this JavaScript type of declaration.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Benoit
  • 1,109
  • 14
  • 29
  • It's a dictionary or map. See http://stackoverflow.com/q/15877362/746347, http://stackoverflow.com/q/30019542/746347. Your definition should work. – mixel Aug 18 '16 at 06:10
  • 1
    Define views property as `views: IFuelUxRepeaterViews`. – mixel Aug 18 '16 at 06:12

2 Answers2

14

From your code example it seems like your views can be represented using the following interface:

interface IFuelUxRepeaterViews {
    [index: string]: {
        dropPagingCap: number;
        dataSource?: (options: any, cb: () => void) => void;
        list_infiniteScroll?: boolean;
        list_selectable?: string | boolean;   
    }
}
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • tanks, it's like what i have used, the isue was with multiple possible type, i have replaced it with the one with all params. – Benoit Aug 18 '16 at 17:24
  • Could be also generic `interface IFuelUxRepeaterViews { [index: string] : T } – ymz Jan 06 '19 at 08:49
1
interface IFuelUxRepeaterParametersBase {
    dropPagingCap: number,
    list_selectable: boolean | string
}

interface IFuelUxRepeaterTumbnailParameter extends IFuelUxRepeaterParametersBase {
    list_infiniteScroll: boolean,
}

interface IFuelUxRepeaterListParameter extends IFuelUxRepeaterParametersBase {
    dataSource: (x: any, y: any) => any,
}

interface IFuelUxRepeaterViews {
    [index: string]: IFuelUxRepeaterParametersBase | IFuelUxRepeaterListParameter | IFuelUxRepeaterTumbnailParameter ;
}


let views: IFuelUxRepeaterViews = {
    'list.view1': {
        dropPagingCap: 20,
        list_infiniteScroll: true,
        list_selectable: 'multi'
    },
    'list.view2': {
        dataSource: (options, callback) => { },
        dropPagingCap: 30,
        list_selectable: true
    }
}
Vadim Levkovsky
  • 336
  • 2
  • 14