#6 - Union Type in TypeScript — Transcript
Full transcript
- 0:00hey guys this is Naia welcome back to
- 0:01nav automation labs and back to our
- 0:03typescript Series so so far in the
- 0:06typescript what we have seen that we
- 0:08have seen the basics of typescript in
- 0:10the last session we have seen that uh
- 0:12what do you mean by type what do you
- 0:14mean by array what do you mean by enum
- 0:17what do you mean by Tuple right so today
- 0:20we are going to talk about one more data
- 0:22type that we can use it with the
- 0:25variables that is called the union type
- 0:29Okay so let's create a new a file and
- 0:32here I'm writing that Union concept. TS
- 0:38I have changed the theme with the white
- 0:39color tell me if you like it otherwise
- 0:41we will continue with the black color
- 0:42theme also so Union
- 0:45means if let's see a particular variable
- 0:48is there and I really want to give
- 0:50multiple types of the same variable
- 0:52let's see a variable can be numeric also
- 0:55can be string also in that case I can
- 0:58Define both of them right so for example
- 1:00let's say I'm going to create a variable
- 1:03and the variable name that I'm writing
- 1:04that there is one user ID and this user
- 1:06ID can be string also and can be a
- 1:08numeric value right so either of them we
- 1:10can write it so what you do you simply
- 1:12put a bracket like this and then you
- 1:14write a string or and then you can write
- 1:18it could be a number also here like that
- 1:21right and whatever the value that you
- 1:22really want to Define you can Define it
- 1:25here so this user ID can be string or
- 1:27can be number so with this pipe operator
- 1:30or you can say a union operator you can
- 1:32use it here and N number of operators
- 1:34you can give it let's see n number of
- 1:36types you can give it user ID can be
- 1:38Boolean also that Boolean also you can
- 1:40write it here but Boolean does not make
- 1:42any sense to with the user ID so let me
- 1:44remove it from here so now let's see if
- 1:46I'm assigning the user ID is equal to
- 1:48let's see 1,000 or 10,000 1 and now I'm
- 1:52writing the user ID could be some string
- 1:54value that I'm writing let's see naven
- 1:56automation is my user ID either of them
- 1:59I can write here but if I'm writing the
- 2:01user ID is equal to true it will give
- 2:03you the error here because we have given
- 2:05only two types either a string or number
- 2:08so this is called Union type simple so
- 2:12can we use this Union type for the
- 2:14function parameters also of course you
- 2:17can use that so for example let's see if
- 2:19I'm creating the function and then I'm
- 2:21saying that get user information that I
- 2:25really want to fetch the user
- 2:27information and then it's taking one
- 2:29user ID or it's taking let's see one uh
- 2:33customer ID or something so here I'm
- 2:34creating let's see you give me the
- 2:36customer ID and this customer ID could
- 2:39be in any format so here I'm writing
- 2:41that again you put a bracket and then it
- 2:44could be a string also and then again
- 2:46you write a pipe and then it could be a
- 2:48number
- 2:49also right so customer ID that you have
- 2:52to give it to me and then I'm going to
- 2:53give you some user information from here
- 2:57right or let's say I really want to
- 2:59print some user information here that
- 3:01also we can do it here so for example
- 3:03let's see I can write one if
- 3:05condition and let's see I'm going to
- 3:07call this method from here also so let's
- 3:10call this method at the same time I'm
- 3:12calling this and now I'm passing the
- 3:14user ID is let's see
- 3:1690001 so see I'm passing a numeric value
- 3:19so this 90001 will be given to the
- 3:21customer ID and now I'm checking that
- 3:23what type of customer ID that you are
- 3:25getting it so which operator we have to
- 3:27use so here we have to use type of
- 3:30we can use it here so I'm checking if
- 3:34the type of customer ID is equal to
- 3:37equal to equal to a number right so
- 3:41remember type of operator we discussed
- 3:42in the JavaScript also type of operator
- 3:45will tell you that what is a type of a
- 3:47specific variable so if I'm passing
- 3:5090001 90001 is a numeric value or is a
- 3:53number will be given to this so it will
- 3:55tell you that okay the type of customer
- 3:57ID is actually a number if it is a
- 3:59number then I'm saying that okay fine
- 4:01that you have to return some some number
- 4:05right or let's see whatever that you
- 4:06want to return so let's see here I'm
- 4:08writing that some number 1,000 okay
- 4:10whatever 9,000 some information that I'm
- 4:13Printing and along with that I'm
- 4:15printing the name also that okay the
- 4:16username is naen here right something
- 4:19like this let's see I'm assigning it
- 4:21here in the form of return and then
- 4:24again I'm writing another else if that
- 4:27else if that uh if the type of again I'm
- 4:33writing if the type of what if the type
- 4:35of customer ID which is equal to equal
- 4:37to uh string then in that case I'll say
- 4:41that okay no it's just simple you have
- 4:44to
- 4:45return let's see underscore naven only
- 4:48right so simple let's see a simple basic
- 4:50function that I have return so if I'm
- 4:52passing 90001 90001 will be given to
- 4:55this guy and then uh customer ID could
- 4:58be a string on number here I'm passing
- 5:00that c checking that customer ID is
- 5:02equal to number yes this time is equal
- 5:04to number so return the customer ID that
- 5:06you are passing so let's see whatever
- 5:08the customer ID that you are giving to
- 5:09me and then you append with naven or
- 5:12with your username let's see underscore
- 5:13naven or something and then you return
- 5:15back so if I let's see print it on the
- 5:18console directly so I'm just console.log
- 5:21and I'm printing it so what will be the
- 5:23output in that case right so let's
- 5:26simple compile it first and then let's
- 5:28see so here I'm going to compile with
- 5:30the typescript compiler and the file
- 5:33name is the union concept. TS and the
- 5:36respective JS file will be created here
- 5:39and uh then I'm writing what then I'm
- 5:41writing the node and the union dot Js
- 5:46concept. Js so you can say 901 unor
- 5:49naven we are getting it here so at the
- 5:52same time now I'm calling this
- 5:53particular function with some value so
- 5:56let's see here I'm passing the value the
- 5:58customer ID
- 6:00okay in the form of a string that I'm
- 6:02passing so let's see here I'm passing
- 6:05that automation all right so automation
- 6:08is let's see my customer ID will be
- 6:09given to this guy yes it's not giving me
- 6:11any error at the compile time why
- 6:13because customer ID could be string also
- 6:16and then I can check customer ID type of
- 6:18customer ID is number no type of
- 6:20customer ID is a string yes so what will
- 6:23you return I'm returning underscore
- 6:25naven from here and then underscore
- 6:27naven will be printed here so
- 6:30so let's see so I'll do one thing let's
- 6:32see whatever the customer Ida that you
- 6:33are giving to me I'm just using and
- 6:36appending it here that's it okay let's
- 6:39compile it
- 6:41again and uh run it again with the node
- 6:43so here you can see automation uncore
- 6:45naven we are getting it here simple but
- 6:49at the same time if I'm calling this
- 6:50particular function with some uh Boolean
- 6:53value for example I'm passing a true it
- 6:56is giving me the error why because this
- 6:58get us user information can take the
- 7:00customer ID in the form of a string and
- 7:02the number only and that's it so that's
- 7:05why the Boolean is not allowed here so
- 7:08this is very flexible and uh very
- 7:10flexibility given is uh uh in the
- 7:14typescript that you can Define the type
- 7:16sometimes you know your user ID customer
- 7:19ID in alpha numeric form or alphabetic
- 7:22form or a string form or number form
- 7:24like this you can pass it here that's it
- 7:27so I hope this is clear very simple
- 7:28concept if you see the corresponding JS
- 7:31file also in JS it will display like
- 7:34that whatever the customer ID again is
- 7:35checking the customer ID equal to equal
- 7:37to number then return lve return with
- 7:40this thing right but it is more readable
- 7:42and then it's clearly visible that okay
- 7:44yes that uh get user information you can
- 7:47pass either a string or number here
- 7:49other than that you cannot pass I hope
- 7:52this is clear very simple concept very
- 7:54small concept thank you so much
About this transcript
This page contains the full transcript of #6 - Union Type in TypeScript by Naveen AutomationLabs, generated from the public captions YouTube serves with the video. The transcript has 1,501 words across 198 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.