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.
A collection of key and value pairs is called a dictionary in TypeScript. The dictionary is also referred as a map or a hash. A map can be created by using the type Map and the keyword new. We can store the collection of key value pairs inside a map by creating a map.
To declare and initialize a dictionary in TypeScript, we can create a dynamic object type. const persons: { [id: string]: IPerson } = {}; persons["p1"] = { firstName: "jane", lastName: "smith" }; to set the persons variable to type { [id: string]: IPerson } .
Declaring InterfacesAn interface adds the functionality of strong type checking for your functions, variables, or the class that is implementing the interface.
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;
}
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With