Errors 10 exercises
solution

Options for Resolving Class Property Errors

The "no initializer and not definitely assigned in the constructor" error happens because our class isn't set up properly:


class User {
private username: string; // red squiggly line under username
}

There are two primary ways to solve this issue: initializing the property or as

Loading solution

Transcript

00:00 Okay, let's break down this error, see why it's occurring. Property, username, so this username is a property of the class, has no initializer. It's not actually being initialized, it's not being assigned to, private username will just sort of exist. When we actually instantiate the class,

00:17 const user equals new user here, sure, we can access user.username, or in fact we can't because it's private, but let's say we could. User.username, it's supposed to be a string, but what value does it have? It's not actually being given a value, so it's not being initialized.

00:33 And then it's not definitely assigned in the constructor. So it seems like we have two choices here. We can either give it an initializer or assign it in the constructor. Let's see what that means. We can give it an initializer. We can say private username string equals an empty string.

00:52 And so username here is now being inferred as string, so we say const user equals new user. I'll change this to being public just so we can access it. And now user.username is going to be an empty string on this. So this is a class property initializer here. We are initializing it, kicking it off

01:11 with some sort of value. But how about assigning in the constructor? Well, we can create a constructor here. We can say constructor, which is a function which is called when the user class is initialized. And then we can say this.username equals an empty string. Perfect, this basically does the same thing.

01:29 If we have two here, if we have a username like this and we give it like an actual string there, the constructor will win here. So it's like the class initializer or class property initializer gets called first or gets run first, and then the constructor gets run afterwards. So then there's a third one here,

01:47 which is we can basically make this optional. If we think, right, actually, this error just doesn't matter to me. I don't care about this. I just want to sometimes have a username available on this object. Then we can say private username might exist, and it's a string. So there we go. That's how to make this error go away in a few different ways.

02:06 This basically has no initializer. You haven't initialized it. It's not definitely assigned in the constructor. And let's just hone in on definitely for a second, because that's interesting. If we were to say if math.random is greater than 0.5, for instance, then if we put it in there, is this gonna, oh no,

02:24 because we've accidentally got an initializer on it too. Now, it's just got a 50-50 chance of being initialized, right? It's not definitely assigned in the constructor. And it basically, like TypeScript is pretty clever here. It's basically saying, okay, you've got a 50-50 chance of doing this. It's not gonna work out for you

02:44 if it's like greater than 0 or something, which I'm pretty sure it always will be, certainly when it's greater than minus one. If you take it out of the if statement, now it's definitely going to be assigned. So that's what that definitely is doing inside that error message. So sure, you've got the choice of either giving an initializer

03:03 or actually assigning it in the constructor, or of course, just making it optional. That's a choice too. And so out of those three options, there's probably enough context for you to solve your error.