typescript
/

TypeScript Interfaces

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Interfaces

What is an Interface?

An interface in TypeScript defines the structure of an object, specifying the properties and their types.

Basic Example

TypeScriptRead-only
1
interface User {
  name: string;
  age: number;
}

let user: User = {
  name: 'John',
  age: 30
};

Optional Properties

TypeScriptRead-only
1
interface User {
  name: string;
  age?: number;
}

Readonly Properties

TypeScriptRead-only
1
interface User {
  readonly id: number;
}

Function Interface

TypeScriptRead-only
1
interface AddFn {
  (a: number, b: number): number;
}

Extending Interfaces

TypeScriptRead-only
1
interface Person {
  name: string;
}

interface Employee extends Person {
  salary: number;
}

Interface vs Type

FeatureInterfaceType
ExtendableYesYes
Object ShapeYesYes
Union TypesNoYes

Best Practices

  • Use interfaces for object structures
  • Use meaningful names
  • Keep interfaces small and reusable
  • Use extension for scalability

Common Mistakes

  • Overcomplicating interfaces
  • Using any type inside interfaces
  • Not reusing interfaces
  • Confusing interface with type

Conclusion

Interfaces help enforce structure and improve maintainability in TypeScript applications.

Try it yourself

interface User { name: string; }
const u: User = { name: 'TS' };

Test Your Knowledge

Q1
of 3

Interface defines?

A
Logic
B
Structure
C
Style
D
API
Q2
of 3

Optional property symbol?

A
*
B
?
C
!
D
#
Q3
of 3

Readonly means?

A
Editable
B
Constant
C
Optional
D
Any

Frequently Asked Questions

What is interface?

Defines object structure.

Can interfaces extend?

Yes, they support inheritance.

Interface vs type?

Interfaces define structure, types are more flexible.

Previous

ts rest params

Next

ts type alias

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.