تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Void Return Type0:39

throwing an error at me because I have no implicit any type flag. If you are doing the same thing, the way we get rid of this, if you're following along, is to type it as unknown. Don't worry, we'll cover the unknown type later. It's basically just a way of saying we don't know what type this is. So ignore that for now, but return type is void. Function basically doesn't return anything. We're just console.logging. So logger is not going to return anything. So we could then log something, where it goes rules, and we can assign that to A, and then

So logger is not going to return anything. So we could then log something, where it goes rules, and we can assign that to A, and then we can console.log A equals, and then let's log A like that. So let's run this anyway. Again, we have our first log instance where we've actually called the logger, and then we've tried to log out the return value from that. So we get undefined because nothing returns. So that is the void type. Very straightforward. The void type exists in PHP.

Using Null Unions1:38

Very straightforward. The void type exists in php. If you've ever used types in php, you'd be very familiar with the void type. The null type is one that is fairly useful. So let's grab our user interface again, and it's going to have an ID, which is a string. It's going to have a email, which is a string, name, which is a string, or null. So null is generally used as a union type. So we're basically saying a field is nullable. So now we have a User which needs to have an ID and email, but can have a name or age, which can be null.

So now we have a User which needs to have an ID and email, but can have a name or age, which can be null. The reason we say it can be null is let's pretend we're creating a User in our system. So let's imagine that they give us their email on an input form, and then we basically generate a UUID for them, and then the email we set as the email they give us. So this is something we might do, and then on the next screen, we ask for their name and age. This is a legitimate variant in the system, so we want it to exist. But the reason we can set name and age as explicitly string or null is so when we type this as User, we're going to get a type error that says this type is missing the name and

Creating Users with Nulls2:47

But the reason we can set name and age as explicitly string or null is so when we type this as user, we're going to get a type error that says this type is missing the name and age property from user. So we actually want to ensure there is type safety there. So in our generator function, let's say we have createUser, and it's a function that takes an email, which is a string, and it's going to return, you know, ID, which is going to be some new UUID. We would generate that from a function. Email would be the email given in. We want to explicitly set the name and age as null.

Email would be the email given in. We want to explicitly set the name and age as null. createUser is going to return a User. Let's do this instead of const user is going to equal a createUser of hello@email.com. createUser is a function, and the function is going to return a User. There you go. So let's say we have a generator function to create users, and this is how we do it. This forces us to set those properties. Now if you look at the last type, the undefined type, that is essentially what we get when

Optional Properties and Undefined3:50

This forces us to set those properties. Now if you look at the last type, the undefined type, that is essentially what we get when we set something to optional. So let's say instead of a number or null for age, it's going to be a number, but it's going to be an optional property. What that means is age can be undefined. So here we've set age as null. We're going to get a type error because null is not compatible with undefined. It's a different type. We can delete age altogether, and we're not going to get a type error because we're saying

It's a different type. We can delete age altogether, and we're not going to get a type error because we're saying that undefined basically means it does not exist. So we're saying age either doesn't exist or it's a number. So we either have to set it here as a number or we have to leave it as undefined, and then we can also do like a user.age equals whatever we want later on in the process, and it's still going to be fine on the user because we've defined that it can exist.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