Code Typing · TypeScript
Result Type
Discriminated union for explicit error handling.
Difficulty: Intermediate · 11 Lines · License: public-domain
type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E };
function parseJson<T>(raw: string): Result<T> {
try {
return { ok: true, value: JSON.parse(raw) as T };
} catch (err) {
return { ok: false, error: err as Error };
Time0:00
Speed (WPM)0.0
Accuracy100.0%
Mistakes0
Live Keyboard
Next Key: T | Last Pressed: -
Progress: 0.0%
`
1
2
3
4
5
6
7
8
9
0
-
=
Q
W
E
R
T
Y
U
I
O
P
[
]
A
S
D
F
G
H
J
K
L
;
'
Z
X
C
V
B
N
M
,
.
/
Space
Timed Test Leaderboard
No results yet. Be the first to set a score.