Lesson 32 of 60 – Interface Properties
53%

Interface Properties in TypeScript

Interface properties define the data that an object should contain. In TypeScript, each property can have a specific type such as string, number, boolean, arrays, objects, or functions.

Note: Interface properties provide a clear structure for objects and help TypeScript detect incorrect or missing property values.

1. What are Interface Properties?

Interface properties describe the fields that an object should contain.

interface Student {
    name: string;
    age: number;
}

Here, name and age are interface properties.

2. Basic Property Syntax

The basic syntax of an interface property is:

propertyName: type;

Example:

interface User {
    name: string;
}

3. String Property

A property can be declared as a string.

interface User {
    name: string;
}

const user: User = {
    name: "Amit"
};

The name property must contain a string.

4. Number Property

A property can contain a number.

interface Student {
    age: number;
}

const student: Student = {
    age: 20
};

5. Boolean Property

Boolean properties accept true or false.

interface User {
    active: boolean;
}

const user: User = {
    active: true
};

6. Multiple Properties

An interface can contain multiple properties.

interface Student {
    id: number;
    name: string;
    age: number;
    course: string;
}

const student: Student = {
    id: 101,
    name: "Amit",
    age: 20,
    course: "TypeScript"
};

7. Accessing Interface Properties

Interface properties are accessed using dot notation.

console.log(
    student.name
);

console.log(
    student.age
);

console.log(
    student.course
);

8. Updating Properties

Normal interface properties can be changed after the object is created.

interface Student {
    name: string;
    age: number;
}

let student: Student = {
    name: "Amit",
    age: 20
};

student.name = "Rahul";
student.age = 21;

9. Array Property

An interface property can be an array.

interface Student {
    name: string;
    marks: number[];
}

const student: Student = {
    name: "Amit",
    marks: [
        80,
        75,
        90
    ]
};

10. String Array Property

An interface can contain an array of strings.

interface Developer {
    name: string;
    skills: string[];
}

const developer: Developer = {
    name: "Amit",
    skills: [
        "HTML",
        "CSS",
        "TypeScript"
    ]
};

11. Optional Property

Use the question mark ? to make a property optional.

interface User {
    name: string;
    email: string;
    phone?: string;
}

const user: User = {
    name: "Amit",
    email: "amit@example.com"
};

The phone property is not required.

12. Required and Optional Properties

Required properties must be provided, while optional properties may be omitted.

interface User {
    name: string;
    email: string;
    phone?: string;
    city?: string;
}

const user: User = {
    name: "Rahul",
    email: "rahul@example.com"
};

13. Readonly Property

The readonly keyword prevents a property from being reassigned.

interface Student {
    readonly id: number;
    name: string;
}

const student: Student = {
    id: 101,
    name: "Amit"
};

student.name = "Rahul";

// student.id = 102;

14. Union Type Property

An interface property can accept more than one type using a union.

interface User {
    id: number | string;
    name: string;
}

const user: User = {
    id: 101,
    name: "Amit"
};

user.id = "USR101";

15. Literal Type Property

A property can be restricted to specific literal values.

interface User {
    name: string;
    role: "admin" | "student";
}

const user: User = {
    name: "Amit",
    role: "student"
};

The role property can only contain the specified values.

16. Nested Object Property

An interface property can itself be another object.

interface Address {
    city: string;
    pin: number;
}

interface Student {
    name: string;
    address: Address;
}

const student: Student = {
    name: "Amit",
    address: {
        city: "Aurangabad",
        pin: 824101
    }
};

17. Object Property Directly Inside Interface

A nested object can also be written directly inside the interface.

interface Student {
    name: string;

    address: {
        city: string;
        pin: number;
    };
}

const student: Student = {
    name: "Rahul",
    address: {
        city: "Patna",
        pin: 800001
    }
};

18. Date Property

Interface properties can use built-in types such as Date.

interface Student {
    name: string;
    admissionDate: Date;
}

const student: Student = {
    name: "Amit",
    admissionDate: new Date()
};

19. Function Property

An interface property can be a function.

interface User {
    name: string;

