-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
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';
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug