Giter Site home page Giter Site logo

Help with Path* methods about ts-toolbelt HOT 7 CLOSED

millsp avatar millsp commented on August 15, 2024
Help with Path* methods

from ts-toolbelt.

Comments (7)

millsp avatar millsp commented on August 15, 2024

Wow this is a crazy quest you've started! (Personally, I use Ramda (it's ok/well typed))

I doubt that anyone would be able to guess the proper ouput type as it's implementation-dependent.

But you could get a wider type though that is the return type of the iteratee as an array/list:
ReturnOf<Iteratee>[]

Where Iteratee is the generic type of the iteratee function that is passed to map.

And if you get this error it means that you're either doing something too complex or what I've explained over here

from ts-toolbelt.

regevbr avatar regevbr commented on August 15, 2024

@pirix-gh I'm not sure I follow...
The example I presented doesn't work as a standalone so it has nothing to do with overloading of the Iteratee ...

from ts-toolbelt.

millsp avatar millsp commented on August 15, 2024

Then I am not sure of what you're doing. I never used underscore. I just saw that the docs specify the second argument but I see that you're passing an array? But I can't see this anywhere in the docs.

So I guess you're trying to do some kind of function type overload and some kind of selection?
If so, then you should write a mapped type that picks the right sub-elements. So it would map your first argument type then transform it (that's what mapped types are used for) with O.Pick

from ts-toolbelt.

regevbr avatar regevbr commented on August 15, 2024

The docs state vaguely (and the code backs it up) that iteratee value is actually passed to _.iteratee and then if you pass a string path array, it will be passed to _.property so my use case is valid.

But all of this has nothing to do with my problem, as the example above simply fails regardless of the other overloaded methods (which I can send here if you want)

It seems that the issue only happens because we pass an explicit array (instead of spread args)... Where in the following example it works good:

        mapE<T extends object, K extends string[]>(
            list: T[],
            ...iterator: A.Cast<K, O.PathValid<T, K>>): Array<U.Nullable<O.Path<T, K>>>;

_.map( [{arr: {arr2: [5, 1, 7]}}, {arr: {arr2: [5, 1, 7]}}], 'arr', 'arr2');

Can you tell why that happens?

from ts-toolbelt.

regevbr avatar regevbr commented on August 15, 2024

The following example does work with the original code (no spread).
It seems that it is related to how Typescript realizes the type of the array.

_.map( [{arr: {arr2: [5, 1, 7]}}, {arr: {arr2: [5, 1, 7]}}], ['arr', 'arr2'] as ['arr', 'arr2']);

One can try to use

_.map( [{arr: {arr2: [5, 1, 7]}}, {arr: {arr2: [5, 1, 7]}}], ['arr', 'arr2'] as const);

But then the array becomes readonly and can't be passed into your library types...

from ts-toolbelt.

millsp avatar millsp commented on August 15, 2024

You can, use T.Writable before passing it to other types. Sorry that I can't help you more atm. I am very busy.

from ts-toolbelt.

regevbr avatar regevbr commented on August 15, 2024

That works!
Writable of Tuple is not exported - I will open a bug for it.

from ts-toolbelt.

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.