This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Union Types and Type Aliases in TypeScript

    Ari PickerAri Picker

    Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an array. This lesson will show us how to assign more than 1 type to a variable with Typescript union types and type aliases.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Annotating a variable looks like this. Annotating a variable to have more than one type looks like this. Thing is a string or number.

    00:09 This is called a Union type, and a Union type isn't just two types. It's as many as you want within the limitations of the machine.

    00:17 This is cool until you want to define a function whose argument can be all of these types, so let Return something equal a function that accepts something who can be any of these types, and then returns something.

    00:31 Luckily, TypeScript has aliases. All we've got to do is change the Let to Type, and the colon to an equals. Now let's add the type alias to the function argument. Let's call the function, run the compiler and the code, and we can see that we returned a cool guy.

    00:51 You probably want the function to behave differently depending on the type, so let's use a type guard. If type of something equals a string, or type of something equals a number, or type of something equals a Boolean, console log something.

    01:07 Here, I've used the Type of type card. We also have Instance of. If something is an instance of an array, we're going to join each of those things together. When you use a type card, TypeScript knows what the type is.

    01:21 TypeScript knows that something is an instance of an array, so that when I start to use something, all of the methods that are available on an array will be available, and for each, that's what I want.

    01:35 For each thing, add all the things together, both the IDE and compilers show no errors, because they know that something is an array.

    01:43 Let's console log our joined things. Call Return something. Let's try a number, run the compiler and the code. Sweet. Now let's try an array. Run the compiler and the code. It totally worked.

    02:02 The smarter way to do this would be to use the instanceof Array as the first condition, and just use an ounce for everything else. Let's see how this works. Run the compiler and the code. Sweet.

    02:16 One thing to watch out for is Union typing objects with Not Objects. Object with a Name property that's a string, with a Not Object. Here, I'm going to use my Type guard for the string, "Type of stuff equals string." All right, the IDE is cool with that. "Type of stuff.name equals string."

    02:41 We can see already that the IDE is pretty mad about this property right here, so let's see what the compiler says. "Property name does not exist on type string or an object with name string." Clearly, it exists on the object, but string doesn't have a name property.

    02:59 The compiler gets mad, because it tries to find the best common type between string and this object, but there is no common type. You also have to watch out when union typing object literals that don't share a common parameter.

    03:12 Here we have two objects with different parameters. Let's make a function that accepts a thing whose type is Cool Things. "If type of thing.name equals a string," and we can see already that the IDE is upset about the name property, but let's go all the way through to see what the compiler will say.

    "03:30 If type of thing.id equals a number, return it." Let's run the compiler. There's an error for each time the IDE displays an error. Here and here are these two, and here and here are these two. These are saying the same thing that we saw in this example, which is "Name does not exist on an object without a name."

    03:52 This one says, "ID does not exist on an object without an ID," and again, the compiler is getting mad, because there is no common type. However, if you have two objects with the same parameter, you can access that common parameter, but not the uncommon parameters.

    04:11 Let's say, "Return sat.cool or sat.lame," and we can see that the IDE is upset about the lame property, because it is not a common property, but it's cool with the cool property, because the cool property is a common property.

    04:26 To review, the Union type is defined by adding an Or pipe. The Type alias is kind of like a bar, except you're defining a type, not a variable. As of now, we have Type of and Instance of for type cards. Type cards let us differentiate between types and allow TypeScript to know what those types are.

    04:45 If you Union type Objects with Not Objects, the compiler gets mad. If you Union type Objects without a common parameter, the compiler gets mad. If you Union type Objects with a common parameter, you can access that common parameter.

    Discuss

    Discuss