The Value of Typescript | Concept

Ole Ersoy
Apr - 10  -  1 min

We have an array of functions that validate data.

We've defined an function type signature for the validators that looks like this:

export type ValidationFunctionSignature = ( dataMap: Map<number, CsvSelectionData> ) => IndexedMetaError | null

All the functions must be designed according to this signature.

We can then create an array of the functions:

/**
 * Sequenced array of validators
 */
validators: ValidationFunctionSignature[] = [validateKeyValues,validateUniqueKeys]

Now suppose we change the return type of validateKeyValues to boolean. So it's defined like this:

function validateKeyValues(...):boolean

VSCode will now immediately notice that validateKeyValues no longer conforms to the ValidationFunctionSignature and flag it as an error.