#TS2420 when using dynamic code to set missing types (really hard to explain in a title)

6 messages · Page 1 of 1 (latest)

sly sluice
#
const panzoomDefaults = {
    a: 3
};

type PanzoomDefaults = typeof panzoomDefaults;

type PanzoomOptionsStartup = {
    bind: Element // bind element on startup
};

type PanzoomOptions = Partial<PanzoomOptionsStartup & PanzoomDefaults>;

export class Panzoom implements PanzoomDefaults { // ERROR SHOWS HERE
    constructor(options?: PanzoomOptions) { 
              // Set options
          for (const key in panzoomDefaults)
            this[key] = options?.hasOwnProperty(key) ? options[key] : panzoomDefaults[key];
        
        }
}
Class 'Panzoom' incorrectly implements interface '{ a: number; }'.
  Property 'a' is missing in type 'Panzoom' but required in type '{ a: number; }'.ts(2420)

I know I can simply put a: number in the class or manually do each property of PanzoomOptions

Is there a better way?

sly sluice
#

ts-ignore it is x-x

#

nope but that just doesnt inherit the types

#

it just fails silently

#
class PanzoomOptions {
    a: number = 3;
};

type PanzoomOptionsStartup = {
    bind: Element
};

type PanzoomOptionsConstructor = Partial<PanzoomOptionsStartup & PanzoomOptions>;

export class Panzoom extends PanzoomOptions {
    constructor(options?: PanzoomOptionsConstructor) {
        super();
#

figured it out