TypeScript Assertion functions Last Updated : 07 Nov, 2023 Comments Improve Suggest changes Like Article Like Report TypeScript Assertion functions are nothing but the functions that allow us to mainly create the custom type guards that assert the type of a value on our specific criteria. The most suitable use of Assertion Functions is when we are working with the more complex data structures. Syntax:function assert(condition: any, msg?: string): asserts condition { if (!condition) { throw new AssertionError(msg); }}Parameters:assert: This is the name of the function.condition: any, msg?: string: Thi is the condition which will be checked and the message of string type is optional.throw new AssertionError(msg): This will throw the error if the condition gets unsatisfied.Example: In this example, we are defining a function in which we are asserting the value, if the value is other than number it will throw the error else it will give the sum of the values passed in the function. JavaScript // Define an assertion function // which is named as assertFnArray function asssertFnArray(value: unknown): asserts value is number[] { // Checking if the 'value' is not an // array or if it has non-number items if (!Array.isArray(value) || value.some((item) => typeof item !== 'number')) { throw new Error("Assertion failed"); } } // Creating a unknown varibale named arr // arr has the array of numbers from 1-4 const arr: unknown = [1, 2, 3, 4]; // Applying the assertion function to 'arr' asssertFnArray(arr); // Now TypeScript knows that // 'arr' is of type number[] // Performing an operation on // 'arr' (calculating the sum of its elements) console.log(arr.reduce((acc, curr) => acc + curr, 0)); Output:10Example:In this example, we are checking whether the passing object has the defined type of properties or not. If it has other type other that the given type in the function then it will throw the error. JavaScript // Defining a custom type 'Person' with 'name' // as a string and 'age' as a number type Person = { name: string; age: number }; // Creating an assertion function named 'assertPerson' // to make sure an object follows to the 'Person' type function assertPerson(value: unknown): asserts value is Person { // Checking if 'value' is an object, // not null or undefined, and its 'name' // is a string and 'age' is a number if ( typeof value !== 'object' || !value || typeof (value as Person).name !== 'string' || typeof (value as Person).age !== 'number' ) { throw new Error("Assertion failed"); } } // Creating an unknown variable 'obj' // with an object that represents a Geek Data const obj: unknown = { name: "Geek1", age: 23 }; // Applying the assertion function 'assertPerson' to 'obj' assertPerson(obj); // TypeScript knows that 'obj' is of type 'Person' // Printing the output console.log(`Name: ${obj.name}, Age: ${obj.age}`); Output:"Name: Geek1, Age: 23" Comment More info G gauravggeeksforgeeks Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like