镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Return type annotation missing error #1146

Closed

Description

Hi,

VS: 2013 Update 4
TS: 1.3

Here's some code that compiles correctly:

class Base {

    foo() {
        return this;
    }
}

class Derived extends Base {

    foo() {
        return super.foo();
    }
}

Here's some other code that fails:

class Base2 {

    foo() {
        return this;
    }
}

class Derived2 extends Base2 {

    // 'foo' 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.
    foo() {
        return <Derived2> super.foo();
    }
}

I'm aware that a bug with implicit-any and recursive methods has been fixed, but to me it's pretty clear that the return value for Derived2.foo is Derived2.

Activity

  1. mhegazy commented on Nov 13, 2014

    @mhegazy
    Contributor

    The issue is not no-implicit-any. The problem is the compiler was resolving g the type of Dervived2, that is trying to build a structure for how objects of this type look like. It figured that it has a foo method, then it tried to get the signature for it. Since it does not have a return type annotation, the compiler then needs to look at all return statements to get the return type. In this case it is the type assertion that defines the type. Now here is the issue we do not really know what dervided2 is, cause we were just trying to figure this our. In these situations the compiler breaks the tie by giving it an any type.

    The warning basically tells you the compiler could not infer the type as you write it and set it to any. An explicit type annotation should do the trick.

    Hope that helps.

  2. RyanCavanaugh commented on Nov 26, 2014

    @RyanCavanaugh
    Member

    Hopefully we will eventually figure something more concrete out here, but for now the compiler is still in a recursive type inference situation when it is trying to decide whether or not the operand of the type assertion is assignable to the asserted type, which triggers a 'recursive any'.

  3. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions