TypeScript Other Types to Know About
Last Updated :
28 Apr, 2025
TypeScript Other Types to Know About describes that there are many other types that can be used in various ways. like void, object, unknown, never, and function, and how they can be used to enhance the functionality of your code.
Other types to know about:
- void: In TypeScript, a void function is a function that doesn't return any value, or returns undefined.
- object: Object Literals are typically defined as a set of name-value pairs stored in comma-separated lists.
- unknown: In TypeScript, the unknown type is used for variables whose type is not known in advance. It provides type safety by requiring explicit type checks or assertions before using the value, ensuring safe and controlled handling of uncertain data types.
- never: In TypeScript, the never type is a function that never returns a value but throws an exception or error and terminates the program. It is often used to indicate that a function will not return a value or that a variable will never have a value.
- function: Functions in TypeScript represent blocks of code that can be executed and can possess properties like
bind
, call
, and apply
. They're versatile as they can be called and can return different types of values, including this
others.
Example 1: In this example, we are creating a function that takes a parameter of type string and returns void.
JavaScript
function greet(name: string): void {
console.log(`Hello, ${name}!!`);
}
greet("GeeksforGeeks")
Output:
Hello, GeeksforGeeks!!
Example 2: In this example, we are creating an object giving it a key-value pair, and printing the result in the console.
JavaScript
let Employee_details = {
Empname: "John",
EmpSection: "field"
}
console.log("Employee Name is:" +
Employee_details.Empname +
" Employee's section is:"
+ Employee_details.EmpSection
);
Output:
Employee Name is:John Employee's section is:field
Example 3: In this example, we are creating a function that is taking a parameter of unknown type which means it can accept any type of data.
JavaScript
function greet(name: unknown): void {
if (name === null) {
console.log("null!");
} else if (typeof name === "string") {
console.log(`Hello, ${name}!`);
} else {
console.log("Hello, guest!");
}
}
// Calling the greet function
// with different arguments
greet("GeeksforGeeks");
greet(44);
greet(null);
Output:
Hello, GeeksforGeeks!
Hello, guest!
null!
Conclusion: In this article, we have discussed about the other types which can be used to write code in efficient way. also, we have seen descriptive explanation of each of them and some examples.
Similar Reads
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read
Explain the Tuple Types in TypeScript TypeScript is an open-source object-oriented programming language developed and maintained by Microsoft Corporation. It is a strongly typed language and was first introduced in 2012. TypeScript is a strict superset of JavaScript, which means that anything implemented in JavaScript can be implemented
3 min read
Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
3 min read
Higher-Order Types in TypeScript Higher-order types are among the advanced aspects of Typescript that give priority to types as first-class citizens, similar to higher-order functions of JavaScript that accept a function as an argument or return a function, higher-order types can accept types or return types.These are the following
6 min read
TypeScript Object Types TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters.Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by
3 min read
TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u
2 min read