Skip to content

Implicit types are too strict on class decorators #17989

@MLoughry

Description

@MLoughry

TypeScript Version: 2.4.2

I ran into this issue with some of our class decorators after upgrading to TS 2.4.2 and upgrading our React typings to 15.0.38. It seems the type checking doesn't allow for return types that are a subset of the return types of an interface to resolve to that interface.

Code

declare namespace React {
    class Component {
        render(): string | false;
    }

    interface ComponentClass {
        new (): Component;
    }
}

function classDecorator(
    InputTemplate: React.ComponentClass
): React.ComponentClass {
    return class classDecorator extends React.Component {

        render(): string | false {
            return 'foo';
        }
    };
}

@classDecorator // error: Type '() => string | false' is not assignable to type '() => string'.
class Foo extends React.Component {
    render() {
        return 'foo';
    }
}

Expected behavior:
No error, as () => string is a subest of () => string | false, and is therefore compatible.

Actual behavior:
Transpilation throws an error:

index.ts(22,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
  Type 'ComponentClass' is not assignable to type 'typeof Foo'.
    Type 'Component' is not assignable to type 'Foo'.
      Types of property 'render' are incompatible.
        Type '() => string | false' is not assignable to type '() => string'.
          Type 'string | false' is not assignable to type 'string'.
            Type 'false' is not assignable to type 'string'.

I can work around the issue in this repro by explicitly specifying the return type of Foo.render()

@classDecorator
class Foo extends React.Component {
    render(): string | false {
        return 'foo';
    }
}

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