Apparently when assigning a value to a function type, the argument types should be an exact match. For example assigning a function with type:
(arg: {prop: string}) => void
to a variable with type:
(arg: object) => void
will throw error that:
Type '(arg: {prop: string;}) => void' is not assignable to type '(arg: object) => void'. Types of parameters 'arg' and 'arg' are incompatible. Property 'prop' is missing in type '{}' but required in type '{ prop: string; }'
Any help or explanation about the purpose of this behavior is appreciated, why typescript does not accept any object type for arguments with type object in this scenario?
objectdoesn't guarantee there is keyprop. Thisobject? to accept any object. because for example hereconst v:object = { prop: '' };, any object is accepted for variablevobjectis likeanybecause almost everything is js is object exception for some primitives See this. And Typescript need to make sure that your function will accept thepropkey since object is any object if you doesn't pass thepropkey it will throw error in runtime and typescript is trying to prevent that.--strictFunctionTypesenabled (see docs). This is for safety reasons. Yes, every{prop: string}is anobject, but not every function that accepts any{prop: string}argument is one that accepts anyobjectargument. See other linked q/a for more info