g-makarov / dot-path-value Goto Github PK
View Code? Open in Web Editor NEWSafely get and set deep nested properties using dot notation.
License: MIT License
Safely get and set deep nested properties using dot notation.
License: MIT License
What – Wrong type for optional paths
Expected – string | undefined
Actual – string
Example
import {getByPath} from 'dot-path-value';
import { Equal, Expect } from "@type-challenges/utils";
type AuthorDetails = {
author?: {
location?: {
city: string;
};
};
};
const obj: AuthorDetails = {
author: {
location: {
city: "London"
}
},
};
const authorLocationCity = getByPath(obj, "author.location.city");
type cases = [
// ✅ optional path
Expect<Equal<typeof authorLocationCity, string | undefined>>,
];
TS Playground – https://tsplay.dev/WJ7e6w
Given the following code:
import { getByPath } from 'dot-path-value';
type Recursive = {
a: string;
r: Recursive;
};
function x(type: Recursive) {
getByPath(type, '');
~~~~~~~~~~~~
}
we get the following error:
Type of property 'r' circularly references itself in mapped type
'{ [K in //keyof Recursive]-?: PathConcat<K & string, Recursive[K]>; }'.(2615)
which makes sense why it would not print until infinity (or at the max TS iterations).
Don't try to infer the fields of the recursive type, or allow inference for only one level deep.
BTW, really cool library, thanks for sharing 🙂
I am using getByPath
in a helper function and passing in a Path<T>
as a parameter. The path is useful to me in another package, which takes an array rather than a "dotted" path. However, attempting path.split('.')
throws up a Property 'split' does not exist on type 'Path<T>'
typescript error.
function takesPathAsArray(path: string[]) {
// does something fancy with the path
}
function helperFn<T>(obj: T, path: Path<T>) {
const value = getByPath(obj, path);
// error is here v v v v v v v v
const somethingElse = takesPathAsArray(path.split('.'));
}
I can of course cast path
as a string there, but I was wondering if there was a way for the library to support it?
Hi,
Thanks for the library. It would be great if it provided an hasByPath()
method to check the path's availability.
Thanks,
Hello, g-makarov
I think I found vulnerability in this package. Where can I send this report??
It seems that Path
doesn't pick out object keys which are numbers, rather than strings. For example:
const obj = {
days: {
1: true,
"2": false,
3: true
}
}
type PathObj = Path<typeof obj>;
// ^? type PathObj = 'days' | 'days.2'
Example playground for reference
const obj = {
a: {
b: [
{
c: 1,
},
],
},
}
getPath(obj, a.b[0].c) // 1
js is easy, safe type is too difficult 😅.
export function getByPath(
obj,
path,
) {
return path
.replace(/\[([^\[\]]*)\]/g, '.$1.')
.split('.')
.filter(t => t !== '')
.reduce((prev, cur) => prev && prev[cur], obj)
}
Hi,
It seems that passing a generic which extends something does not correctly resolve at least the extended options:
Reproduction: https://tsplay.dev/NaEx9N
Firstly, tremendous typescript expertise on offer here. Lots to learn from.
I was wondering how you might feel about a PR for a setByPath
function? I have knocked up a basic one locally using your existing getByPath
function.
Let me know and I will put one together!
The type of the item accessed is a union of all the types of the items in the array:
const obj = {
a: {
b: [
{
c: 1,
},
'test',
true
],
},
};
const value = getByPath(obj, 'a.b.1');
In this case the type of value
is string | boolean | { c: number; }
where it should be string
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.