Skip to content

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

Description

@unional

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions