freezer icon indicating copy to clipboard operation
freezer copied to clipboard

Typescript definition file

Open Juancoll opened this issue 7 years ago • 4 comments

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!

Juancoll avatar Jul 10 '17 14:07 Juancoll

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?

arqex avatar Jul 10 '17 16:07 arqex

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

rutgervd avatar Nov 20 '17 08:11 rutgervd

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.

arqex avatar Nov 20 '17 16:11 arqex

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

a7madgamal avatar Aug 16 '22 12:08 a7madgamal