I am using NSwag to generate a typescript client for a backend API. Ultimately, it ends up generating a method:
login(request: LoginRequest): Promise<AuthResponse>
This is fine, however the problem is with the LoginRequest
constructor. It generates like so:
export class LoginRequest implements ILoginRequest {
email!: string;
password!: string;
constructor(data?: ILoginRequest) {
if (data) {
// ...
}
}
}
As such, it allows LoginRequest
to be created without any of the required data. I had a look at the source of the generator and it appears there is no option to change it.
Is there a way to force the constructor to make the data
parameter required?
Note that I'm aware I could set the generation type to use interfaces, but then as far as I can see I would lose some of the parsing smarts (ie. date strings to date instances). If I could use only interfaces but retain that logic somehow that would be ideal.