Search code examples
reactjstypescriptreact-typescript

Typescript not throwing error for callback return type mismatch


CONTEXT :

  • I have a callback for handling onChange event and a react component using this callback.
  • For this example, the return types of the callback is a 'string' and the return type expected in the type of the handle change props component is 'void'

enter image description here

enter image description here

QUESTION :

Why am i not getting error's even when the return types are different ?


Solution

  • You don't get an error because one type is compatible with the other.

    <Difficulties handleChange> does NOT use the return value of the handler (it's claimed with the void keyword), thus you can basically put there a handler which returns whatever you like, it's all acceptable and TS won't complain.