collection
collection copied to clipboard
Rewrite in TypeScript
Hi guys, I am in the process of adding types to @cycle/collection.
We'll see if I am really able to achieve this task but, for the moment, it's pretty smooth.
I have a question though, concerning default parameters' value. Here is an example : https://github.com/cyclejs/collection/blob/master/src/collection.js#L30
The function collection takes an optional items parameter that defaults to an empty array. I'd like to type this parameters as an array of components but when I do I get errors from the typescript compiler.
Type 'Component[]' is not assignable to type 'never[]'.
Type 'Component' is not assignable to type 'never'.
I have a few ideas in mind, but I am not sure what is the best way to deal with this.
either
-
I change the function signature and change all the internal calls to that function
function collection (options: Options, items: Array<Component>)and change the call of that line https://github.com/cyclejs/collection/blob/master/src/collection.js#L74 -
I use an optional parameter and force it to the empty array at the beginning of the function
function collection(options: Options, items?: Array<Collection>) {
items = items || []
}
- any other way I do not know.
I am pretty new to typescript so the exercise is really interesting! If you have any tips to help my, I'd be grateful :)
Does it work if you use .never<Component>()?
@Hypnosphi Where should I put this? in the signature?
Oh, I though it's because of xs.never() used somewhere
In general, I don't know how to deal with default values. Typescript won't let me type them. I'd like to do something like
function collection (options: Options, items = []: Array<Component>) {
}
But it seems typescript doesn't understand this notation
items: Array<Component> = [] maybe?
@Hypnosphi oh god, it works!!! I am confused I though I tried this notation :/ Thanks and sorry for the noise
Hi guys,
I just created a similar library called cycle-lot. It's 100% percent in typescript and uses proxies to make API simpler. I'd really appreciate your feedback on it.
Let me know what you all think!