TypeScript Interfaces

What is an Interface?

An interface defines the structure of an object.

It tells TypeScript what properties an object should have.

Loading...
Output:

Why Use Interfaces

  • Enforce structure
  • Improve readability
  • Reduce errors
  • Reuse types

Optional Properties

Use ? to make properties optional.

Loading...
Output:

Readonly Properties

Loading...
Output:

Methods in Interfaces

Interfaces can include functions.

Loading...
Output:

Extending Interfaces

You can extend one interface from another.

Loading...
Output:

Interfaces with Functions

Loading...
Output:

Full Example

Loading...
Output:

Why Interfaces are Important

  • Used in APIs
  • Used in React props
  • Structure large applications
  • Improve maintainability

Common Mistakes

  • Missing required properties
  • Wrong property types
  • Confusing interface and type

Practice

Create an interface for a car with brand, model, and a method that prints details.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic