[英]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:您好,我有一个基础 class,我还有其他类扩展了这个基础 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:基础 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:子 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()但我在我的 super() 上遇到了这个错误
BaseError.ts(41, 15): An argument for 'args' was not provided.
BaseError.ts(41, 15):未提供“args”的参数。
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基本上我有一个带有默认数据的 class 和其他使用此默认数据并具有更多属性/类型/接口的类我不知道我是否有正确的逻辑,如果有人可以告诉我
In your call to BaseError
s constructor using super()
, you did not specify the error, basically change your constructor to something like:在使用
super()
调用BaseError
的构造函数时,您没有指定错误,基本上将构造函数更改为:
/* 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;
}
*/
The BaseError constructor takes an args parameter, which you have not specified in your super() call. BaseError 构造函数采用 args 参数,您没有在 super() 调用中指定该参数。
You can specify a default value for the args parameter to resolve this.您可以为 args 参数指定默认值来解决此问题。
constructor(args: DefaultErrors = ‘’)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.