Why keyof Record wont actually give the keys of the record ?
I have this exemple :
So based on what i describe with type i would have hope that descriptionsKeys would get auto completion base on the description the user give but it doesn't The type of descriptionKeys is Record<string, string> and not Record<"test" , string> and if i had more keys to desc i would hope to get these keys to in the Record. Any idea of where i m wrong ?
12 Replies
It's because of
type C = Record<string,object>
. When you're using Record<string, object>
, the key is essentially being read as "any string", and it doesn't know how to evaluate it beyond that, since it doesn't know the shape of the object in your validate
function. Someone else might have a more sophisticated solution, but here's an easy way around it:
You define the object explicitly outside of test
, which is that objectToHaveKeysFrom
, then you extract those keys into type C
, and pass the object into test
.It’s a good start i think but than the user is unable to declare other think directly in description 😅
At the end i want to genrate a zod schema and parse it
what do you mean with generate a zod schema?
The idea is in the description key the user describe an objet not usine zod notation. And based on that i create a dynamic zod schema to check the value before passing it to the function
okay so maybe this helps you
wrapping the definition in a function allows for inference using generics
But then i can’t do the parsing before the validate function
where would you do the parsing in your example?
The idea is there i want to inject the authenticate function of the user when someone tries to login a bit like nextjs does but before that based on the credentials they declare i wanna parse it with zod. The code exemple in message before were just test for this
And what is the relation between this class implementation and the snippet you provided in the question?
I don't see the problem with the implementation above
I must admit that all snipet are out dated but have the same goal as here i just worked on it and made it a class so you can have more info but there is the same issue of i want authenticate to get the autocomplete of the keys the credentials the user declare.but when you go for creating a class and declare the authenticate function in the constructor you aren't getting the autocomplete
I wasn't able to get your implementation fully working, I just don't know how to generate the schema based on the credentials. I tried to do it a different way of passing the schema and the credentials together. I'm not sure if this is what your are looking for but here it is
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.