Giter VIP home page Giter VIP logo

Comments (2)

Harris-Miller avatar Harris-Miller commented on July 18, 2024

Great catch! Symbols() are inherently non-enumerable. Do you have a solution in mind? If not I can look into fixing

from types.

Harris-Miller avatar Harris-Miller commented on July 18, 2024

While looking into this, I found out it's worse than just symbols. The current typing for toPairs includes any non-enumerable property in its output, including class methods!

Check out this simple instance of a class put through R.toPairs

Now let's put the same code into typescript/play

Simplified here, you get this

import * as R from 'ramda';

class Point {
  public x: number;
  public y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }

  map(fn: (a: number) => number) {
    return new Point(fn(this.x), fn(this.y));
  }
}

const point = new Point(3, 2);

const asPairs = R.toPairs(point);
//    ^? ["x", number] | ["y", number] | ["map", (fn: (a: number) => number) => Point]
// but the value you get is `[["x", 3], ["y", 4]]`

What this means is we can never use keyof to determine what should be part of the output array of key/value pairs

Because you also would have map if you use a function to create and object literal:

const makePoint = (x, y) => ({
  x,
  y,
  map: fn => makePoint(fn(x), fn(y))
});

const point2 = makePoint(3, 2);
const asPairs2 = R.toPairs(point2);
//    ^? ["x", number] | ["y", number] | ["map", (fn: (a: number) => number) => Point]
// but the value you get is `[["x", 3], ["y", 4], ["map", null]]`

I've always thought that the way Object.entires was typed was just due to very old typescript, before it had the ability to be more specific

entries<T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][];

But I understand now that it's due to how keyof does not descriminate against non-enumerables and non-own-properties

I'm thinking that the types for toPairs and fromPairs just needs to be typed exactly like Object.entries and Object.fromEntries, respectively.

Same with Object.keys and R.keys, Object.values and R.values, etc

Shitty thing is this is going to be a breaking change

from types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.