-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f40f234
commit 65e21f8
Showing
67 changed files
with
533 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
src/020-sets-of-types/057-extend-object-using-intersections.problem.ts
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,38 @@ | ||
type User = { | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
type Product = { | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
}; | ||
|
||
type tests = [ | ||
Expect< | ||
Extends< | ||
User, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Extends< | ||
Product, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
} | ||
> | ||
>, | ||
]; |
39 changes: 39 additions & 0 deletions
39
src/020-sets-of-types/057-extend-object-using-intersections.solution.ts
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,39 @@ | ||
type BaseEntity = { | ||
id: string; | ||
createdAt: Date; | ||
}; | ||
|
||
type User = { | ||
name: string; | ||
email: string; | ||
} & BaseEntity; | ||
|
||
type Product = { | ||
name: string; | ||
price: number; | ||
} & BaseEntity; | ||
|
||
type tests = [ | ||
Expect< | ||
Extends< | ||
User, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Extends< | ||
Product, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
} | ||
> | ||
>, | ||
]; |
39 changes: 39 additions & 0 deletions
39
src/020-sets-of-types/058-extend-object-using-interfaces.problem.ts
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,39 @@ | ||
type BaseEntity = { | ||
id: string; | ||
createdAt: Date; | ||
}; | ||
|
||
type User = { | ||
name: string; | ||
email: string; | ||
} & BaseEntity; | ||
|
||
type Product = { | ||
name: string; | ||
price: number; | ||
} & BaseEntity; | ||
|
||
type tests = [ | ||
Expect< | ||
Extends< | ||
User, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Extends< | ||
Product, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
} | ||
> | ||
>, | ||
]; |
39 changes: 39 additions & 0 deletions
39
src/020-sets-of-types/058-extend-object-using-interfaces.solution.1.ts
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,39 @@ | ||
interface BaseEntity { | ||
id: string; | ||
createdAt: Date; | ||
} | ||
|
||
interface User extends BaseEntity { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
interface Product extends BaseEntity { | ||
name: string; | ||
price: number; | ||
} | ||
|
||
type tests = [ | ||
Expect< | ||
Extends< | ||
User, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Extends< | ||
Product, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
} | ||
> | ||
>, | ||
]; |
42 changes: 42 additions & 0 deletions
42
src/020-sets-of-types/058-extend-object-using-interfaces.solution.2.ts
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,42 @@ | ||
interface WithId { | ||
id: string; | ||
} | ||
|
||
interface WithCreatedAt { | ||
createdAt: Date; | ||
} | ||
|
||
interface User extends WithId, WithCreatedAt { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
interface Product extends WithId, WithCreatedAt { | ||
name: string; | ||
price: number; | ||
} | ||
|
||
type tests = [ | ||
Expect< | ||
Extends< | ||
User, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
email: string; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Extends< | ||
Product, | ||
{ | ||
id: string; | ||
createdAt: Date; | ||
name: string; | ||
price: number; | ||
} | ||
> | ||
>, | ||
]; |
1 change: 1 addition & 0 deletions
1
src/020-sets-of-types/059-compare-between-intersections-and-interfaces.explainer.ts
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 @@ | ||
// https://github.com/microsoft/TypeScript/wiki/Performance#preferring-interfaces-over-intersections |
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,5 @@ | ||
const scores = {}; | ||
|
||
scores.math = 95; | ||
scores.english = 90; | ||
scores.science = 85; |
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,9 @@ | ||
type Scores = { | ||
[key: string]: number; | ||
}; | ||
|
||
const scores: Scores = {}; | ||
|
||
scores.math = 95; | ||
scores.english = 90; | ||
scores.science = 85; |
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,9 @@ | ||
interface Scores { | ||
[key: string]: number; | ||
} | ||
|
||
const scores: Scores = {}; | ||
|
||
scores.math = 95; | ||
scores.english = 90; | ||
scores.science = 85; |
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,7 @@ | ||
const scores: { | ||
[key: string]: number; | ||
} = {}; | ||
|
||
scores.math = 95; | ||
scores.english = 90; | ||
scores.science = 85; |
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,5 @@ | ||
const scores: Record<string, number> = {}; | ||
|
||
scores.math = 95; | ||
scores.english = 90; | ||
scores.science = 85; |
16 changes: 16 additions & 0 deletions
16
src/020-sets-of-types/061-index-signatures-with-defined-keys.problem.ts
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,16 @@ | ||
interface Scores { | ||
[key: string]: number; | ||
math: number; | ||
english: number; | ||
science: number; | ||
} | ||
|
||
// @ts-expect-error science is missing! | ||
const scores: Scores = { | ||
math: 95, | ||
english: 90, | ||
}; | ||
|
||
scores.athletics = 100; | ||
scores.french = 75; | ||
scores.spanish = 70; |
16 changes: 16 additions & 0 deletions
16
src/020-sets-of-types/061-index-signatures-with-defined-keys.solution.1.ts
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,16 @@ | ||
interface Scores { | ||
[key: string]: number; | ||
math: number; | ||
english: number; | ||
science: number; | ||
} | ||
|
||
// @ts-expect-error science is missing! | ||
const scores: Scores = { | ||
math: 95, | ||
english: 90, | ||
}; | ||
|
||
scores.athletics = 100; | ||
scores.french = 75; | ||
scores.spanish = 70; |
19 changes: 19 additions & 0 deletions
19
src/020-sets-of-types/061-index-signatures-with-defined-keys.solution.2.ts
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,19 @@ | ||
interface RequiredScores { | ||
math: number; | ||
english: number; | ||
science: number; | ||
} | ||
|
||
interface Scores extends RequiredScores { | ||
[key: string]: number; | ||
} | ||
|
||
// @ts-expect-error science is missing! | ||
const scores: Scores = { | ||
math: 95, | ||
english: 90, | ||
}; | ||
|
||
scores.athletics = 100; | ||
scores.french = 75; | ||
scores.spanish = 70; |
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions
19
src/020-sets-of-types/063-declaration-merging-of-interfaces.problem.ts
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,19 @@ | ||
interface Logger { | ||
log(message: string, level: number): void; | ||
} | ||
|
||
interface Logger { | ||
log(message: string): void; | ||
} | ||
|
||
const myLogger: Logger = { | ||
log: (message: string) => { | ||
console.log(message); | ||
}, | ||
}; | ||
|
||
myLogger.log( | ||
"My message", | ||
// @ts-expect-error Level is NOT needed | ||
123, | ||
); |
15 changes: 15 additions & 0 deletions
15
src/020-sets-of-types/063-declaration-merging-of-interfaces.solution.ts
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 @@ | ||
type Logger = { | ||
log(message: string): void; | ||
}; | ||
|
||
const myLogger: Logger = { | ||
log: (message: string) => { | ||
console.log(message); | ||
}, | ||
}; | ||
|
||
myLogger.log( | ||
"My message", | ||
// @ts-expect-error Level is NOT needed | ||
123, | ||
); |
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,18 @@ | ||
interface User { | ||
id: string; | ||
name: string; | ||
email: string; | ||
role: string; | ||
} | ||
|
||
const fetchUser = async () => { | ||
const response = await fetch("/api/user"); | ||
const user = await response.json(); | ||
return user; | ||
}; | ||
|
||
const example = async () => { | ||
const user = await fetchUser(); | ||
|
||
type test = Expect<Equal<typeof user, { name: string; email: string }>>; | ||
}; |
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,21 @@ | ||
interface NameAndEmail { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
interface User extends NameAndEmail { | ||
id: string; | ||
role: string; | ||
} | ||
|
||
const fetchUser = async (): Promise<NameAndEmail> => { | ||
const response = await fetch("/api/user"); | ||
const user = await response.json(); | ||
return user; | ||
}; | ||
|
||
const example = async () => { | ||
const user = await fetchUser(); | ||
|
||
type test = Expect<Equal<typeof user, { name: string; email: string }>>; | ||
}; |
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,20 @@ | ||
interface User { | ||
id: string; | ||
name: string; | ||
email: string; | ||
role: string; | ||
} | ||
|
||
type OnlyNameAndEmail = Pick<User, "name" | "email">; | ||
|
||
const fetchUser = async (): Promise<OnlyNameAndEmail> => { | ||
const response = await fetch("/api/user"); | ||
const user = await response.json(); | ||
return user; | ||
}; | ||
|
||
const example = async () => { | ||
const user = await fetchUser(); | ||
|
||
type test = Expect<Equal<typeof user, { name: string; email: string }>>; | ||
}; |
Oops, something went wrong.