You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
401 B
18 lines
401 B
7 days ago
|
function safeParse(json: string): unknown {
|
||
|
try {
|
||
|
return JSON.parse(json);
|
||
|
} catch {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const result = safeParse('{"name":"Dorr"}');
|
||
|
console.log(result);
|
||
|
|
||
|
if (typeof result === "object" && result !== null && "name" in result) {
|
||
|
console.log((result as { name: string }).name);
|
||
|
}
|
||
|
|
||
|
function fail(): never {
|
||
|
throw new Error("Failed!");
|
||
|
}
|