David Tejada - Hi. I have the following functio...
Hi. I have the following function which I have simplified for the example. The type Storage is inferred from the zod object zStorage. I would like to know why the type assertion is necessary here. Shouldn't the return type be inferred automatically when using parse?
9 Replies
I assume you want something like this?
the issue right now is that with your current setup key could be anything, including a non existing key
Why do you say it could be anything? It can only be one of the keys in Storage as it is typed
sorry kinda misread the intent of your code, didn't think about Storage being an external interface
anyway, it can be anything since T extends keyof Storage but is not equal to it
okay disregard once again lmfao
The type Storage is inferred from the zod object zStorage.sorry had very little sleep anyway point still stands, altho in this case just do unless I'm once again missing something 😅
No problem😀 I was about to try that approach
This doesn't work. The problem is the return type of something like
await retrieve("test")
is not string
but string | number
. The way I have done it works for me but I don't know if there is a cleaner way.oohh wait sorry yes I'm starting to get around to what you were trying to do
I wanted to know if there is a way to avoid the type assertion
Solution
yea sorry you would indeed need a generic for that
Yeah, makes sense. Thanks