TypeScript Fields

Main inspiration for that feature have been dependency injection frameworks. Preferred way to inject fields' values to classes is to do it via constructor. This forces very repetitive construction of constructors where we always have argument passed and assigned to the corresponding field in a class. TypeScript Fields feature is designed to let programmer get rid of that redundancy with possibility of automatic fields' generation. There are 4 annotations that contribute to that feature: @Private, @PackagePrivate, @Protected and @Public. Each of it should be applied to the constructor in a class and causes Kendal to generate field with proper access modifier. There is also an optional attribute makeFinal which let's programmer decide whether new field should be final or not. It defaults to true.

Vanilla Java


      @Service
      class FooService {

          private final SomeFactory someFactory;
          private final SpecialValidator specialValidator;
          private final ItemRepository itemRepository;

          @Autowired
          FooService(SomeFactory someFactory, SpecialValidator specialValidator, ItemRepository itemRepository) {
              this.someFactory = someFactory;
              this.specialValidator = specialValidator;
              this.itemRepository = itemRepository;

              // some logic
          }

          Object createNewItem(Object newItemDto) {
              specialValidator.validate(newItemDto);
              Object newItem = someFactory.construct(newItemDto);
              return itemRepository.save(newItem);
          }
      }
  

Kendal


      @Service
      class FooService {

          @Autowired
          FooService(@Private SomeFactory someFactory, @Private SpecialValidator specialValidator,
                      @Private ItemRepository itemRepository) {
              // some logic
          }

          Object createNewItem(Object newItemDto) {
              specialValidator.validate(newItemDto);
              Object newItem = someFactory.construct(newItemDto);
              return itemRepository.save(newItem);
          }
      }