Checked branded types

Open
#18 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
frontend

Research direction

Start with the useLens type definitions and reproduce the issue using the branded EmailAddress and FormSchema shown in the report. Trace where the form value type is reduced to string, then verify that passing the resulting lens to Child preserves the branded email type without introducing the reported incompatibility.

Written by the indexing model from the issue text.

Description

help wanted
import { type Lens, useLens } from '@hookform/lenses'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

const EmailAddressS = z.string().min(1).email().brand('EmailAddress')
type EmailAddress = z.infer<typeof EmailAddressS>

const FormSchema = z.object({ email: EmailAddressS })

type FormValue = z.infer<typeof FormSchema>

const Child = ({ lens }: { lens: Lens<FormValue> }) => <></>

function Parent() {
  const { control } = useForm({ resolver: zodResolver(FormSchema) })

  const lens = useLens({ control })

  /*
  Here cannot pass lens as type info is reduced to plain string in useLens().
  Probably "brand" should be opaque/unchecked.
  ....
  Types of property 'email' are incompatible.
    Type 'string | undefined' is not comparable to type 'string & BRAND<"EmailAddress">'.
      Type 'string' is not comparable to type 'string & BRAND<"EmailAddress">'.
        Type 'string' is not comparable to type 'BRAND<"EmailAddress">'.
  */
  return <Child lens={lens} />
}



Dominant language
TypeScript
Stars
150
Forks
13
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from react-hook-form/lenses

All issues in react-hook-form/lenses

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.