An interface in TypeScript helps you describe the shape of an object. It tells what properties and methods an object should have, like a blueprint.
Interface declaration syntax in Typescript
interface InterfaceName {
propertyName: propertyType;
methodName(param: paramType): returnType;
}The interface keyword starts the declaration.
Properties and methods inside the interface do not have implementations, only their types.
name and age.interface Person {
name: string;
age: number;
}interface Calculator {
add(a: number, b: number): number;
subtract(a: number, b: number): number;
}brand property and a start method that returns nothing.interface Car {
brand: string;
start(): void;
}This program defines a User interface with two properties and one method. Then it creates an object user that follows this interface and calls the login method.
interface User {
username: string;
email: string;
login(): void;
}
const user: User = {
username: "alice123",
email: "alice@example.com",
login() {
console.log(`${this.username} logged in`);
}
};
user.login();Interfaces only exist during development and are removed when the code runs.
You can extend interfaces to build bigger interfaces from smaller ones.
Interfaces help catch errors early by checking object shapes before running the code.
Interfaces describe the shape of objects with properties and methods.
They help keep your code organized and error-free by defining clear contracts.
Use interface keyword followed by the name and the list of properties/methods.
Practice
interface in TypeScript?Solution
Step 1: Understand the role of interfaces
Interfaces describe how an object should look by specifying its properties and methods.Step 2: Compare with other options
Options A, B, and C describe unrelated concepts like control flow, variables, and modules.Final Answer:
To define the shape of an object with properties and methods -> Option CQuick Check:
Interface = Object shape definition [OK]
- Confusing interfaces with classes
- Thinking interfaces run code
- Mixing interfaces with variables
Person with a string property name?Solution
Step 1: Recall interface declaration syntax
Interfaces use the keywordinterface, followed by the name and curly braces with property declarations using colon syntax.Step 2: Check each option
interface Person { name: string; } uses correct syntax:interface Person { name: string; }. Options B, C, and D have syntax errors like using equals, parentheses, or assignment instead of colon.Final Answer:
interface Person { name: string; } -> Option DQuick Check:
Interface syntax uses colon for properties [OK]
- Using '=' instead of ':' for properties
- Using parentheses instead of braces
- Assigning values inside interface
interface Car {
brand: string;
year: number;
}
const myCar: Car = { brand: "Toyota", year: 2020 };
console.log(myCar.brand + " " + myCar.year);Solution
Step 1: Understand the interface and object assignment
The interfaceCarrequiresbrand(string) andyear(number). The objectmyCarcorrectly provides both.Step 2: Analyze the console.log output
ConcatenatingmyCar.brandandmyCar.yearwith a space results in Toyota 2020.Final Answer:
Toyota 2020 -> Option AQuick Check:
Correct object matches interface = "Toyota 2020" [OK]
- Missing required properties in object
- Confusing property names
- Expecting runtime errors for correct code
interface User {
id: number;
name: string;
}
const user: User = { id: 1 };Solution
Step 1: Check interface properties
The interfaceUserrequires bothid(number) andname(string).Step 2: Check object assignment
The object assigned touseronly hasid, missing the requirednameproperty, causing a TypeScript error.Final Answer:
Missing property 'name' in object assigned to User -> Option BQuick Check:
All interface properties must be present in object [OK]
- Ignoring missing required properties
- Thinking interface names must be lowercase
- Confusing property types
Product with an optional description property?interface Product {
id: number;
name: string;
description?: string;
}Solution
Step 1: Understand optional properties in interfaces
Thedescriptionproperty is optional (marked with?), so it can be omitted.Step 2: Check each object against the interface
{ id: 101, name: "Book" } has requiredid(number) andname(string), no description, which is valid.
{ id: 101, description: "A book" } misses requiredname.
{ name: "Book", description: "A book" } misses requiredid.
{ id: "101", name: "Book" } hasidas string instead of number.Final Answer:
{ id: 101, name: "Book" } -> Option AQuick Check:
Optional properties can be omitted safely [OK]
- Omitting required properties
- Using wrong types for properties
- Confusing optional with required
Ready to prove it?
Pass the test and this pattern moves unknown
