TypeScript Non-null Assertion Operator (Postfix !) Type Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report TypeScript non-null assertion operator (!) is used to assert that a value is non-null and non-undefined, even when TypeScript's strict null checks are enabled. This operator tells TypeScript to treat the expression as if it's guaranteed to have a value, eliminating compile-time null and undefined checks. Syntaxconst nonNullValue: Type = value!;ParametersnonNullValue is the variable where you want to store the non-null value.Type is the expected type of the value.value is the variable or expression that you're asserting to be non-null and non-undefined using the! operator.Example 1: In this example, We have a function greetUser() that takes a name parameter, which can be a string or null.Inside the function, we use the non-null assertion operator name! to assert that the name is not null or undefined.We then use formattedName to hold the non-null value of the name, allowing us to treat it as a string.We log a greeting message that includes the user's name if provided, or "GeeksforGeeks" if the name is null. JavaScript function greetUser(name: string | null) { // Using the non-null assertion operator // to ensure 'name' is not null const formattedName: string = name!; console.log( `Hello, ${formattedName || 'GeeksforGeeks'}!`); } greetUser("Akshit"); // Output: Hello, Akshit! greetUser(null); // Output: Hello, GeeksforGeeks! Output: Example 2: In this example:We define a User type representing a user object with potentially nullable properties. We create a user object with a name property (non-nullable) and an email property (nullable). We use the non-null assertion operator ! to assert that the email property is non-null when accessing it. This is done because TypeScript considers user.email to be potentially undefined due to the email? property definition in the User type. We then log both the userName and userEmail variables. TypeScript will not raise any errors related to nullability because of the non-null assertion operator. JavaScript type User = { name: string; email?: string; }; // Create a user object with nullable // properties const user: User = { name: "GeeksforGeeks", // Email is intentionally omitted }; // Use the non-null assertion operator // to access properties // No assertion needed, name is guaranteed const userName: string = user.name; // Use ! to assert that email is non-null const userEmail: string = user.email!; console.log(`User Name: ${userName}`); console.log(`User Email: ${userEmail}`); Output: Reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator Comment More infoAdvertise with us Next Article TypeScript Non-null Assertion Operator (Postfix !) Type A akshitsaxenaa09 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads Explain Type assertions in TypeScript In TypeScript, type assertions allow developers to override the compiler's inferred type, informing it of the specific type of a value. Type assertions are purely compile-time constructs and do not alter the runtime behavior of the code. They are particularly useful when interfacing with APIs or thi 3 min read TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As 2 min read How to fix "object is possibly null" in TypeScript ? The "object is possibly null" error in TypeScript occurs when the compiler detects a potential null or undefined value for an object or variable. This error is important for ensuring type safety and preventing runtime errors related to null or undefined values. There are several approaches to fix th 4 min read TypeScript Assertion functions 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 asse 3 min read How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th 3 min read 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 TypeScript Truthiness Narrowing Type In this article, we are going to learn about Truthiness narrowing Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, truthiness narrowing is a concept that allows you to narrow down the type of a variable based on its t 3 min read What is Type Predicates in Typescript ? In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narro 3 min read How to enforce strict null checks in TypeScript ? In Typescript to enforce strict null checks in tsconfig.json file, we need to enable "strictNullChecks" to true. When "strictNullChecks" is false, the language generally ignores variables of type null and undefined. If null and undefined is used in places where a definite value is expected, it raise 2 min read TypeScript Equality Narrowing Type In this article, we are going to learn about Equality narrowing Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, "equality narrowing" refers to the process of narrowing the type of a variable based on equality checks 4 min read Like