marpple / FxTS

A functional programming library for TypeScript/JavaScript
https://fxts.dev
Apache License 2.0
862 stars 63 forks source link

Type inference is not working when used with Enum #233

Open hg-pyun opened 9 months ago

hg-pyun commented 9 months ago

Bug Report

πŸ’» Code

https://codesandbox.io/s/competent-star-g4nqnp?file=/src/App.tsx


import { entries, fromEntries, groupBy, map, pipe, reduce } from "@fxts/core";
import "./styles.css";

type DataSet = {
  status: Status;
  count: number;
};

enum Status {
  Todo = "TODO",
  InProgress = "IN_PROGRESS",
  Done = "DONE"
}

const data: Array<DataSet> = [
  {
    status: Status.Todo,
    count: 0
  },
  {
    status: Status.InProgress,
    count: 0
  }
];

export default function App() {
  pipe(
    data,
    map((item) => {
      return {
        progress: Status.Done,
        count: item.count
      };
    }),
    groupBy((item) => item.progress),
    entries,
    map(([progress, values]) => {
      return [
        progress,
        reduce((acc, cur) => acc + cur.count, 0, values) // <-- Type Error: values
      ] as const;
    }),
    fromEntries
  );

  return null;
}

πŸ™ Actual behavior

TypeScript Inference is not working.

πŸ™‚ Expected behavior

TypeScript Inference works.


Version Information

@fxts/core@0.14.0: fine. @fxts/core@0.15.0: bug occur.

Maybe #199 side effect.

ppeeou commented 9 months ago

@hg-pyun

First If you change the function which inner reduce below, you can use it. but I think improvement is needed.

reduce((acc:number, cur:{count:number}) => acc + cur.count, 0, values)