I am working on something that has checkboxes and I don't understand why the following is not valid TypeScript:
declare function checkChecked(isAllChecked: boolean): void
declare function determineIfChecked(): boolean
const checkBoxes: number[] = []
const toggleCheckAll = (isAllChecked?: boolean): void => {
if (isAllChecked === undefined) isAllChecked = determineIfChecked()
checkBoxes.forEach(() => checkChecked(isAllChecked))
checkChecked(isAllChecked)
}
The isAllChecked in the call of checkChecked in the forEach loop raises an:
Argument of type 'boolean | undefined' is not assignable to parameter of type 'boolean'. Type 'undefined' is not assignable to type 'boolean'.
I'm guessing my understanding of the scope of function arguments is not quite right as I would have thought the if statement should have updated the type of isAllChecked to be boolean and that this would be carried through to the forEach loop.
I can work round this but I would like to understand why this is not valid TypeScript.