Issue
Hi I ave a code like this:
export type Options = Record<string, string>
export type CheckboxValue<T extends Options> = Partial<
Record<keyof T, boolean>
>
export type Checkbox<T extends Options> = {
value: CheckboxValue<T>
}
export function Checkbox<T extends Options>({
value,
}: Checkbox<T>) {
return value
}
export type FilterProps<T extends Options> = {
options: T
value: string[]
}
export function Filter<T extends Options>(
props: FilterProps<T>,
) {
const { value = [] } = props
const mappedValue = (() => {
const contextValue: string[] = value
const result = {}
for (const item of contextValue) {
/*
* Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
* No index signature with a parameter of type 'string' was found on type '{}'.(7053)
*/
result[item] = true
}
return result
})()
return Checkbox({value: mappedValue})
}
Why does this happen and how to fix it?
Solution
You could use the index signature:
const result: { [key: string]: boolean } = {}
As to why it happens if I'd explain it most likely it wouldn't be accurate. Maybe TS docs could help you: https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures
Update: This old SO thread could also be useful: How do I prevent the error "Index signature of object type implicitly has an 'any' type" when compiling typescript with noImplicitAny flag enabled?
Answered By - Kari F.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.