TypeScript unknown Function Last Updated : 18 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In TypeScript, the unknown type is used for variables whose types aren't known in advance. It ensures type safety by requiring explicit type checks or assertions before usage, preventing arbitrary operations, and promoting safer handling compared to the `any` type.Syntaxfunction gfg(input: unknown): void { }Parametersgfg: This represents the name of the function.input: This represents the name of the parameter passed to the function.unknown: This represents the type of parameter passed.void: This represents the return type of the function.Example 1: Greeting Function with unknown TypeThis example demonstrates a function that accepts an unknown parameter and handles different types. Strings get personalized greetings, null is a special case, and other types receive a generic greeting. TypeScript function greet(name: unknown): void { if (typeof name === "string") { console.log(`Hello, ${name}!`); } else if (name === null) { console.log("Hello, guest!"); } else { console.log("Hello, there!"); } } greet("GeeksforGeeks"); greet(null); greet(42); Output:Hello, GeeksforGeeks!Hello, guest!Hello, there!Example 2: Calculating Area with unknown TypeThis example demonstrates a function that handles various shapes or values by type checking. It calculates circle or rectangle areas based on the input type. If unrecognized, it returns undefined. TypeScript type Circle = { radius: number }; type Rectangle = { width: number, height: number }; function calculateArea(shape: unknown): number | undefined { if (typeof shape === "object" && shape !== null) { if ("radius" in shape) { return Math.PI * (shape as Circle).radius ** 2; } else if ("width" in shape && "height" in shape) { return (shape as Rectangle).width * (shape as Rectangle).height; } } return undefined; } console.log(calculateArea({ radius: 5 })); console.log(calculateArea({ width: 4, height: 6 })); console.log(calculateArea("random shape")); Output:78.53981633974483 24 undefinedConclusionThe unknown type in TypeScript enhances type safety by requiring explicit type checks or type assertions before using the variable. It prevents arbitrary operations on values of uncertain types, promoting safer and more robust code. Comment More infoAdvertise with us Next Article TypeScript unknown Function N nikitamehrotra99 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript void Function Void functions in TypeScript are functions that do not return a value. They perform actions or computations without producing a result that needs to be captured. Commonly used for side effects like logging, modifying external state, or triggering asynchronous operations, they enhance code clarity.Sy 3 min read TypeScript Functions Type TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error 6 min read TypeScript Generic Functions TypeScript generic functions allow you to create functions that work with various types while maintaining type safety. By using type parameters, defined within angle brackets (<T>), generics enable functions to operate on different data types without losing the benefits of TypeScript's type-ch 3 min read TypeScript Function Overloads TypeScript function overloads enable defining multiple signatures for a single function, allowing it to handle various parameter types or counts.Enhances type safety by ensuring correct argument handling.Improves code flexibility and readability.JavaScriptfunction greet(person: string): string; func 3 min read What is the Function type in TypeScript ? TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance. It uses type 3 min read TypeScript Anonymous Functions Type In TypeScript, an Anonymous Function Type defines a function without a specific name, specifying parameters and return types. This allows for flexible and reusable function definitions, enabling the assignment of functions to variables and the use of type annotations for parameters and return values 3 min read TypeScript Function Type Expressions In this article, we are going to learn about TypeScript Function Type Expressions in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a function type expression represents the type of a function, including its parameter types 3 min read PHP gettype() Function The PHP gettype() function returns the type of a variable as a string. It identifies the variable's data type, such as string, integer, array, boolean, etc., allowing developers to check and handle different data types dynamically.Syntax:string gettype ( $var )Parameter: This function accepts a sing 2 min read PHP | imagetypes() Function The imagetypes() function is an inbuilt function in PHP which is used to return the image types supported by the PHP inbuilt installed library. Syntax: int imagetypes( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the bit-field corresponding to t 1 min read TypeScript Assignability of Functions In this article, we will explore the concept of assignability of functions in TypeScript. Specifically, we will discuss how functions with a return type of void can be assigned to function types with other return types, including those that return values.Understanding AssignabilityIn TypeScript, fun 3 min read Like