What is this piece of TypeScript code doing?

Viewed 82

I am learning TypeScript and am still fairly new to it, and am attempting to digest this bit of code from the react-hook-form (library?) that is being deprecated.

I was lead to the declaration of this code as I am receiving the error

Argument of type 'UnpackNestedValue<PathValue<ControlType, Path<ControlType>>>' is not assignable to parameter of type 'PathValue<ControlType, Path<ControlType>>'. 

Could someone kindly explain what it is doing? I am struggling to make sense of it.

export declare type UnpackNestedValue<T> = T extends NestedValue<infer U> ? U : T extends Date | FileList | File | Blob ? T : T extends object ? {
[K in keyof T]: UnpackNestedValue<T[K]>;
} : T;
1 Answers

UnpackNestedValue with generic type input T is type of;

  1. If T is of extended type of a NestedValue, say U, then U.

  2. If not, then if T is of extended type of Date, FileList, File or Blob, then T.

  3. If not, then if T is a non-primitive (object), then for each key of T, the types of respective values of T will be determined by the procedure defined starting from step 1.

  4. If not, then T.

Note that step 3 initiates a recursion to resolve the types for values of T, if the condition in step 1 or step 2 does not hold.

Related