-
-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .appendToArray()
method
#124
Changes from 3 commits
22c1270
7778f0d
de63b83
957a5a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,24 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I | |
this.store = store; | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The types here should limit it to only allow using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm new to typescript, I tried to understand much from the existing code to make this one. Sorry, I'm not sure how to do what you mean 😅 Can you suggest one please ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being new to something is a great opportunity to learn more about it. Give it a shot at least. I can help if you really cannot do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can I know if the overload definition I set is correct, I tried doing store.set('a', 1)
store.appendToArray('a', 2) These lines didn't show any error in VSCode, it should right ?, that's how typescript should work right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests would fail if it's correct, as TS would refuse to compile. You need to make it TS fail, and then you can use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried many, but can't figure it out. Is there any examples that I can look for ? I've been at this for hours now 😅. The best I could come up to do "limit it to only allow using appendToArray on a key defined to have an array value". Each line is a separate try and not a group : appendToArray<Key extends keyof T, Index extends keyof any[]>(key: Key, value: T[Key][Index]): void
appendToArray<Key extends keyof T, Index extends keyof T[Key]>(key: Key, value: T[Key][Index]): void
appendToArray<Key extends keyof T, Index extends T[Key][keyof T[Key]]>(key: Key, value: T[Key][Index]): void There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sindresorhus I'm not able to make it, would like to know the solution please |
||
Append an item to array | ||
|
||
@param {key|object} - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. Or a hashmap of items to set at once. | ||
@param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`. | ||
*/ | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
appendToArray<Key extends keyof T>(key: Key, value: Required<T>[Key]): void; | ||
// This overload is used for dot-notation access. | ||
appendToArray<Key extends string>(key: Key, value: Required<T>[Key]): void { | ||
const array: [] = this.get(key, [] as T[Key]); | ||
|
||
if (!Array.isArray(array)) { | ||
throw new TypeError(`The key \`${key}\` is already set to a non-array value`); | ||
} | ||
|
||
this.set(key, [...array, value]); | ||
} | ||
|
||
/** | ||
Check if an item exists. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a bit more verbose and also explain the behavior of how it works, like if the existing value is not array, or if there's no value.