isaacs / node-primordials

https://isaacs.github.io/node-primordials/
11 stars 3 forks source link

Function params are `unknown` and not generics, thus not useable in TypeScript #10

Open unional opened 8 months ago

unional commented 8 months ago

For example:

import { ArrayPrototypeReduce } from 'node-primordials'

const input: string[] = []

const result: Array<{ value: string }> = ArrayPrototypeReduce(
  input,
  (acc, value) => (acc.push({ value }), acc), [] as Array<{ value: string }>
)

The above code does not work because the type of ArrayPrototypeReduce is:

type ArrayPrototypeReduce = (
  self: unknown,
  callbackfn: (previousValue: unknown, currentValue: any, currentIndex: number, array: any[]) => unknown, 
  initialValue: unknown
) => unknown

It should be something like:

// note that there are improvements can be done to this type
type ArrayPrototypeReduce<T, I> = (
  self: T[],
  callbackfn: (previousValue: I, currentValue: T, currentIndex: number, array: T[]) => I, 
  initialValue: I
) => I