Skip to content

Compiler is unable to resolve return type of function even though it is returning function with known return type #26623

@devuxer

Description

@devuxer

TypeScript Version: 3.0.1

Search Terms:

  • "implicitly has return type 'any'"
  • TS7023

Code

interface Identifiable {
    name: string;
    nickname: string;
    isNameSameAsNickname: boolean;
}

class Person implements Identifiable {
    public name = "Baz";
    public nickname = "Baz";

    get isNameSameAsNickname() {
        return compareNames(this);
    }
}

function compareNames(identifiable: Identifiable) {
    return identifiable.name === identifiable.nickname;
}

Expected behavior:

Compiles without error

Actual behavior:

The following error occurs:

TS7023: 'isNameSameAsNickname' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.

Comment: This error appears to be incorrect (or at least worded ambiguously) because isNameSameAsNickname is not referenced directly or indirectly in one of its return expressions. Specifically, isNameSameAsNickname does not appear directly in the return expression compareNames(this) nor does it appear indirectly in identifiable.name === identifiable.nickname.

Workaround:

Declare the return type explicitly:

get isNameSameAsNickname(): boolean {
    return compareNames(this);
}

Playground Link:

This error is indicated in the playground with a red squiggle below isNameSameAsNickname().

Note: Error only occurs if noImplicitAny option is checked

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions