Skip to content

assoc returns a narrower type than what is passed #35

@ericyd

Description

@ericyd

Summary

assoc sometimes returns a more narrow type than what is passed to it. Since assoc never removes props, this feels wrong from a type perspective. I would expect assoc to only add or modify props, never remove.

In the example below, a complex type with 2 props is typed as Record<'flag', bool> after passing through assoc

Dependencies

  • ramda@0.29.0
  • @types/ramda@0.29.1
  • typescript@5.0.2

Example

import { assoc } from "ramda";

type OriginalType = {
  name: string;
  flag: boolean;
};

function takesAnOriginalType(original: OriginalType) {
  console.log(original);
}

const array: OriginalType[] = [{ name: "test", flag: true }];

const mapped = array.map(assoc("flag", false));

takesAnOriginalType(mapped);
                    ^^^^^^

Error

const mapped: Record<"flag", boolean>[]
Argument of type 'Record<"flag", boolean>[]' is not assignable to parameter of type 'OriginalType'.
  Type 'Record<"flag", boolean>[]' is missing the following properties from type 'OriginalType': name, flagts(2345)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions