Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
222 const handleSubmitAddCollab = async () => {
223 const updatedCollaborator: NewCollaborator = {
224 ...newCollaborator!,
225 role: selectedRole?.value!,226 };
227
228 let isValid = true;
Description
Optional chain expressions are designed to return undefined
if the optional property is nullish. Using non-null assertions after an optional chain expression is wrong, and introduces a serious type safety hole into your code.
Bad Practice
foo?.bar!;
foo?.bar!.baz;
foo?.bar()!;
foo?.bar!();
foo?.bar!().baz;
Recommended
foo?.bar;
(foo?.bar).baz;
foo?.bar();
foo?.bar();
foo?.bar().baz;