Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Defining Object Shapes with Interfaces | Understanding Interfaces
Working with Interfaces and Generics in TypeScript

bookDefining Object Shapes with Interfaces

Interfaces in TypeScript are a powerful feature that let you define the structure that an object should have. An interface acts as a contract for object shapes, specifying which properties and types are required. This helps you write code that is both safer and easier to understand, because TypeScript can check that objects used in your code match the expected structure. Using interfaces makes it much easier to catch mistakes early, and they provide clear documentation for anyone reading your code.

1234567891011
interface User { name: string; age: number; } function printUserInfo(user: User): void { console.log(`Name: ${user.name}, Age: ${user.age}`); } const user1 = { name: "Alice", age: 30 }; printUserInfo(user1);
copy

In this example, the User interface defines two properties: name, which must be a string, and age, which must be a number. The function printUserInfo takes an argument of type User, so TypeScript will enforce that any object passed to this function includes both a name and an age property with the correct types. If you try to pass an object that is missing one of these properties, or if the types don't match, TypeScript will give you an error before you even run your code. This ensures that your functions always receive objects in the shape they expect.

1234567891011
interface User { name: string; age: number; } function greetUser(user: User): string { return `Hello, ${user.name}! You are ${user.age} years old.`; } const user2 = { name: "Bob", age: 25 }; console.log(greetUser(user2));
copy
question mark

What is the primary purpose of an interface in TypeScript when working with object shapes

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain how interfaces are different from types in TypeScript?

What happens if I try to pass an object with extra properties to a function expecting a User interface?

Can interfaces be extended or combined in TypeScript?

Awesome!

Completion rate improved to 9.09

bookDefining Object Shapes with Interfaces

Pyyhkäise näyttääksesi valikon

Interfaces in TypeScript are a powerful feature that let you define the structure that an object should have. An interface acts as a contract for object shapes, specifying which properties and types are required. This helps you write code that is both safer and easier to understand, because TypeScript can check that objects used in your code match the expected structure. Using interfaces makes it much easier to catch mistakes early, and they provide clear documentation for anyone reading your code.

1234567891011
interface User { name: string; age: number; } function printUserInfo(user: User): void { console.log(`Name: ${user.name}, Age: ${user.age}`); } const user1 = { name: "Alice", age: 30 }; printUserInfo(user1);
copy

In this example, the User interface defines two properties: name, which must be a string, and age, which must be a number. The function printUserInfo takes an argument of type User, so TypeScript will enforce that any object passed to this function includes both a name and an age property with the correct types. If you try to pass an object that is missing one of these properties, or if the types don't match, TypeScript will give you an error before you even run your code. This ensures that your functions always receive objects in the shape they expect.

1234567891011
interface User { name: string; age: number; } function greetUser(user: User): string { return `Hello, ${user.name}! You are ${user.age} years old.`; } const user2 = { name: "Bob", age: 25 }; console.log(greetUser(user2));
copy
question mark

What is the primary purpose of an interface in TypeScript when working with object shapes

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 1
some-alt