Giter Site home page Giter Site logo

Comments (9)

millsp avatar millsp commented on July 17, 2024 1

Cool! We started support at 3.5 because of this breaking change microsoft/TypeScript#30769
Which allowed me to improve performance very much (indexes)

I will update the Readme to mention more clearly the non-support

Happy coding

from ts-toolbelt.

millsp avatar millsp commented on July 17, 2024 1

In short, let's say you have an object O and a path ['a', 'b', 'c']

type O = {
    a: {
        b: {
            c: {}
        }
    }
} 

Instead of trying all possible paths like before (heavy), PathValid now follows a given path and stops when it's wrong, then fills whatever is left with never. So it stops, and to do so, it follows your path:

// We use `At` to do the following
type test0 = O['a']['b']['x']
// But `At` never fails, it rather returns `never`

So if we dive in O, and the path is wrong (or finished) it will end up being never.

Then you might wonder why use brackets? Because if you don't use them and O (like O['a' | 'b']) is an union then the union will get distributed. But I don't what that, I want to match a specific condition. It might not be useful in this case (because I match never), but it clearly indicates that I do not want to distribute it.

from ts-toolbelt.

millsp avatar millsp commented on July 17, 2024

@regevbr, thanks for reporting this first bug. From now on, don't hesitate to open issue, bug or feature request. Happy coding!

from ts-toolbelt.

regevbr avatar regevbr commented on July 17, 2024

@pirix-gh thanks for the swift fix!

from ts-toolbelt.

regevbr avatar regevbr commented on July 17, 2024

@pirix-gh this still doesn't work for me... (v2.0.14)

import { Object, Any } from 'ts-toolbelt';

export function getProp<O extends object, P extends string[]>(obj: O,
                                                              ...keys: Any.Cast<P, Object.PathValid<O, P>>)
  : Object.Path<O, P> {
  return keys.reduce(
    (result: any, key: string) => (result === null || result === undefined) ? undefined : result[key],
    obj);
}

const o1: any = { a: { b: { c: 100 } } } ;

const t0 = getProp(o1, 'a', 'b', 'c');
//  Argument of type '"a"' is not assignable to parameter of type 'HasPath<any, ["a"], any, "default"> extends true ? "a" : never'.

from ts-toolbelt.

millsp avatar millsp commented on July 17, 2024

@regevbr, your error message looks odd. I get no errors on TS 3.5, 3.6

There is no support for TS below 3.5. What is your environment (TS, IDE) ?
Screenshot from 2019-06-27 09-57-01

from ts-toolbelt.

regevbr avatar regevbr commented on July 17, 2024

@pirix-gh I was using 3.4.5 because of breaking changes that were introduced in 3.5 (the deep nesting error). I just upgraded to 3.5.2 and indeed it works!
Thanks!

from ts-toolbelt.

millsp avatar millsp commented on July 17, 2024

@regevbr I've just released v2.1, including performance improvements for PathValid

from ts-toolbelt.

regevbr avatar regevbr commented on July 17, 2024

@pirix-gh thanks! saw what you did there - stopped the iteration once you hit a block - nice!
Can you please explain this line though :
https://github.com/pirix-gh/ts-toolbelt/blob/c0287eb0a8b51383b6fc1b5910a412d4dc7071b0/src/Object/PathValid.ts#L26

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.