Comments (9)
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.
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.
@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.
@pirix-gh thanks for the swift fix!
from ts-toolbelt.
@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.
@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) ?
from ts-toolbelt.
@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.
@regevbr I've just released v2.1, including performance improvements for PathValid
from ts-toolbelt.
@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)
- Documentation needs to have examples HOT 1
- Unreadable documentation du to contrast issues HOT 2
- U.Merge adds unexpected index signatures when applied to intersection types
- Dark theme is too hard to read code. HOT 4
- Namespace for testing related types
- Any.Compute changes empty tuple type
- String.StartsWith?
- Custom merge types
- Exact Object HOT 3
- request: retheme the documentation
- Object.Paths broken with TS 4.9 HOT 8
- O.Merge with nested objects should compute.
- OptionalDeep<any> shows an error
- Update syntax highlighting colors for docs HOT 2
- A/Any namespace doesn't export "If" type
- deprecate `Narrow` type
- `Update` doesn't work when attempting to make keys non-nullable
- Create a new docs website HOT 3
- Incorrect Optional (Patch?) results on types which look like Error
- Flexible export
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-toolbelt.