Skip to content

Add $ObjMap type utility#217

Open
Davifek wants to merge 1 commit into
piotrwitek:masterfrom
Davifek:codex/add-objmap
Open

Add $ObjMap type utility#217
Davifek wants to merge 1 commit into
piotrwitek:masterfrom
Davifek:codex/add-objmap

Conversation

@Davifek

@Davifek Davifek commented May 20, 2026

Copy link
Copy Markdown

Summary

  • add $ObjMapFn and $ObjMap<T, Fn> so object property values can be mapped through a type-level mapper
  • export the new utility and document the mapper pattern in the README
  • add dts-jest coverage and snapshots for the mapped object and optional property behavior

Verification

  • node node_modules/prettier/bin-prettier.js --list-different src/utility-types.ts src/utility-types.spec.ts src/utility-types.spec.snap.ts src/index.ts
  • node node_modules/typescript/bin/tsc -p . --noEmit
  • node node_modules/tslint/bin/tslint --project ./tsconfig.json
  • node node_modules/jest/bin/jest.js --config jest.config.json --no-cache --testMatch "**/src/utility-types.spec.ts"
  • node node_modules/dts-jest/bin/dts-jest-remap.js ./src/utility-types.spec.ts --rename "{{basename}}.snap.{{extname}}" --check
  • git diff --check

Notes

Fixes #18.

Prepared with Codex assistance from the Davifek fork.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the $ObjMap and $ObjMapFn utility types, which allow mapping property values of an object through a type-level mapper, similar to Flow's $ObjMap. The changes include the implementation in src/utility-types.ts, exports in src/index.ts, comprehensive test cases, and updated documentation in the README.md. Feedback suggests making the $ObjMap type distributive to correctly handle union types of objects, ensuring that each constituent of the union is mapped individually.

Comment thread src/utility-types.ts
Comment on lines +153 to +155
export type $ObjMap<T extends object, Fn extends $ObjMapFn> = {
[K in keyof T]: $ObjMapApply<Fn, NonUndefined<T[K]>>;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To better support union types, consider making $ObjMap distributive. In its current form, if T is a union of objects (e.g., { a: string } | { b: number }), the result will be an empty object because keyof T will be never. Using a distributive conditional type ensures that each constituent of the union is mapped individually, which more closely aligns with Flow's behavior.

Suggested change
export type $ObjMap<T extends object, Fn extends $ObjMapFn> = {
[K in keyof T]: $ObjMapApply<Fn, NonUndefined<T[K]>>;
};
export type $ObjMap<T extends object, Fn extends $ObjMapFn> = T extends any
? { [K in keyof T]: $ObjMapApply<Fn, NonUndefined<T[K]>> }
: never;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$ObjMap support

1 participant