- Notifications
You must be signed in to change notification settings - Fork 59
Generate proper types for a Record #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
jordanhunt22 left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nyoung697 for this contribution!
I'm going to update the convex package to export the RecordKeyValidatorJSON and RecordValueValidatorJSON objects. That way, we can do this correctly. Can you please ping me in 1-2 weeks if I don't circle back?
| return 'any' | ||
| case 'record': { | ||
| const keyType = generateRecordKeyType(argsJson.keys) | ||
| const valueType = generateArgsType(argsJson.values.fieldType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this value type is also restricted because it can't be optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Jordan! What do you mean by this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified it to check if the field is optional and it will add undefined. Is this the correct way of handling this?
case 'record': { const keyType = generateRecordKeyType(argsJson.keys); const valueType = generateArgsType(argsJson.values.fieldType); const isOptional = argsJson.values.optional ? " | undefined" : ""; return `Record<${keyType}, ${valueType}${isOptional}>`; } Exports `RecordKeyValidatorJSON` and `RecordValueValidatorJSON`, so we can properly support [this](get-convex/convex-helpers#447) change. GitOrigin-RevId: 933eadedd710b068ba5106578ff0701bee7bd5e1
Exports `RecordKeyValidatorJSON` and `RecordValueValidatorJSON`, so we can properly support [this](get-convex/convex-helpers#447) change. GitOrigin-RevId: 933eadedd710b068ba5106578ff0701bee7bd5e1
jordanhunt22 left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I exported the types from convex-js to make this a bit easier. The pr I made is here
| /** | ||
| * Generates a TypeScript-compatible key type for a record. | ||
| * | ||
| * The keys should actually be RecordKeyValidatorJSON, but this isn't exported from validators.ts in convex. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please update this to use the types I exported here? Please also do the same with the values.
| case "record": { | ||
| const keyType = generateRecordKeyType(argsJson.keys); | ||
| const valueType = generateArgsType(argsJson.values.fieldType); | ||
| const isOptional = argsJson.values.optional ? " | undefined" : ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
values should never be optional
ianmacartney left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see jordan's feedback
| I added the |
jordanhunt22 left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look good. Thanks for contributing! Can you confirm that we can modify, copy, and redistribute this contribution by adding a thumbs up to this message?
Once that's done, we can go ahead and get this merged in.
| It's live in |
Generate the correct types for a record, instead of just returning any.
The type
RecordKeyValidatorJSONisn't exported from the convex package, but if it were to be exported, thegenerateRecordKeyTypefunction can be updated.