 Defining Object Shapes with Interfaces
Defining 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.
1234567891011interface 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);
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.
1234567891011interface 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));
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
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 Defining Object Shapes with Interfaces
Defining Object Shapes with Interfaces
Deslize para mostrar o menu
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.
1234567891011interface 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);
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.
1234567891011interface 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));
Obrigado pelo seu feedback!