Even while type MyRequest requires the ID attribute the function process is able to declare the variable of type MyRequest even while the ID attribute is missing:
export type MyRequest =
{
ID: string,
Name?: string
};
function process(jsonData:string) {
try {
let data: MyRequest = JSON.parse(jsonData);
return data
} catch (err) {
console.log("Error:", err)
}
}
let jsonData = JSON.stringify( { foo: "bar" } );
let result = process(jsonData);
console.log(result);
How to edit the function process so it errors if the incoming string data doesn't contain the ID attribute required by the type MyRequest?