    greet: () => void;
}

const user: User = {
    name: "Amit",

    greet: () => {
        console.log("Hello");
    }
};

user.greet();

20. Function Property with Parameters

Function properties can accept parameters and return values.

interface Calculator {
    add: (
        a: number,
        b: number
    ) => number;
}

const calculator: Calculator = {

    add: (
        a,
        b
    ) => a + b

};

console.log(
    calculator.add(10, 20)
);

21. Interface Property with Union Array

A property can be an array containing values from more than one type.

interface Data {
    values: (string | number)[];
}

const data: Data = {
    values: [
        "Amit",
        100,
        "Rahul",
        200
    ]
};

22. Interface Property with Tuple

A property can also use a tuple type.

interface Student {
    name: string;
    result: [number, string];
}

const student: Student = {
    name: "Amit",
    result: [
        85,
        "Pass"
    ]
};

The tuple contains a number followed by a string.

23. Interface Properties in React

Interface properties are commonly used to define React component props.

interface UserProps {
    name: string;
    age: number;
    active: boolean;
}

function UserCard(
    props: UserProps
) {

    return (
        <div>
            {props.name}
            {props.age}
        </div>
    );
}

Each prop has a clearly defined type.

24. Interface Properties in React Native

React Native components can use interfaces to define their props.

interface UserProps {
    name: string;
    age: number;
}

const UserCard = (
    props: UserProps
) => {

    return (
        <Text>
            {props.name}
        </Text>
    );
};

25. Interface Properties for API Data

Interfaces are useful for describing the properties expected from an API response.

interface Product {
    id: number;
    title: string;
    price: number;
    available: boolean;
}

const product: Product = {
    id: 101,
    title: "Mobile Phone",
    price: 15000,
    available: true
};

26. Interface Property Inheritance

An interface can inherit properties from another interface.

interface Person {
    name: string;
}

interface Student extends Person {
    age: number;
    course: string;
}

const student: Student = {
    name: "Amit",
    age: 20,
    course: "TypeScript"
};

The Student interface contains all properties from Person.

27. Common Interface Property Mistake

A common mistake is assigning a value with the wrong type.

interface Student {
    name: string;
    age: number;
}

const student: Student = {
    name: "Amit",
    age: 20
};

// Incorrect:
// student.age = "Twenty";

The age property is declared as a number, so a string cannot be assigned to it.

28. Required Properties Must Be Provided

All required interface properties must be provided when creating an object.

interface Student {
    name: string;
    age: number;
}

const student: Student = {
    name: "Amit",
    age: 20
};

If age is required, it cannot simply be omitted. Make it optional with age? if it is not always needed.

29. Complete Practical Example

This example combines several interface property types in a student object.

interface Student {
    readonly id: number;
    name: string;
    age: number;
    course: string;
    marks: number[];
    phone?: string;

    address: {
        city: string;
        pin: number;
    };
}

const student: Student = {

    id: 101,

    name: "Amit",

    age: 20,

    course: "TypeScript",

    marks: [
        80,
        75,
        90
    ],

    phone: "9876543210",

    address: {
        city: "Aurangabad",
        pin: 824101
    }

};

console.log(
    student.name
);

console.log(
    student.marks
);

console.log(
    student.address.city
);

30. Interface Properties Summary

Interface properties define the structure and types of data that an object should contain.

interface Student {
    readonly id: number;
    name: string;
    age: number;
    course: string;
    marks: number[];
    phone?: string;
}

Properties can be required, optional, readonly, nested, arrays, functions, unions, tuples, and many other types.

📌 Key Points

  • Interface properties define the structure of an object.
  • Each property can have its own TypeScript type.
  • Properties can be strings, numbers, booleans, arrays, objects, and functions.
  • Use ? for optional properties.
  • Use readonly for properties that should not be reassigned.
  • Properties can use union and literal types.
  • Interfaces can contain nested objects.
  • Interfaces can describe React and React Native props.
  • Interfaces are useful for API response structures.
  • Correct property types improve type safety and code readability.

🧠 Quick Quiz

Question: Which symbol makes an interface property optional?