703 B - Async Await - TypeScript Fail
by steen_hansen
HTML
<!DOCTYPE html><meta charset="utf-8" />
<title>703 B - Async Await - TypeScript Fail</title>
<!--
This example exists to show that TypeScript does not help with runtime type errors that TypeCzech catches.
This is the HTML in https://jsfiddle.net/steen_hansen/hydvt4jq/
-->
<script src="https://cdn.jsdelivr.net/gh/steenhansen/type-czech@latest/web-resources/consolelog.js"></script>
<script src="https://cdn.jsdelivr.net/gh/steenhansen/type-czech@latest/web-resources/TypeCzech.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/steenhansen/type-czech@latest/web-resources/type-czech.css">
<div id="app"></div>
<div id='js-div' style="width:50%">
<span class='git-hub'><b>Vanilla JavaScript</b></span>
<div id='js-code'>the javascript</div>
<br>
<div id='js-fiddles'>
<a title="Example-Contents" href="https://jsfiddle.net/steen_hansen/1Lshcept/?Example-Contents" target="_blank">Contents</a>
<a title="00-Readme-Example" href="https://jsfiddle.net/steen_hansen/0xtpLwsc/?00-Readme-Example" target="_blank">00</a>
<a title="01-What-is-TypeCzech" href="https://jsfiddle.net/steen_hansen/9u54vsd2/?01-What-is-TypeCzech" target="_blank">01</a>
<a title="02-Validate-Parameters-by-Value.html" href="https://jsfiddle.net/steen_hansen/6zy89om1/?02-Validate-Parameters-by-Value" target="_blank">02</a>
<a title="03-Turn-Off-Library-Not-Loaded.html" href="https://jsfiddle.net/steen_hansen/m1tce27f/?03-Turn-Off-Library-Not-Loaded" target="_blank">03</a>
<a title="04-Turn-Off-No-Check-Functions.html" href="https://jsfiddle.net/steen_hansen/nve4d3ah/?04-Turn-Off-No-Check-Functions" target="_blank">04</a>
<a title="05-Verify-Return-Results-by-Value.html" href="https://jsfiddle.net/steen_hansen/wrkvscqg/?05-Verify-Return-Results-by-Value" target="_blank">05</a>
<a title="06-Validate-Parameters-and-Results.html"...
TypeScript
// This example exists to show that TypeScript does not help with runtime type errors that TypeCzech catches.
/*
This is the TypeScript + No-Library (pure JS) code in https://jsfiddle.net/steen_hansen/hydvt4jq/
*/
type JSONResponse = {
args: {correct_key: string}
}
async function fetchJsonTs(the_url:string) :Promise<JSONResponse> {
let the_response = await fetch(the_url)
let the_json = await the_response.json()
return the_json
}
fetchJsonTs("https://httpbin.org/anything?correct_key=correct_value")
.then( the_json=> log("|| typescript gets correct key ", the_json.args) )
fetchJsonTs("https://httpbin.org/anything?wrong_key=wrong_value2")
.then(no_err_ts => no_err_ts);