Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i disable / handle init, toJSON

I've created a .NET core 2.2 webapi, and using swagger / nswag to generate the API for my React / typescript application. When I try to set up a new object I get a ts(2739) message:

Type '{ firstName: string; lastName: string; }' is missing the following properties from type 'User': init, toJSON

Is there any way to disable / handle this globally? it works like it should but I would like to get rid of the error (maybe a ts-ignore?)

I've tried multiple solutions like so;

Error but read the data:

const newUser: User = {
                firstName,
                lastName,
            };

No error but doesn't read the data:

const newUser = new User ({
                firstName,
                lastName,
            });

I could also remove all the nswag created init and toJSON but that would be to time consuming.

.NETCore Model (Baseclass is just the Id and createdAtDate)

    public class User : BaseModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Image { get; set; }
    }
}

generated Typescript Nswag Code

export interface IBaseModel {
    id?: string | null;
    creationDate?: Date | null;
    updateDate?: Date | null;
}

export class User extends BaseModel implements IUser {
    firstName?: string | null;
    lastName?: string | null;
    image?: string | null;

    constructor(data?: IUser) {
        super(data);
    }

    init(data?: any) {
        super.init(data);
        if (data) {
            this.firstName = data["firstName"] !== undefined ? data["firstName"] : <any>null;
            this.lastName = data["lastName"] !== undefined ? data["lastName"] : <any>null;
            this.image = data["image"] !== undefined ? data["image"] : <any>null;
        }
    }

    static fromJS(data: any): User {
        data = typeof data === 'object' ? data : {};
        let result = new User();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["firstName"] = this.firstName !== undefined ? this.firstName : <any>null;
        data["lastName"] = this.lastName !== undefined ? this.lastName : <any>null;
        data["image"] = this.image !== undefined ? this.image : <any>null;
        super.toJSON(data);
        return data;
    }
}

export interface IUser extends IBaseModel {
    firstName?: string | null;
    lastName?: string | null;
    image?: string | null;
}

Typescript use class as type

const newUser: User = {
                firstName,
                lastName,
            };

I would like to disable init & toJSON that causes the error, and I don't need to declare them to function.

error:

Type '{ firstName: string; lastName: string; }' is missing the following properties from type 'User': init, toJSON

I would like to get rid of the errors without having to manually rewrite the whole NSWAG generated API client. Maybe I'm using the classes wrong, when using the interfaces I get the same error message.

like image 779
RMCS Avatar asked Oct 02 '19 18:10

RMCS


1 Answers

init and toJSON methods are generated when you create the TypeScript file setting DTO style as Class. If you set that option to Interface style you should not have implemented methods but only properties. See the following cropped image: DTO settings

like image 75
0x3ff Avatar answered Sep 18 '22 19:09

0x3ff