0

I was refactoring the other people's code. The original fragment was:

        this.respondValue$
            .subscribe((partForm) => {
                this.sale = { ...this.sale, ...partForm };
            });

I wanted to introduce unsubscribe here and added takeUntil:

        this.respondValue$
            .pipe(takeUntil(this.unsubscribe$))
            .subscribe((partForm) => {
                this.sale = { ...this.sale, ...partForm };
            });

After that i've started getting the following error:

        (parameter) partForm: unknown: Spread types may only be created from object types.ts(2698)

Is there any fast workaround if i don't want to take care of investigating and describing the exact partForm type?

E F
  • 474
  • 3
  • 15
  • code looks right. Maybe a wrong import ? Can you show them (pipe, takeUntil...) ? Also, what is the typing of `respondValue$` ? – Random May 06 '21 at 08:25
  • 1
    Try defining the type as `any`: `.subscribe((partForm: any) => { ... }`. – ruth May 06 '21 at 08:27
  • @EF just make sure that `partForm` is spreadable, because if it is not you will get runtime error. If it is an object, I think it is better to use `Record`, `object` types of some interface for this argument – captain-yossarian from Ukraine May 06 '21 at 08:33
  • @EF: While the `any` workaround solves the issue you ought to define a type for the `partForm` (eg. using `Interface` or `Class`) to take advantage of the Typescript type assertion features. – ruth May 06 '21 at 08:35
  • Despite there seems to be no reason using `: any` with such a simple code, it should also not solve the issue, since it is the same code as before the refactoring. So it should neither have worked before... – Random May 06 '21 at 08:38
  • For some reason VisualStudioCode started indicating the error only after adding takeUntil. The goal of my refactoring was just to avoid memory leaks. Of course i do understand that `any` is evil in the typescript universe... – E F May 06 '21 at 10:50

0 Answers0