Closed
Description
TypeScript Version: "2.8.0-dev.20180221"
Not in "2.7.0-rc"
Search Terms: type narrowing class generics function method call props readonly
Code
// Loose function or method. Same result.
declare function bar(thing: string): void;
interface Base {
thing?: string;
}
class Example<Props extends Base> {
props: Readonly<Props>;
mutableProps: Props;
base: Readonly<Base>;
foo() {
if (this.props.thing) {
this.props.thing.toString(); // No error
bar(this.props.thing); // Error!
}
if (this.mutableProps.thing) {
bar(this.mutableProps.thing); // No error
}
if (this.base.thing) {
bar(this.base.thing); // No error
}
}
}
Expected behavior:
No error.
Actual behavior:
TypeScript error, but only with that combination of generics, Readonly
, a class, and calling a function. Remove any one of those factors and it works as expected. Some examples of that are also shown above.
Argument of type 'P["thing"]' is not assignable to parameter of type 'string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Playground Link:
Note: Turn on StrictNullChecks.
Related Issues:
None that I am aware of. This showed up when updating TypeScript, so it is probably a recent regression.