arqex / freezer

A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
MIT License
1.28k stars 56 forks source link

Typescript definition file #107

Open Juancoll opened 7 years ago

Juancoll commented 7 years ago

Hey there!

Lately we worked a lot with Typescript and React and we have noticed that nobody has yet made a declaration file for typescript on FreezerJS. We have tried to create a d.ts file ourselves for our project, but we have noticed that all fields of the state can access the functions of the listener, so we can't declare the state as 'any' type.

Here's our code for d.ts file:

declare interface IFreezerOptions{
    mutable:boolean;
    live:boolean;
    freezeInstances:boolean;
}

declare interface IFreezerListener {
    on:(eventName: string, cb: (...params:any[]) => void) => void;

    once:(eventName: string, cb: (...params:any[]) => void) => void;

    off:(eventName: string, cb: (...params:any[]) => void) => void;

    emit:(eventName:string, ...params:any[]) => void;
}

declare class Freezer implements IFreezerListener{
    new (a: any, b:IFreezerOptions) : Freezer;

    set(state:any):void;
    set(key:any, state:any):void;

    reset(newData:any):void;

    get():any;
    getEventHub(): IFreezerListener;

    on(eventName: string, cb: (...params:any[]) => void) : void;

    once(eventName: string, cb: (...params:any[]) => void) : void;
    off(eventName: string, cb: (...params:any[]) => void) : void;
    emit(eventName:string, ...params:any[]) : void;
}

declare namespace Freezer{
}

export = Freezer;

Is there any way to type the library by ourselvers or are we just squealing because it's impossible?

Thanks a lot to everybody!

arqex commented 7 years ago

Hey Juan,

I think I didn't understand well what's your problem on creating the d.ts file, maybe because I don't use typescript myself.

When you do a get in a freezer store you get a freezer node. What's a freezer node? there are 3 types:

I suppose this polymorphic behavior, is a pain for creating a typescript declaration. Isn't it?

rutgervd commented 6 years ago

@Juancoll Did you guys ever get to finish the bindings? Would love to use them myself.

arqex commented 6 years ago

I think it never got done. I don't use typescript so I don't plan to add it, but if someone wants to create a PR I'd be glad to merge it.

a7madgamal commented 1 year ago

@arqex still not using ts? :D we are migrating our codebase to typescript and freezer makes the heart of the state management logic so we would love to get ts support. I have good experience in TS but never wrote types for a lib before. I will probably give it a try starting with migrating the code itself to ts then see how can I define the types. I will fork it but would you be interested to help me if needed or accept a PR with the changes when done?