1

I'm new to Angular and was going through various scenarios and examples. One of the examples I came across is the following custom validator:

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls.password.value) {
      return { confirm: true, error: true };
    }
    return {};
  };

In the above, what is the code { [s: string]: boolean } mean? Is it checking for the input value or it is used for something else?

Ero0706
  • 13
  • 3

2 Answers2

1

{ [s: string]: boolean } uses an index type, saying that confirmationValidator returns an object with properties keyed by strings whose values are booleans.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

This is Typescript. The { [s: string]: boolean } declares the return type of the arrow function.

This is how Typescript describes the type of objects that can have any strings as keys, and booleans as values.

It's a bit of a shame, as a type could have been used that listed all the optional keys (like required, confirm, error). It would seem that that would give more of the benefits of Typescript.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79