S
SolidJS•6mo ago
Mr Void

setState variable as path doesn't work

The following works
setState("items", "collections", (collections: ItemCollection[]) =>
[...collections].filter((collection) =>
collection.id !== collection_id
)
)
setState("items", "collections", (collections: ItemCollection[]) =>
[...collections].filter((collection) =>
collection.id !== collection_id
)
)
However, this doesn't work, why? path is always one of the possible keys inside items object.
setState("items", path, (collections: ItemCollection[]) =>
[...collections].filter((collection) =>
collection.id !== collection_id
)
)
setState("items", path, (collections: ItemCollection[]) =>
[...collections].filter((collection) =>
collection.id !== collection_id
)
)
ts error:
Argument of type 'string' is not assignable to parameter of type 'Part<ItemStore, keyof ItemStore>'. ts(2345)
Argument of type 'string' is not assignable to parameter of type 'Part<ItemStore, keyof ItemStore>'. ts(2345)
2 Replies
Mr Void
Mr Void•6mo ago
having values as types in argument list resolves the issue 🤔
path: "collections" | "sharedCollections"
path: "collections" | "sharedCollections"
bigmistqke
bigmistqke•6mo ago
it's the same type of type-error like
const obj = {
key: 'property'
}

let someKey = 'key';
obj[someKey] // type-error
const obj = {
key: 'property'
}

let someKey = 'key';
obj[someKey] // type-error
because 'string' can't be used to index type '{ key: string; }' while
let someKey = 'key' as const;
obj[someKey] // no type-error
let someKey = 'key' as const;
obj[someKey] // no type-error