Skip to content
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

Typescript definition file #107

Open
Juancoll opened this issue Jul 10, 2017 · 4 comments
Open

Typescript definition file #107

Juancoll opened this issue Jul 10, 2017 · 4 comments

Comments

@Juancoll
Copy link

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
Copy link
Owner

arqex commented Jul 10, 2017

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:

  • Objects, with methods set, replace, remove, toJSON, pivot, transact, now an events operations.
  • Arrays, with replace, toJSON, pivot, transact, now` and all event and array operations.
  • Leaves: They have no methods in it.

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

@rutgervd
Copy link

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

@arqex
Copy link
Owner

arqex commented Nov 20, 2017

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
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants