Add $ObjMap type utility#217
Conversation
There was a problem hiding this comment.
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.
| export type $ObjMap<T extends object, Fn extends $ObjMapFn> = { | ||
| [K in keyof T]: $ObjMapApply<Fn, NonUndefined<T[K]>>; | ||
| }; |
There was a problem hiding this comment.
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.
| 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; |
Summary
$ObjMapFnand$ObjMap<T, Fn>so object property values can be mapped through a type-level mapperVerification
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.tsnode node_modules/typescript/bin/tsc -p . --noEmitnode node_modules/tslint/bin/tslint --project ./tsconfig.jsonnode 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}}" --checkgit diff --checkNotes
Fixes #18.
Prepared with Codex assistance from the Davifek fork.