Last active
September 20, 2021 19:37
-
-
Save clarade/7807eed16dd66be0be1c59d16e43d2e3 to your computer and use it in GitHub Desktop.
Small exercise about ts basics and types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare an User interface with typed properties | |
interface User { | |
name: string; | |
age: number; | |
[key: string]: any; | |
} | |
// Declare types for the parameter, and for the function return prettyPrintWilder | |
const prettyPrintWilder = (users: User[]) => { | |
users.map((user: User) => { | |
console.log(`${user.name} is ${user.age} years old`); | |
}); | |
}; | |
// Type wilders | |
const wilders: User[] = []; | |
const user1: User = { name: 'Pierre', age: 23 }; | |
// Adapt user2 so it can't display any error — found a solution creating an additionnal key in User interface, maybe it can be improved | |
const user2: User = { name: 'Paul', age: 31, birthday: '10/02/1990' }; | |
const user3: User = { name: 'Jacques', age: 25 }; | |
wilders.push(user1); | |
wilders.push(user2); | |
wilders.push(user3); | |
prettyPrintWilder(wilders); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment