Skip to content

Replace remaining any usages with shared types/interfaces (TypedArray, MotionKernel, …) #85

Description

@kalwalt

Summary

The .ts source still contains ~25 uses of any (across ~9 modules). Most map to a small set of real shapes — introducing a few shared type aliases/interfaces would eliminate the majority and improve type safety, editor autocomplete and refactor safety.

Environment

  • Product/Service: src/**/*.ts (excluding generated types/**/*.d.ts)
  • Version: post-0.9.0 (dev)
  • Constraint: tsconfig runs strict: true but strictNullChecks: false and noImplicitAny: true — so these are all explicit anys, not inferred ones.

Actual state (inventory)

Grouped by the underlying real type, most-impactful first:

1. Typed-array payloads → a shared TypedArray alias
The recurring one. matrix_t.data, pyramid_t.data, pool buffers etc. are always one of Uint8Array | Int32Array | Float32Array | Float64Array.

  • src/matrix_t/matrix_t.ts:19,63data: any (interface + class)
  • src/matrix_t/matrix_t.ts:25,27copy_to(other: any), resize(..., ch: any)
  • src/pyramid_t/pyramid_t.ts:17,59data: any, b: any = this.data[0]
  • src/node_utils/data_t.ts:44buffer?: any
  • src/node_utils/_pool_node_t.ts:22buffer: any
  • src/imgproc/imgproc.ts:698dst_tilted: any[]
  • src/optical_flow_lk/optical_flow_lk.ts:18scharr_deriv: any

2. Cache pool node → _pool_node_t

  • src/cache/cache.ts:13,85put_buffer(node: any) (interface + impl); the node is a _pool_node_t
  • src/node_utils/_pool_node_t.ts:16next: any (self-referential _pool_node_t)

3. Motion-model kernel → a MotionKernel interface
motion_estimator.ransac/lmeds take kernel: any; the real contract is { run(from, to, model, count): number; error(...): void; check_subset(...): boolean }, implemented by affine2d/homography2d.

  • src/motion_estimator/motion_estimator.ts:137,250kernel: any

4. Point-subset scratch arrays → point_t[]

  • src/motion_estimator/motion_estimator.ts:156,157,270,271subset0/1: any = []

5. matrix_t args typed loosely → matrix_t

  • src/linalg/linalg.ts:605svd_decompose(A: any, ...) (A is a matrix_t)

6. Misc / lower-value

  • src/cache/cache.ts:9,47allocate(capacity: any, ...) (should be number)
  • src/imgproc/imgproc.ts:1179skindetector(src: { ...; data: any[] })
  • src/yape/yape_utils.ts:130dirs: any[] | Int32Array

Proposed approach

  1. Add a shared export type TypedArray = Uint8Array | Int32Array | Float32Array | Float64Array; (e.g. in src/data_type/ or a small src/types.ts) and replace group 1.
  2. Replace group 2 with the existing _pool_node_t type.
  3. Define and export a MotionKernel interface; type groups 3–5 against it / matrix_t / point_t[].
  4. Mop up group 6 with the obvious primitive/real types.

Incremental is fine — one module (or one group) per commit. The parity suite (npm test, 63 tests) must stay green at each step since several of these are on hot numeric paths.

Impact

Low — no runtime behavior change; purely type-safety / DX. But it's cheap, self-contained, and removes a class of latent bugs the compiler currently can't catch. Aligns with the AGENTS.md convention: "Prefer real types over any in new code."

Non-goals

  • Not enabling strictNullChecks (separate, larger effort).
  • Not touching generated types/**/*.d.ts.
  • No algorithm/numeric changes — types only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions