Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

observe reporting incorrect type under observation #255

Open Hyperionza opened 4 years ago

Hyperionza commented 4 years ago

I'm using FJP 3.0.0-1 on TypeScript 3.9 with Angular 9

I've got a strongly types set of UDTs analogous to:

export class ChildClass { Id: number} 

export class ParentClass {
    myList: Array<ChildClass>;

    constructor() {
        this.myList = new Array<ChildClass>();
    }
}

export class MyComponent implements OnInit, OnDestroy{
    obj: ParentClass;
    observer: Observer<Array<ChildClass>>;

    ngOnInit(): void {
        this.obj = new ParentClass();
        this.observer = jsonpatch.observe(this.obj.myList); // *1
    }

    ngOnDestroy(): void {
        if (this.observer) {
            jsonpatch.unobserve(this.obj.myList, this.observer); // *2
        }
}

Note 1: in the current setup, this line throws a compile time exception: Type 'Observer' is not assignable to type 'Observer<ChildClass[]>'

Note 2: if I change observer to Observer<ChildClass>, then this line throws a compile time exception.

I can work around the compile time exceptions by using Observer<any> or Observer<ChildClass | Array<ChildClass>>, but that does defeat the purpose of strongly typed code.

miyconst commented 4 years ago

cc @Starcounter-Jack

Starcounter-Jack commented 4 years ago

I suspect this is not a complete runnable example (for example there is an uncompilable comma at jsonpatch,observe(this.obj.myList); // *1.

It also appears that the type signature of this.obj.mylist is a UserGroup or an array of the same.

Hyperionza commented 4 years ago

@Starcounter-Jack

I suspect this is not a complete runnable example (for example there is an uncompilable comma at jsonpatch,observe(this.obj.myList); // *1.

It also appears that the type signature of this.obj.mylist is a UserGroup or an array of the same.

Fixed your issue with the typo in the example - sorry, I tried to quickly write a simple example rather than paste my actual code base, which I cant for obvious reasons. Do you want a stack blitz or something?

Updated the workaround paragraph to match the example classes.