Skip to content

feat request: allow preserving keys (+ types by key) to map over objects #12393

Closed
@KiaraGrouwstra

Description

@KiaraGrouwstra

I'm hoping to be able to properly type functions like Ramda's R.map (for objects) or Lodash's _.mapValues -- in such a way as to acknowledge that mapped over values (which may have been of different types) may give results of different types, dependent on both their respective input type as well as on the mapper function.

Code

let arrayify = <T> (v: T) => [v];
declare function mapObject<T, V, M extends {[k: string]: T}>(func: (v: T) => V, m: M): {[K in keyof M]: V}
// ... or Lodash's _.mapValues, Ramda's R.map / R.mapObjIndexed / R.project...
mapObject({ a: 1, b: 'foo' }, arrayify)

Desired behavior:
{ a: number[], b: string[] }

Actual behavior:
{ a: any[], b: any[] }

I apologize for the use of libraries for this example. Probably more verbose without, but the concept is common in FP libraries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions