Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix getFieldset with interfaces and nullable schemas #508

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/conform-dom/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ export type DefaultValue<Schema> = Schema extends
: Schema extends Array<infer Item>
? Array<DefaultValue<Item>> | null | undefined
: Schema extends Record<string, any>
?
| { [Key in keyof Combine<Schema>]?: DefaultValue<Combine<Schema>[Key]> }
| null
| undefined
? { [Key in keyof Schema]?: DefaultValue<Schema[Key]> } | null | undefined
: string | null | undefined;

export type FormValue<Schema> = Schema extends
Expand All @@ -77,9 +74,7 @@ export type FormValue<Schema> = Schema extends
: Schema extends Array<infer Item>
? string | Array<FormValue<Item>> | undefined
: Schema extends Record<string, any>
?
| { [Key in keyof Combine<Schema>]?: DefaultValue<Combine<Schema>[Key]> }
| undefined
? { [Key in keyof Schema]?: FormValue<Schema[Key]> } | null | undefined
: unknown;

const error = Symbol('error');
Expand Down Expand Up @@ -258,8 +253,7 @@ function createFormMeta<Schema, FormError, FormValue>(
): FormMeta<FormError> {
const lastResult = !initialized ? options.lastResult : undefined;
const defaultValue = options.defaultValue
? // @ts-expect-error
(serialize(options.defaultValue) as Record<string, unknown>)
? (serialize(options.defaultValue) as Record<string, unknown>)
: {};
const initialValue = lastResult?.initialValue ?? defaultValue;
const result: FormMeta<FormError> = {
Expand Down
11 changes: 6 additions & 5 deletions packages/conform-react/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ type SubfieldMetadata<
Schema,
FormSchema extends Record<string, any>,
FormError,
CombinedSchema = Combine<Schema>,
> = Exclude<Schema, undefined> extends Array<infer Item>
> = [Schema] extends [Primitive]
? {}
: [Schema] extends [Array<infer Item> | null | undefined]
? {
getFieldList: () => Array<FieldMetadata<Item, FormSchema, FormError>>;
}
: Exclude<Schema, undefined> extends Record<string, unknown>
: [Schema] extends [Record<string, any> | null | undefined]
? {
getFieldset: () => Required<{
[Key in keyof CombinedSchema]: FieldMetadata<
CombinedSchema[Key],
[Key in keyof Combine<Schema>]: FieldMetadata<
Combine<Schema>[Key],
FormSchema,
FormError
>;
Expand Down
23 changes: 18 additions & 5 deletions playground/app/routes/typing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import {
import { Form } from '@remix-run/react';
import { Playground } from '~/components';

type Task = { content: string; completed: boolean };
interface Task {
content: string;
completed: boolean;
}

type Rule = {
interface Rule {
type: 'rule';
key: string;
operator: string;
value: string;
};
}

type Group = {
interface Group {
type: 'group';
conditions: Array<Group | Rule>;
};
}

type Schema =
| {
intent: 'foo';
tasks: Array<Task>;
date: Date;
}
| {
intent: 'bar';
Expand Down Expand Up @@ -118,6 +122,15 @@ export default function Example() {
// @ts-expect-error
isFieldMetadataType1<Array<string>>(tasks);

const date = fields.date;

isFieldMetadataType0(date);
isFieldMetadataType1<Date>(date);
isFieldMetadataType1<Date | undefined>(date);

// @ts-expect-error
date.getFieldset();

const tasksList = tasks.getFieldList();

isFieldMetadataType0(tasksList[0]);
Expand Down
Loading