I'v faced a problem trying to define an interface for the following structure:
interface JSONRecord {
[propName: string]: any;
}
type ReturnType = (id: string|number, field: string, record: JSONRecord) => string
export const formatDictionary = ({
mode = "render", key = "originalValue",
defaultKey = "originalValue"
}):ReturnType => (id, field, record) => {
...
}
interface Lookup {
Dictionary: ({mode, key, defaultKey}:{mode: string, key: string, defaultKey: string}) => ReturnType,
...
}
export const functionLookup:Lookup = {
Dictionary: formatDictionary,
...
}
export const formatField = (params:JSONRecord):string|ReturnType => {
const type:string = params.type
if (type === undefined) { return identity }
const fn = functionLookup[type]
if (fn === undefined) { return identity }
return fn({ ...params })
}
I'm getting the following errors:
- In line
const fn = functionLookup[type]: Element implicitly has an 'any' type becasue expression of type string can't be used to index type 'Lookup'. No index signature with parameter of type 'string' was found on type 'Lookup'.
- I'm not really sure why is this happening, i thought that the Dictionary i defined in Lookup is supposed to be interpreted as a string. When i change Dictionary to
[x: string]: ({mode, key, defaultKey}:{mode: string, key: string, defaultKey: string}) => ReturnTypethe error disappears, but i want to be specific with the arguments that can be passed.
- In line
return fn({ ...params }): Expected 3 arguments, but got 1
- I can't really wrap my head around this, the function is clearly expecting only 1 object as an argument
{mode, key, defaultKey}or is it expecting the ReturnType function there?
I would appreciate any help. Thanks a lot in advance :)
identityis not defined variable