In order to combine types together in TypeScript you will need to use the Union operator which is a pipe symbol ‘|’ to do so. In the following example, I will create a function that will take in either one of the three types of TypeScript objects that are type number, type string, and type boolean and output the relevant outcome according to the type of object the user has entered into that function.
function objtype(obj : string | number | boolean) { if(typeof obj == "string") { console.log(`${obj} is a string`) } else if(typeof obj == "number") { console.log(`${obj} is a number`) } else { console.log(`${obj} is a boolean`) } } objtype(3) objtype("Hello World") objtype(true)
The above program will produce the below outcome:-
3 is a number Hello World is a string true is a boolean
You can combine as many types of TypeScript objects as you like with the union operator in TypeScript.