-
Notifications
You must be signed in to change notification settings - Fork 136
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
a0c3d63
commit aa818a2
Showing
9 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/075-designing-your-types/177-domain-modelling-in-typescript.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,7 @@ | ||
// Modelling your domain in TypeScript | ||
|
||
// Think of the entities in your domain as individual types | ||
|
||
// Compose those types together | ||
|
||
// Think about types as containers for other types |
Empty file.
Empty file.
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 PromiseFunc = (input: any) => Promise<any>; | ||
|
||
type Example1 = PromiseFunc<string, string>; | ||
|
||
type test1 = Expect<Equal<Example1, (input: string) => Promise<string>>>; | ||
|
||
type Example2 = PromiseFunc<boolean, number>; | ||
|
||
type test2 = Expect<Equal<Example2, (input: boolean) => Promise<number>>>; |
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 PromiseFunc<Input, Output> = (input: Input) => Promise<Output>; | ||
|
||
type Example1 = PromiseFunc<string, string>; | ||
|
||
type test1 = Expect<Equal<Example1, (input: string) => Promise<string>>>; | ||
|
||
type Example2 = PromiseFunc<boolean, number>; | ||
|
||
type test2 = Expect<Equal<Example2, (input: boolean) => Promise<number>>>; |
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 @@ | ||
type Result<TResult, TError = Error> = | ||
| { | ||
success: true; | ||
data: TResult; | ||
} | ||
| { | ||
success: false; | ||
error: TError; | ||
}; | ||
|
||
type BadExample = Result< | ||
{ id: string }, | ||
// @ts-expect-error Should be an object with a message property | ||
string | ||
>; | ||
|
||
type GoodExample = Result<{ id: string }, TypeError>; | ||
type GoodExample2 = Result<{ id: string }, { message: string; code: number }>; | ||
type GoodExample3 = Result<{ id: string }, { message: string }>; | ||
type GoodExample4 = Result<{ id: 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 @@ | ||
type Result<TResult, TError extends { message: string } = Error> = | ||
| { | ||
success: true; | ||
data: TResult; | ||
} | ||
| { | ||
success: false; | ||
error: TError; | ||
}; | ||
|
||
type BadExample = Result< | ||
{ id: string }, | ||
// @ts-expect-error Should be an object with a message property | ||
string | ||
>; | ||
|
||
type GoodExample = Result<{ id: string }, TypeError>; | ||
type GoodExample2 = Result<{ id: string }, { message: string; code: number }>; | ||
type GoodExample3 = Result<{ id: string }, { message: string }>; | ||
type GoodExample4 = Result<{ id: 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,15 @@ | ||
type StrictOmit<T, K> = Omit<T, K>; | ||
|
||
type ShouldFail = StrictOmit< | ||
{ a: string }, | ||
// @ts-expect-error | ||
"b" | ||
>; | ||
|
||
type tests = [ | ||
Expect<Equal<StrictOmit<{ a: string; b: number }, "b">, { a: string }>>, | ||
Expect<Equal<StrictOmit<{ a: string; b: number }, "b" | "a">, {}>>, | ||
Expect< | ||
Equal<StrictOmit<{ a: string; b: number }, never>, { a: string; b: number }> | ||
>, | ||
]; |
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 StrictOmit<T, K extends keyof T> = Omit<T, K>; | ||
|
||
type ShouldFail = StrictOmit< | ||
{ a: string }, | ||
// @ts-expect-error | ||
"b" | ||
>; | ||
|
||
type tests = [ | ||
Expect<Equal<StrictOmit<{ a: string; b: number }, "b">, { a: string }>>, | ||
Expect<Equal<StrictOmit<{ a: string; b: number }, "b" | "a">, {}>>, | ||
Expect< | ||
Equal<StrictOmit<{ a: string; b: number }, never>, { a: string; b: number }> | ||
>, | ||
]; |