npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@oasisdigital/angular-typed-forms-helpers

v3.2.0

Published

Some helper types for when you are dealing with Angular's typed reactive forms system

Downloads

173

Readme

Angular Typed Forms Helpers

This package includes helper types for when you are dealing with Angular's typed reactive forms system. These types allow for translating raw typescript interfaces/types into the Reactive Forms Types and back into raw typescript interfaces/types for the value.

Installation

npm

npm install --save-dev @oasisdigital/angular-typed-forms-helpers

yarn

yarn add --dev @oasisdigital/angular-typed-forms-helpers

Then you can simply import the helper interfaces from angular-typed-forms-helpers.

Version Compatability with @angular/forms

  • 3.2.x - Angular 17.x.x
  • 3.1.x - Angular 16.x.x
  • 2.2.x - Angular 15.x.x
  • 2.1.x - Angular 14.x.x

Stackblitz Demo

For the below sections describing the different interfaces/types, if you want a NonNullable version of any of them simply prefix the type/interface with NonNullable. This does not include the value & rawValue types as they work regardless of Nullability.

For example, with AngularForm you would do NonNullableAngularForm.

AngularForm Interface

This interface allows for deeply translating an object or general TS type/interface into one of the 3 main Angular Reactive Forms types (FormControl, FormGroup, FormArray).

import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { AngularForm } from '@oasisdigital/angular-typed-forms-helpers';

export interface Animal {
  name: string;
  species: string;
  lifeStage: string;
  birthDate: Date;
  alive: boolean;
}

export interface Zone {
  name: string;
  maxCapacity: number;
  animals: Animal[];
}

type AnimalForm = AngularForm<Animal>;
type ZoneForm = AngularForm<Zone>;

const animalForm: AnimalForm = new FormGroup({
  name: new FormControl(''),
  species: new FormControl(''),
  lifeStage: new FormControl(''),
  birthDate: new FormControl(new Date('01 Jan 1994')),
  alive: new FormControl(true),
});

const zoneForm: ZoneForm = new FormGroup({
  name: new FormControl(''),
  maxCapacity: new FormControl(10),
  animals: new FormArray<AnimalForm>([]),
});

It is important to note this interface only covers basic cases of form structures, it makes the assumption that all objects are FormGroups and arrays are FormArrays. If you would like to convert on a per property basis consider using the AngularFormGroup or AngularFormArray from the below sections.

The NonNullable version of this type is NonNullableAngularForm.

AngularFormGroupShallow Interface

This interface will do a shallow conversion of an object over to the Angular Typed Forms system. Where every property of the object becomes a FormControl.

type ZoneForm = AngularFormGroupShallow<Zone>;
const zoneForm: ZoneForm = new FormGroup({
  name: new FormControl(''),
  maxCapacity: new FormControl(10),
  animals: new FormControl<Animal[]>([]),
});

The NonNullable version of this type is NonNullableAngularFormGroupShallow.

AngularFormGroup Interface

This is a subset of the AngularForm interface that deeply converts an object over to the Angular Typed Forms system. If you give an array type to this interface the return will be never. This is due to a limitation of diffing arrays and objects in the generic extension type.

type ZoneForm = AngularFormGroup<Zone>;
const zoneForm: ZoneForm = new FormGroup({
  name: new FormControl(''),
  maxCapacity: new FormControl(10),
  animals: new FormArray<AnimalForm[]>([]),
});

The NonNullable version of this type is NonNullableAngularFormGroup.

AngularFormArrayShallow Interface

This interface will do a shallow conversion of an array over to the Angular Typed Forms system. Where the Array subtype becomes a matching FormControl subtype for the FormArray.

type ZonesForm = AngularFormArrayShallow<Zone[]>;
const zonesForm: ZonesForm = new FormArray([
  {
    name: new FormControl(''),
    maxCapacity: new FormControl(10),
    animals: new FormControl<Animal[]>([]),
  },
  {
    name: new FormControl(''),
    maxCapacity: new FormControl(10),
    animals: new FormControl<Animal[]>([]),
  },
]);

The NonNullable version of this type is NonNullableAngularFormArrayShallow.

AngularFormArray Interface

This is a subset of the AngularForm interface that deeply converts an array over to the Angular Typed Forms system.

type ZonesForm = AngularFormArray<Zone[]>;
const zonesForm: ZonesForm = new FormArray([
  {
    name: new FormControl(''),
    maxCapacity: new FormControl(10),
    animals: new FormArray<AnimalForm>([]),
  },
  {
    name: new FormControl(''),
    maxCapacity: new FormControl(10),
    animals: new FormArray<AnimalForm>([]),
  },
]);

The NonNullable version of this type is NonNullableAngularFormArray.

AngularFormValue Interface

This interface is used to translate the .value property type from a Angular Reactive Forms object. This interface automatically accounts for the Partial<> nature of FormGroups since sub-controls can be disabled. If you would like the whole form value regardless of disabled controls see AngularFormRawValue below.

type AnimalForm = AngularForm<Animal>;

const animalForm: AnimalForm = new FormGroup({
  name: new FormControl(''),
  species: new FormControl(''),
  lifeStage: new FormControl({ value: '', disabled: true }),
  birthDate: new FormControl(new Date('01 Jan 1994')),
  alive: new FormControl(true),
});

const animalValue: AngularFormValue<AnimalForm> = animalForm.value;
/*
{
  name?: string | null;
  species?: string | null;
  lifeStage?: string | null;
  birthDate?: Date | null;
}
*/

This interface also works for custom implementations of the AbstractControl class.

AngularFormRawValue Interface

This interface is used to translate the .getRawValue() method return type from a Angular Reactive Forms object. This will include all sub-controls regardless of their disabled state.

type AnimalForm = AngularForm<Animal>;

const animalForm: AnimalForm = new FormGroup({
  name: new FormControl(''),
  species: new FormControl(''),
  lifeStage: new FormControl({ value: '', disabled: true }),
  birthDate: new FormControl(new Date('01 Jan 1994')),
  alive: new FormControl(true),
});

const animalValue: AngularFormRawValue<AnimalForm> = animalForm.getRawValue();
/*
{
  name: string | null;
  species: string | null;
  lifeStage: string | null;
  birthDate: Date | null;
}
*/

This interface also works for custom implementations of the AbstractControl class.