You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<Admonitiontype="info"title="Type System Escape Hatches">
352
+
353
+
The `narrow`, `assert`, `defined`, and `cast` methods serve as escape hatches for current TypeScript limitations with lens type compatibility. These methods address scenarios where you need to pass lenses with wider types to components expecting narrower types.
354
+
355
+
These workarounds will become less necessary once [issue #38](https://github.com/react-hook-form/lenses/issues/38) is resolved, which aims to improve lens type variance to allow more natural type narrowing and component composition.
356
+
357
+
</Admonition>
358
+
359
+
### narrow {#narrow}
360
+
361
+
The `narrow` method provides type-safe narrowing of union types, allowing you to tell the type system which branch of a union you want to work with. This is particularly useful when working with discriminated unions or optional values.
362
+
363
+
#### Manual Type Narrowing
364
+
365
+
Use the single generic parameter to manually narrow the type when you know (by external logic) what the value should be:
366
+
367
+
```tsx copy
368
+
// Lens<string | number>
369
+
const unionLens =lens.focus("optionalField")
370
+
371
+
// Narrow to string when you know it's a string
372
+
const stringLens =unionLens.narrow<string>()
373
+
// Now: Lens<string>
374
+
```
375
+
376
+
#### Discriminated Union Narrowing
377
+
378
+
Use the discriminant overload to narrow based on a specific property value:
379
+
380
+
```tsx copy
381
+
typeAnimal=
382
+
| { type:'dog'; breed:string }
383
+
| { type:'cat'; indoor:boolean }
384
+
385
+
const animalLens:Lens<Animal> =lens.focus("pet")
386
+
387
+
// Narrow to Dog type using discriminant
388
+
const dogLens =animalLens.narrow('type', 'dog')
389
+
// Now: Lens<{ type: 'dog'; breed: string }>
390
+
391
+
const breedLens =dogLens.focus("breed")
392
+
// Type-safe access to dog-specific properties
393
+
```
394
+
395
+
<Admonitiontype="important"title="Type Safety">
396
+
397
+
The `narrow` method performs type-level operations only. It doesn't validate the runtime value - use it when you have external guarantees about the value's type (e.g., from validation, conditional rendering, or runtime checks).
398
+
399
+
</Admonition>
400
+
401
+
### assert {#assert}
402
+
403
+
The `assert` method provides runtime type assertions that convince TypeScript the current lens is already the desired subtype. Unlike `narrow`, this is a type assertion that modifies the current lens instance.
404
+
405
+
#### Manual Type Assertion
406
+
407
+
Use the generic parameter to assert the lens is already the desired type:
`assert` is a type-only operation that doesn't perform runtime validation. Ensure your assertions are backed by proper runtime checks to avoid type safety violations.
446
+
447
+
</Admonition>
448
+
449
+
### defined {#defined}
450
+
451
+
The `defined` method is a convenience function that narrows the lens type to exclude `null` and `undefined` values. This is equivalent to using `narrow<NonNullable<T>>()` but provides a more expressive API.
The `cast` method forcefully changes the lens type to a new type, regardless of compatibility with the original type. This is a powerful but potentially **unsafe** operation that should be used with extreme caution.
<Admonitiontype="danger"title="Use with Extreme Caution">
535
+
536
+
`cast` bypasses TypeScript's type system entirely. It can lead to runtime errors if the underlying data doesn't match the asserted type. Always validate data at runtime before using `cast`, or prefer safer alternatives like `narrow` when possible.
0 commit comments