-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section 1: Exercise 02: Tree-mendous Mapped Types
- Loading branch information
1 parent
71507ea
commit f8a5206
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Exercise 02: Tree-mendous Mapped Types | ||
|
||
In this exercise we're going to use one object type to create | ||
another object type. It's tree-mendously powerful! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
type Tree = { | ||
name: string; | ||
height: number; | ||
age: number; | ||
}; | ||
|
||
type TreeDetails<Type> = { | ||
[Key in keyof Type as `get${Capitalize<string & Key>}`]: () => Type[Key]; | ||
}; | ||
|
||
type OakTree = TreeDetails<Tree>; | ||
|
||
const oakData: Tree = { | ||
name: "Oak", | ||
height: 20, | ||
age: 100, | ||
}; | ||
|
||
let oak: OakTree = { | ||
getName: () => oakData.name, | ||
getHeight: () => oakData.height, | ||
getAge: () => oakData.age, | ||
}; | ||
|
||
console.log(`Name: ${oak.getName()}`); | ||
console.log(`Height: ${oak.getHeight()}`); | ||
console.log(`Age: ${oak.getAge()}`); | ||
|
||
// ---- | ||
|
||
type PartialTree<Type> = { | ||
[Key in keyof Type]+?: Type[Key]; | ||
}; | ||
|
||
let partialOak: PartialTree<Tree> = { | ||
name: "Oak", | ||
}; | ||
|
||
console.log({ partialOak }); | ||
|
||
// ---- | ||
|
||
type TreeWithAge<Type extends number> = `Tree with age ${Type}`; | ||
|
||
type OldTree = TreeWithAge<ReturnType<OakTree["getAge"]>>; | ||
|
||
let oldOak: OldTree = `Tree with age ${oak.getAge()}`; | ||
|
||
console.log(oldOak); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@total-typescript/tsconfig/tsc/no-dom/app" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const oakData = { | ||
name: "Oak", | ||
height: 20, | ||
age: 100, | ||
}; | ||
|
||
let oak = { | ||
getName: () => oakData.name, | ||
getHeight: () => oakData.height, | ||
getAge: () => oakData.age, | ||
}; | ||
|
||
console.log(`Name: ${oak.getName()}`); | ||
console.log(`Height: ${oak.getHeight()}`); | ||
console.log(`Age: ${oak.getAge()}`); |