How can i disable / handle init, toJSON
Asked Answered
S

1

9

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.

Shiver answered 2/10, 2019 at 18:36 Comment(0)
R
10

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

Renaldorenard answered 24/5, 2020 at 18:25 Comment(1)
That works. One minor annoyance to doing this is that the letter "I" is no longer prefixed to interface names. All my other interfaces start with "I" and now this adds a bit of confusion to the code. Do you know of a solution?Barty

© 2022 - 2024 — McMap. All rights reserved.