I encountered an issue while trying to define an interface for the structure outlined below:
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 })
}
The errors I'm encountering are as follows:
- In line
const fn = functionLookup[type]
: Element implicitly has an 'any' type because expression of type string cannot be used to index type 'Lookup'. No index signature with a parameter of type 'string' was found on type 'Lookup'.
- I'm uncertain about why this is occurring, as I believed that the Dictionary I defined in Lookup should be interpreted as a string. When I change Dictionary to
, the error disappears, but I want to specify the arguments allowed.[x: string]: ({mode, key, defaultKey}:{mode: string, key: string, defaultKey: string}) => ReturnType
- In line
return fn({ ...params })
: Expected 3 arguments, but only received 1
- This is confusing to me, as the function appears to expect only one object as an argument
{mode, key, defaultKey}
. Is it actually expecting the ReturnType function instead?
Any assistance would be greatly appreciated. Thank you in advance :)