0
interface StringValidator { isAcceptable(s: string): boolean; }
let validators: { [s: string]: StringValidator } = {};

My understanding of typescript is limited and I tried looking at the docs but I couldn't quite navigate to the correct location to answer this question.

My understanding of the code is:

first: we have an interface, self-explanatory, an object of type StringValidator must have an isAcceptable method, which takes a single string-type argument, and returns a boolean value

second: we have a variable named 'validators', which is wholly undecipherable to me

Questions:

  1. why is the array element in the validators object { [s: string]: StringValidator } not a variable name? my understanding is that any object member is a variable in itself, and the name must be supplied, all I see is a cryptic array declaration

  2. why is the obviously apparent array of type string assigned another type? is that to say that the elements (which should be of type string) implementing the stringvalidator interface? what the heck even is that?

Can somebody (anybody) explain (any links are highly appreciated) what the heck is going on with this statement 'let validators: { [s: string]: StringValidator } = {};'

I have no clue how to interpret this, any and all suggestions/help is highly appreciated

2
  • Your validators type is an object with string keys and StringValidator values. Think of it like Record<string, StringValidator> Commented Jan 19, 2024 at 4:17
  • See index signatures for the official docs Commented Jan 19, 2024 at 4:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.