Search code examples
reactjsnode.jstypescriptintellij-idea

IntelliJ does not hint error on return type mismatch in typescript


We are developing a web application with react+typescript+spring boot with IntelliJ. Everything was fine until I noticed something that I did not expect. Consider this code snippet example:

export class TreeRefreshOutcome
{
}

export class TreeRefreshOutcome2
{
}

function foo() : TreeRefreshOutcome
{
    return new TreeRefreshOutcome2();
}

I expected, that the code inspection recognizes the type mismatch of the defined return-type of the function foo. The problem occurred while refactoring the code. There are currently no other marked errors in the codebase. I tried different combinations:

export class TreeRefreshOutcome
{
}
function foo() : string
{
    return new TreeFreshOutcome; //ERROR
}
export class TreeRefreshOutcome
{
}
function foo() : TreeRefreshOutcome
{
    return ""; //NO WARNING / ERROR
    return 5; //NO WARNING / ERROR
    return null; //ERROR
}

I switched from TSLint (that was the default in IntelliJ) to ESLint with no difference. It seems, that the problems rises with using a class as the defined return type. Using type definitions does raise errors as expected. Using interfaces behaves exactly like using classes. Other warnings and errors show up as I expected them to be. I am not that familiar with typescript nor JavaScript but I expected the IDE to recognize such simple errors. Every hint to why this happens or what could possibly be wrong with some settings / configuration file might be appreciated.


Solution

  • VLAZ hinted me in the right direction. Thank you for that. I know, that the runtime does not distinguish between compile-time types. But in my opinion at least a warning should be raised from the IDE because the situation hints to a possibly wrong return type.

    Answer: Empty class types are treated equaly at runtime and compile time.