Closed
Description
TypeScript Version: 2.2.1, 2.3.0
Code
// A *self-contained* demonstration of the problem follows...
// Compiled with strictNullChecks=true
class Foo {
test1(foo:string|undefined):string {
return foo || 'default'; // no error
}
test2(params:{foo: string|undefined}):string {
return params.foo || 'default'; // no error
}
test3(params:Pick<{foo: string|undefined}, 'foo'>):string {
return params.foo || 'default'; // no error
}
foo:string|undefined;
test4(params:Pick<this, 'foo'>):string {
return params.foo || 'default'; // error: undefined not assignable to string
}
test5(foo:this['foo']):string {
return foo || 'default'; // error: undefined not assignable to string
}
test6(foo:this['foo']):string {
return (foo as string|undefined) || 'default'; // no error
}
}
Expected behavior:
Expected no errors because the type of the expression foo || 'default'
should be string
in all cases.
Actual behavior:
When a type is derived indrectly from this
it seems to break the type inference of the || operator.
Error:(17, 6) TS2322:Type 'this["foo"]' is not assignable to type 'string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Error:(20, 6) TS2322:Type 'this["foo"]' is not assignable to type 'string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.