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

noImplicitAny reports incorrect error? #8546

Closed

Description

@radu-b

TypeScript Version:

1.8.10

Code
This code uses the React type definitions via tsd install react-global

Unfortunately I could not reproduce the problem in a simpler setting, such as using a plain base class, without React and TSX.

class Control extends React.Component<any, any> {
    render() {
        return <button onClick={() => buttonClicked(Control)}>Button</button>;
    }
}

function buttonClicked(controlType: typeof React.Component) {
}

It is compiled using a tsconfig.json file:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "jsx": "react"        
    }
}

Expected behavior:
No compile errors.

Actual behavior:
Error TS7023: 'render' 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.

The problem does not happen if the onClick callback is given through an intermediary variable, instead of being inlined.

Activity

  1. mhegazy commented on May 10, 2016

    @mhegazy
    Contributor

    The compiler tries to figure out the return types of functions from the type of their return expressions. this can depend on figuring out other types. sometimes that could lead to cycles; the compiler breaks these by giving the type any. this can be solved by adding an explicit type annotation, something like render(): JSX.Element { .. }.

    Please see my reply to a similar issue in #1146 (comment)

  2. locked and limited conversation to collaborators on Jun 19, 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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions