-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fix IdSchema and PathSchema types #4196
base: main
Are you sure you want to change the base?
Conversation
@solimant It looks like your type updates broke the building of |
@solimant Have you figured out how to fix the build issues? |
Hi @heath-freenome - I was actually looking into it today. It seems to have to do with how |
@heath-freenome - apologies for the delay. Got this fixed on my local. Will wait to see if tests pass. |
@heath-freenome - could get the tests running when you get a chance? Thank you 🙏🏼 |
@heath-freenome - looks like CI failed again. I had run EDIT: |
@heath-freenome - looks like tests passed this time. The PR branch is out of date, though, which I'm happy to rebase. Let me know how you'd like me to proceed. |
@heath-freenome - are you good merging this? What are next steps? |
customValidate?: CustomValidator<T>, | ||
customValidate?: CustomValidator, |
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.
@solimant why remove the generic? Did this cause problems?
customValidate?: CustomValidator<T>, | ||
customValidate?: CustomValidator, |
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.
@solimant why remove the generic? Did this cause problems?
element, | ||
_recurseList | ||
); | ||
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] = |
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.
Does it work to update the pathSchema
type above? Right now it is (incorrectly?) defined as PathSchema
. Maybe it wants to be PathSchema<T>
. Or maybe create a local variable that is cast once to the proper type? I.e.
// on line 74 do this?
const keyablePathSchema = pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> });
This avoids multiple casts later and can be commented once as to why its being done
And then this line and all others below become:
keyablePathSchema[i as keyof PathSchema<T>] = ...
@@ -64,7 +64,11 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F | |||
for (const name in schema.properties) { | |||
const field = get(schema, [PROPERTIES_KEY, name]); | |||
const fieldId = idSchema[ID_KEY] + idSeparator + name; | |||
idSchema[name] = toIdSchemaInternal<T, S, F>( | |||
(idSchema as { [key in keyof IdSchema<T>]: IdSchema<T> })[name as keyof IdSchema<T>] = toIdSchemaInternal< |
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.
Add a comment here to explain why the cast
@solimant sorry, I was waiting to review this with @nickgros and we had some additional feedback |
Thank you for the review and feedback, @heath-freenome and @nickgros. I'll work on incorporating that. |
Reasons for making this change
The way
IdSchema
andPathSchema
are defined results in reaching a conflicting type when the type argument passed to those types is of a primitive type, such asstring
.For example, in the snippet below (TS playground link), when the type argument for
IdSchema
isstring
(i.e.IdSchema<string>
), the effective resolved type would be an intersection ofFieldId
andstring
, which is impossible:When the type argument is
string
,keyof string
in TypeScript actually results inkeyof String
, which includes index signatures likelength
,toString
, etc. However, becauseIdSchema
andPathSchema
map over the keys of the string primitive type, we need to consider thatstring
itself is not treated as an object with properties that can be indexed. Thus,IdSchema
andPathSchema
will effectively ignore any keys and result instring
.This PR addresses this contradiction by limiting the resolved type of
IdSchema
andPathSchema
to justFieldId
andFieldPath
, respectively, when the type argument is a primitive type, in which it leverages the existing typeGenericObjectType
. This would let TypeScript accept the types as intended (TS playground link):If this is related to existing tickets, include links to them as well. Use the syntax
fixes #[issue number]
(ex:fixes #123
).If your PR is non-trivial and you'd like to schedule a synchronous review, please add it to the weekly meeting agenda: https://docs.google.com/document/d/12PjTvv21k6LIky6bNQVnsplMLLnmEuypTLQF8a-8Wss/edit
Checklist
npm run test:update
to update snapshots, if needed.