TypeScript Object Type Optional Properties Last Updated : 18 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In TypeScript, optional properties are denoted using the ? modifier after the property name in the object type definition. This means that when an object is created, the optional property can either be provided or omitted.Syntax:type TypeName = { propertyName: PropertyType; optionalPropertyName?: OptionalPropertyType; };Where:TypeName: The name of the object type you're defining.propertyName: A required property of the object, with its type specified.optionalPropertyName?: An optional property of the object, denoted by the ? symbol after the property name. It can be omitted when creating objects of this type.OptionalPropertyType: Represents the type of the optional property.Example 1: In this example, We define a Course object type with one optional property: price. We create two objects of type Course, Course1 with just the name property and Course2 with both name and price.We safely access the optional property price using conditional checks to ensure its existence before displaying its value or indicating its absence. JavaScript // Define an object type with an optional property type Course = { name: string; // Optional property price?: number; }; // Create an object using the defined type const Course1: Course = { name: "Java", }; const Course2: Course = { name: "C++", price: 150.00, }; // Accessing the optional property safely if (Course1.price !== undefined) { console.log(`${Course1.name} costs $${Course1.price}`); } else { console.log(`${Course1.name} price is not specified.`); } if (Course2.price !== undefined) { console.log(`${Course2.name} costs Rs${Course2.price}`); } else { console.log(`${Course2.name} price is not specified.`); } Output:Example 2: In this example,We have the strictNullChecks option enabled, which ensures that you handle undefined and null values appropriately.We define a Person object type with two optional properties: age, which can be undefined, and address, which can be null or undefined. JavaScript // Enable strictNullChecks in tsconfig.json // or via command line compiler flags: // "strictNullChecks": true // Define an object type with optional properties type Person = { name: string; // Optional property that can be undefined age?: number; // Optional property that can be null or undefined address?: string | null; }; // Create an object using the defined type const person1: Person = { name: "Akshit", age: 25, }; const person2: Person = { name: "Bob", // age is not provided, so it's undefined by default // address is explicitly set to null address: null, }; // Accessing optional properties safely with strictNullChecks if (person1.age !== undefined) { console.log(`${person1.name}'s age is ${person1.age}`); } else { console.log(`${person1.name} did not provide an age.`); } if (person2.address !== undefined && person2.address !== null) { console.log(`${person2.name} lives at ${person2.address}`); } else { console.log(`${person2.name} did not provide an address.`); } Output:Conclusion: In this article, we explored the concept of optional properties in TypeScript. We discussed their syntax and provided practical examples to illustrate their usage. Optional properties allow for greater flexibility when defining object types, enabling the creation of objects with varying sets of properties. This feature is particularly useful in scenarios where not all properties are always required, making the code more robust and adaptable. Comment More infoAdvertise with us Next Article Spring Boot Interview Questions and Answers 21mcsrltd Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like