dot-diver icon indicating copy to clipboard operation
dot-diver copied to clipboard

Use circular constraints for depth-problem

Open djfhe opened this issue 2 years ago • 0 comments

Currently, if a type has many cyclic dependencies (see readme-question), TypeScript fails due to the large number of possible paths.

If circular constraints were better supported (see https://github.com/microsoft/TypeScript/issues/51011), an alternative approach like the one below could be feasible:

function getByPath<T extends SearchableObject, P extends PathsOneLevelDeeper<T, P>>(
  object: T,
  path: P
): number {
  const pathArray = (path as string).split('.')

  return pathArray.reduce((accumulator: any, current) => accumulator?.[current], object) as any
}

The PathsOneLevel type would return P (or a valid path in T close to P) and all paths on level deeper. This would significantly reduce the number of paths TypeScript has to compute, potentially addressing the issue with excessive recursion depth.

djfhe avatar Mar 26 '23 15:03 djfhe