Search code examples
typescriptjhipstertypescript-typingsjdl

jhipster generates required fields as not required in typescript types


I am using the jhipster generator to automatically generate Typescript types. Recently I've noticed that the generator has some behavior I did not expect. I have the following jdl code:

entity MeasurementResult {
    frequencies Float required
}

which generates following typescript interface out of it:

interface IMeasurementResult {
    frequency?: number
}

Now my question is why is it generated with a "?" (indicating it can be undefined), while it is defined as required in the jdl file?


Solution

  • I'm guessing the JHipster team decided to make all fields optional (?) because form validation takes care of model correctness anyway. You can see exactly where the generator does this here.

    This means that, for example, near the top of your update.component.ts you will find a Validators.required on that field definition. Everything is validated again server-side.