collection icon indicating copy to clipboard operation
collection copied to clipboard

Rewrite in TypeScript

Open Widdershin opened this issue 8 years ago • 8 comments
trafficstars

Widdershin avatar Nov 22 '16 04:11 Widdershin

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 :)

atomrc avatar Jul 26 '17 11:07 atomrc

Does it work if you use .never<Component>()?

Hypnosphi avatar Jul 26 '17 11:07 Hypnosphi

@Hypnosphi Where should I put this? in the signature?

atomrc avatar Jul 26 '17 11:07 atomrc

Oh, I though it's because of xs.never() used somewhere

Hypnosphi avatar Jul 26 '17 11:07 Hypnosphi

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

atomrc avatar Jul 26 '17 11:07 atomrc

items: Array<Component> = [] maybe?

Hypnosphi avatar Jul 26 '17 11:07 Hypnosphi

@Hypnosphi oh god, it works!!! I am confused I though I tried this notation :/ Thanks and sorry for the noise

atomrc avatar Jul 26 '17 11:07 atomrc

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!

shfrmn avatar Aug 01 '18 18:08 shfrmn