Skip to content

Generic Lens cannot interop with useController #52

@raigoinabox

Description

@raigoinabox

While using this library I've discovered a curious type error.

function test<T>(lens: Lens<T>) {
  useController(lens.interop())
}

doesn't typecheck. useController doesn't accept lens.interop() type.

One issue seems to be that, LensSelector type is defined like this:

type LensSelector<T> = [T] extends [any[]] ? ArrayLens<T> : [T] extends [FieldValues] ? ObjectLens<T> : [T] extends [boolean | true | false] ? PrimitiveLens<boolean> : [T] extends [null | undefined] ? never : PrimitiveLens<T>;

In which you can see PrimitiveLens<boolean>. This causes a typescript union of HookFormControlShim<T> | HookFormControlShim<boolean> (shortened for brevity) and that doesn't seem to me like it can pass the T generic check. If instead you replace PrimitiveLens<boolean> with PrimitiveLens<T> then the union goes away and we have now a proper HookFormControlShim<T>. I couldn't truly verify that this was a problem because of the second issue.

Unfortunately the second issue is beyond my comprehension. Seems like everything should work but they do not. When I tried different options, I got really weird results. Good luck!

This is my shortened tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "jsx": "react-jsx",
    "strict": true,
    "baseUrl": "src/main/frontend",
    "module" : "es2020",
    "moduleResolution": "Node",
    "resolveJsonModule": true
  },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions