I ran into a problem while defining a value for a constant. Despite the specified type, Typescript does not throw an error and I think it should. I have prepared a code that presents my problem. I believe line 14 should be reported as an error because the "valueN" property is missing.
//example
type TT = string | {string_:'S', valueS:string} | {number_:'N', valueN:number};
function run(p:TT):number{
if(typeof p === 'string'){
throw '';
}
if('number_' in p){
return p.valueN;
}
throw '';
}
const C:TT = {number_:'N',string_:'S',valueS:'text'};
console.log(run(C));
Can someone explain to me why TS behaved like this, and how to improve the code so that line 14 reports an error for the missing property.