Challenge 1: Types
- TypeScript course
- Beginner
- Lesson 86/89
- 6 min
Solve a task combining basic types and interfaces.
Challenge 1: Types
In this challenge you will combine basic types and interfaces to design a Person model. You will define an interface with name, age, and email fields, use it, then extend it with an intersection with Address.
Practice Person interface and object
Define a Person interface: name: string, age: number, email: string. Create a person object and print its name field.
Starter code
// Person arayüzü tanımla (isim, yaş, e-posta)
// bir person nesnesi oluştur
// name alanını yazdırSample solution
interface Person {
name: string;
age: number;
email: string;
}
const person: Person = {
name: "Ada",
age: 30,
email: "ada@x.com"
};
console.log(person.name);Expected output
Ada
Practice Combine Person with Address
Define an Address interface: city: string, zip: string. Define the PersonWithAddress type as Person & Address. Create an object and print its city field.
Starter code
// Address arayüzü tanımla (şehir, posta kodu)
// PersonWithAddress = Person & Address
// bir nesne oluştur ve city yazdırSample solution
interface Person {
name: string;
age: number;
email: string;
}
interface Address {
city: string;
zip: string;
}
type PersonWithAddress = Person & Address;
const pa: PersonWithAddress = {
name: "Ada",
age: 30,
email: "ada@x.com",
city: "Ankara",
zip: "06000"
};
console.log(pa.city);Expected output
Ankara
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What type should the age field be in a Person interface?
- string
- number Correct answer
- boolean
- "young" | "old"
Explanation: Age is a numeric value, so it should be of type number. string is for text, boolean for true/false.
2 Complete the email field type
interface Person { name: string; age: number; email: ____; }
interface Person { name: string; age: number; email: string; }
Explanation: Email is text, so the email field should be of type string. number is numeric, boolean is logical, and any removes type safety.
3 Person model parts
| Term | Meaning |
|---|---|
name: string |
Name field |
age: number |
Age field |
email: string |
Email field |
interface Person |
Person model definition |