Felipe
Felipe

Reputation: 462

Typescript class and multiples sub classes, types and interfaces

Hello I have a base class, and I have other classes that extend this base class:

types and interfaces:

export type fooBar = {
  foo: string;
  bar: string;
};
/*UnprocessableEntityError */
export type missing_fields_error = {
  field: string;
  code: string;
};
/*Default Errors */
export type DefaultErrors = {
  type?: string;
  message?: string;
  code?: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData?: object;
  options?: {
    showPath?: boolean;
    showLocations?: boolean;
  };
};

base class:

export class BaseError extends ExtendableError implements DefaultErrors {
  type: string;
  message: string;
  code: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData: object;
  path: any;
  locations: any;
  _showLocations: boolean = false;
  _showPath: boolean = false;

  constructor(args: DefaultErrors) {
    super(args.message || '');
    const type = args.type;
    const message = args.message;
    const code = args.code;
    const internalData = args.internalData;
    const options = args.options;

    this.type = type;
    this.message = message;
    this.code = code;
    this.internalData = internalData;
    this._showLocations = !options.showLocations;
    this._showPath = !options.showPath;
  }

  serialize(): DefaultErrors {
    const { type, message, code, errors } = this;

    let error: DefaultErrors = {
      type,
      message,
      code,
    };

    return error;
  }
}

sub class:

/* Unprocessable Entity Error */
export class UnprocessableEntityERROR extends BaseError {
  constructor(errors: missing_fields_error[]) {
    super();
    this.type = 'Unprocessable Entity';
    this.message = 'Validation Failed';
    this.code = 422;
  }
}

but i got this error on my super()

BaseError.ts(41, 15): An argument for 'args' was not provided.

basically i have a class with defaults data and other classes that use this defaults data and have more attributes / types / interface I don't know if I have the right logic, if someone can give me a light with that

Upvotes: 1

Views: 114

Answers (2)

Elias Schablowski
Elias Schablowski

Reputation: 2812

In your call to BaseErrors constructor using super(), you did not specify the error, basically change your constructor to something like:

/* Unprocessable Entity Error */
export class UnprocessableEntityERROR extends BaseError {
  constructor(errors: missing_fields_error[]) {
    super({
      type: 'Unprocessable Entity',
      message: 'Validation Failed',
      code: 422,
      errors
    });
  }
}

The interfaces (see comments for details):

export interface fooBar {
  foo: string;
  bar: string;
};

/*UnprocessableEntityError */
export interface missing_fields_error {
  field: string;
  code: string;
};

/*Default Errors */
export interface DefaultErrors {
  type?: string;
  message?: string;
  code?: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData?: object;
  options?: {
    showPath?: boolean;
    showLocations?: boolean;
  };
};

interface fluxFooBar extends fooBar { // This is where it gets interesting since foo and bar are also members
  flux: number;
}
/*
  This is equivalent to:
  type fluxFooBar = {
    foo: string;
    bar: string;
    flux: number;
  }
*/

Upvotes: 2

wlf
wlf

Reputation: 3413

The BaseError constructor takes an args parameter, which you have not specified in your super() call.

You can specify a default value for the args parameter to resolve this.

constructor(args: DefaultErrors = ‘’) 

Upvotes: 1

Related Questions