Documentation
@tsly/obj
cast

Cast

Returns the wrapped value, implicitly retyping it object without any restriction or overlap checks

⚠️

This method is fundementally unsafe. It should be used with great caution and only when typescript can't follow typechecking.

Often times, this use case crops up when object properies are being checked on an indexed value. In most cases this indexed object should simply be explicitly bound to a variable, but sitations where it cannot, obj(/* ... */).cast() may be used.

import { obj } from "@tsly/obj";
 
type Shape2D = {
  /* ... */
};
type Shape3D = {
  /* ... */
};
 
type Shape = { type: "2d"; shape: Shape2D } | { type: "3d"; shape: Shape3D };
 
function print2DShape(shape: Shape2D) {
  /* ... */
}
function print3DShape(shape: Shape3D) {
  /* ... */
}
 
function printShapeRange(shapes: Shape[], lowerIdx: number, upperIdx: number) {
  if (shapes[lowerIdx].type == "2d") print2DShape(obj(shapes[lowerIdx]).cast());
  else print3DShape(obj(shapes[lowerIdx]).cast());
 
  if (lowerIdx < upperIdx) printShapeRange(shapes, lowerIdx + 1, upperIdx);
}

Edit on CodeSandbox (opens in a new tab)