Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now type inference is great.
And in general, up to this point
we already learned quite a bit
about TypeScript and its core types.
But up to this point, we also always had the scenario
that every variable took just one type.
So we always had just one possible type that could be stored
in a variable,
just a number or just a string or just a boolean.
And often that is what you need,
but sometimes you want to allow multiple, different types.
Let's say here for the course
we might be fine with storing a string in there
but maybe storing a course ID
as a number should also be correct.
It really depends on what you're building.
It's not unrealistic that we could have a variable
where both a string, as well as a number should be allowed
or that you have some variable
which should allow two different kinds of objects.
And for this scenario, that you have more than one type
which you want to allow
you have a feature called union types.
A union type is a type definition that allows more
than one type.
You add it like any other type assignment by adding a colon
after the variable name,
on the left side of the equal sign,
and then your type.
Now I mentioned that explicitly setting string
again here would be redundant, but it's not redundant
if you don't want to use the type inference here.
But if you instead want to specify such a union type,
hence allowing multiple types, you set up such a union type
by adding a pipe symbol after your first type
and then adding another type, like number.
And you can have as many types
which you want to allow here as needed,
but here I just want to have string or number.
And now you see that that error
which we had down here before is gone,
because now we are allowing a number alternatively
to a string value.
And of course you can use such a union type anywhere
where you have a type assignment,
not just when you're using type inference.
I could also use a union type up here
allowing username to be a string or
let's say an array of strings, for example
so that we have first and last name
in an array, for example.
That's also something I could allow here.
Union types are a never key TypeScript feature
which allows you to define more flexible values and types.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.