-
Notifications
You must be signed in to change notification settings - Fork 137
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
778199c
commit a2a192b
Showing
9 changed files
with
233 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,62 @@ | ||
type ErrorShape = { | ||
error: { | ||
message: string; | ||
}; | ||
}; | ||
|
||
type UserDataShape = | ||
| { | ||
data: { | ||
id: string; | ||
name: string; | ||
email: string; | ||
}; | ||
} | ||
| ErrorShape; | ||
|
||
type PostDataShape = | ||
| { | ||
data: { | ||
id: string; | ||
title: string; | ||
body: string; | ||
}; | ||
} | ||
| ErrorShape; | ||
|
||
type tests = [ | ||
Expect< | ||
Equal< | ||
UserDataShape, | ||
| { | ||
data: { | ||
id: string; | ||
name: string; | ||
email: string; | ||
}; | ||
} | ||
| { | ||
error: { | ||
message: string; | ||
}; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Equal< | ||
PostDataShape, | ||
| { | ||
data: { | ||
id: string; | ||
title: string; | ||
body: string; | ||
}; | ||
} | ||
| { | ||
error: { | ||
message: 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,60 @@ | ||
type ErrorShape = { | ||
error: { | ||
message: string; | ||
}; | ||
}; | ||
|
||
type DataShape<T> = | ||
| { | ||
data: T; | ||
} | ||
| ErrorShape; | ||
|
||
type UserDataShape = DataShape<{ | ||
id: string; | ||
name: string; | ||
email: string; | ||
}>; | ||
|
||
type PostDataShape = DataShape<{ | ||
id: string; | ||
title: string; | ||
body: string; | ||
}>; | ||
|
||
type tests = [ | ||
Expect< | ||
Equal< | ||
UserDataShape, | ||
| { | ||
data: { | ||
id: string; | ||
name: string; | ||
email: string; | ||
}; | ||
} | ||
| { | ||
error: { | ||
message: string; | ||
}; | ||
} | ||
> | ||
>, | ||
Expect< | ||
Equal< | ||
PostDataShape, | ||
| { | ||
data: { | ||
id: string; | ||
title: string; | ||
body: string; | ||
}; | ||
} | ||
| { | ||
error: { | ||
message: string; | ||
}; | ||
} | ||
> | ||
>, | ||
]; |
File renamed without changes.
File renamed without changes.
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,37 @@ | ||
type Result<TResult, TError> = | ||
| { | ||
success: true; | ||
data: TResult; | ||
} | ||
| { | ||
success: false; | ||
error: TError; | ||
}; | ||
|
||
const createRandomNumber = (): Result<number, Error> => { | ||
const num = Math.random(); | ||
|
||
if (num > 0.5) { | ||
return { | ||
success: true, | ||
data: 123, | ||
}; | ||
} | ||
|
||
return { | ||
success: false, | ||
error: new Error("Something went wrong"), | ||
}; | ||
}; | ||
|
||
const result = createRandomNumber(); | ||
|
||
if (result.success) { | ||
console.log(result.data); | ||
|
||
type test = Expect<Equal<typeof result.data, number>>; | ||
} else { | ||
console.error(result.error); | ||
|
||
type test = Expect<Equal<typeof result.error, Error>>; | ||
} |
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,37 @@ | ||
type Result<TResult, TError> = | ||
| { | ||
success: true; | ||
data: TResult; | ||
} | ||
| { | ||
success: false; | ||
error: TError; | ||
}; | ||
|
||
const createRandomNumber = (): Result<number> => { | ||
const num = Math.random(); | ||
|
||
if (num > 0.5) { | ||
return { | ||
success: true, | ||
data: 123, | ||
}; | ||
} | ||
|
||
return { | ||
success: false, | ||
error: new Error("Something went wrong"), | ||
}; | ||
}; | ||
|
||
const result = createRandomNumber(); | ||
|
||
if (result.success) { | ||
console.log(result.data); | ||
|
||
type test = Expect<Equal<typeof result.data, number>>; | ||
} else { | ||
console.error(result.error); | ||
|
||
type test = Expect<Equal<typeof result.error, Error>>; | ||
} |
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,37 @@ | ||
type Result<TResult, TError = Error> = | ||
| { | ||
success: true; | ||
data: TResult; | ||
} | ||
| { | ||
success: false; | ||
error: TError; | ||
}; | ||
|
||
const createRandomNumber = (): Result<number> => { | ||
const num = Math.random(); | ||
|
||
if (num > 0.5) { | ||
return { | ||
success: true, | ||
data: 123, | ||
}; | ||
} | ||
|
||
return { | ||
success: false, | ||
error: new Error("Something went wrong"), | ||
}; | ||
}; | ||
|
||
const result = createRandomNumber(); | ||
|
||
if (result.success) { | ||
console.log(result.data); | ||
|
||
type test = Expect<Equal<typeof result.data, number>>; | ||
} else { | ||
console.error(result.error); | ||
|
||
type test = Expect<Equal<typeof result.error, Error>>; | ||
} |