-
-
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
Conversation
@@ -223,6 +223,25 @@ 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The types here should limit it to only allow using appendToArray
on a key defined to have an array value or unknown.
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.
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 comment
The 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 comment
The 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 comment
The 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 @ts-expect-error
comment to ignore the error just for the test.
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.
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 comment
The 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
source/index.ts
Outdated
const list = this.get(key, [] as T[Key]); | ||
|
||
if (!Array.isArray(list)) { | ||
throw new TypeError('Config param already set and is not array'); |
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.
throw new TypeError('Config param already set and is not array'); | |
throw new TypeError(`The key \`${key}\` is already set to a non-array value`); |
It must be documented in the readme. |
@@ -223,6 +223,25 @@ 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 comment
The 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.
readme.md
Outdated
@@ -338,6 +338,19 @@ const unsubscribe = conf.onDidAnyChange(key, callback); | |||
unsubscribe(); | |||
``` | |||
|
|||
#### .appendToArray(key, value) | |||
|
|||
Append an item to array. |
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.
… an array value or unknown.
This is exactly what I am looking for! Anything I can help to get this merged? |
Yes, see: #124 (comment) |
@subins2000 Bump |
Allows appending or creating an array item with
.appendToArray
: