Upon upgrading from typescript 4.9.3 to 5.0.2, we encountered an error when asserting types.
Can someone explain why the function "wontWorking" is not functioning correctly?
I expected this function to infer v
as Record<string, any>
, but instead it infers it as {}
. Even after calling assertRecord
, the type remains as {}
.
function assertRecord(v: unknown): asserts v is Record<string, any> {
if(typeof v !== 'object' || v === null) throw new Error ();
}
function wontWorking (v: unknown) : null | any{
// v = unkown
if ( v === null || v === undefined ) return null; //checking
// v = {}
assertRecord(v)
// v = {}
var s = v.fileName // ERROR: Property 'fileName' does not exist on type '{}'.
return s
}
wontWorking({fileName: 1231321});
// Additional functions and code snippets omitted for brevity...
The expectation is that the wontWorking
function will correctly infer the type of v
as Record<string,any>
after invoking assertRecord
.