Skip to content
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

feat: improve types #195

Closed

Conversation

lifeiscontent
Copy link
Contributor

@lifeiscontent lifeiscontent commented Jun 21, 2023

What I did

  • Removed most definitions of any
  • Fixed some Typo's
  • Fixed some value castings that didn't look like they were happening but the types were expecting them (not sure if this was a correct assumption)

@codesandbox
Copy link

codesandbox bot commented Jun 21, 2023

This branch is running in CodeSandbox. Use the links below to review this PR faster.


CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders | Preview

const valueMap = new Map<keyof Constraints, unknown>();
const format =
schema.formatError ??
(defaultFormatError as FormatErrorFunction<Constraints, FormError>);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this casting type is currently as lie, I saw that you had a FIXME here, did you have some other idea for going about fixing this?

@@ -697,7 +698,7 @@ export function parse<
});
}

const value = Object.fromEntries(valueMap) as any;
const value = Object.fromEntries(valueMap) as InferType<Constraints>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also a little bit of a lie, I currently just forced it to whatever the used variable is expecting there's also a case where it expects both a Partial and T but didn't make sense of the full context, any suggestions here would be helpful.

@@ -63,15 +63,15 @@ export function getFieldsetConstraint<Source extends yup.AnyObjectSchema>(
case 'min':
if (
!constraint.min ||
constraint.min < Number(test.params?.min)
Number(constraint.min) < Number(test.params?.min)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I casted these, because the original type said they could be a number or string, let me know if you were expecting something else and I'll update accordingly.

@@ -98,7 +98,7 @@ export function parse<Schema extends yup.AnyObjectSchema>(
}: {
name: string;
intent: string;
payload: Record<string, any>;
payload: Record<string, unknown>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed these all to unknown so that it was clear we weren't handling the payload directly in library code and if there needed coersion it will warn in the future about potential improper data handling

schema: z.ZodType<T>,
): FieldsetConstraint<z.input<Source>> {
if (schema instanceof z.ZodObject) {
const result: FieldsetConstraint<z.input<Source>> = {};
const result: Record<string, FieldConstraint<unknown>> = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea here is to cast it to a type that is currently known within this code block

result[key] = inferConstraint(def);
}

return result;
return result as FieldsetConstraint<z.input<Source>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then assume we did our duediligence and hand back the type the caller is expecting.

}

if (schema instanceof z.ZodEffects) {
return resolveFieldsetConstarint(schema.innerType());
return resolveFieldsetConstraint(schema.innerType());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

@@ -17,7 +17,9 @@ interface PlaygroundProps<
children: ReactNode;
}

export function Playground({
export function Playground<
TSubmission extends { error: Record<string, string | string[]> | null },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can pull this out if you want, doesn't really matter.

* @see https://conform.guide/api/react#list
*/
export const list = new Proxy<{
type List = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea here is this is used as our type contract, while the implementation is casted where needed.

payload: ListIntentPayload<Schema>,
): Array<Schema> {
): Array<Schema | undefined> {
Copy link
Contributor Author

@lifeiscontent lifeiscontent Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this had to change, there's a possiblity that this doesn't make sense, since I believe there's some optionality happening in the ListIntentPayload type, which might indicate that it might be worth being pulled out or rearanged to make this make sense during actual runtime usage.

any context you have on the operations that take place here would be helpful.

edmundhung added a commit that referenced this pull request Jun 26, 2023
@edmundhung edmundhung mentioned this pull request Jun 26, 2023
@lifeiscontent lifeiscontent deleted the feat/improve-types branch June 27, 2023 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant