Annotations and Assertions 12 exercises
Problem

Non-null Assertions

Here we'll revisit a previous exercise, but solve it in a different way.

The findUsersByName function takes in some searchParams as its first argument, where name is an optional string property. The second argument is users, which is an array of objects with id and name properties:

``

Loading exercise

Transcript

00:00 This exercise is a setup that we've seen in a previous exercise, and we're going to solve it in a different way. We have a findUsersByName function. That findUsersByName function takes in some search params of which name is an optional property on them. And then we have an array of users that's passed in too, which has objects with id and name on, and that's an array.

00:19 Then if searchParams.name is defined, then we filter the users by that name. But we're getting an error inside there, basically saying user.name includes searchParams.name, string or undefined is not assignable to parameter of type string.

00:35 Your job is to try to figure out how we can use an assertion to actually make this error go away. We previously solved this exercise by extracting searchParams.name into a const variable and then doing the check on that, and that works fine.

00:53 But I want you to actually do this in a slightly more unsafe way using a non-null assertion. Good luck.