Skip to content

Allow passing wider type to more narrow type #38

@aspirisen

Description

@aspirisen

In current implementation it is not possible to pass lens with wide type to lens with narrow type.

For example this code will not work:

function Form() {
  const { control } = useForm<{ child: 'a' | 'b' | 'c' }>();
  const lens = useLens({ control });

  // Will be a type error, however it should be fine
  return <ChildForm lens={lens} />;
}

function ChildForm({ lens }: { lens: Lens<{ child: 'a' | 'b' }> }) {
  return null;
}

Because of that it is not possible to pass a union type:

function Form() {
  const { control } = useForm<{ type: 'a'; a: string } | { type: 'b'; b: string }>();
  const lens = useLens({ control });

  // Has a type error
  return <ChildForm lens={lens} />;
}

function ChildForm({ lens }: { lens: Lens<{ type: 'a'; a: string }> }) {
  return null;
}

And the same with optional fields:

function Form() {
  const { control } = useForm<{ child?: string }>();
  const lens = useLens({ control });

  // Still an error, but should work
  return <ChildForm lens={lens} />;
}

function ChildForm({ lens }: { lens: Lens<{ child: string }> }) {
  return null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions