Closed as not planned
Description
π Search Terms
getter narrowing
function return narrowing
an issue where setter (not getter) is behaving like a plain property: #56894
π Version & Regression Information
same 4.3.5 ~ 5.2.3
prior to 4.3.5 TypeScript rejected the syntax.
β― Playground Link
π» Code
function f10(x: { get r():1|2; readonly v: 1| 2; f:()=>1|2 }): void{
// ^ (parameter) x: { readonly r: 1 | 2; readonly v: 1 | 2; f: () => 1 | 2; }
if (x.v===1){
x.v;
// ^ (property) v: 1
}
if (x.r===1){
const c = x.r;
// ^ (property) r: 1
// ^ const c: 1
}
if (x.f()===1){
const c = x.f();
// ^ (property) f: () => 1 | 2
// ^ const c: 1 | 2
}
}
π Actual behavior
if (x.r===1){
const c = x.r;
// ^ (property) r: 1
// ^ const c: 1
}
π Expected behavior
if (x.r===1){
const c = x.r;
// ^ (property) get r: () => 1 | 2
// ^ const c: 1 | 2
}
Additional information about the issue
- A getter is a function, not a plain type, and should behave like one, even though it doesn't have wings.
- The getter is converted to a readonly plain property (probably at point of declaration) as can be seen by tool tipping to
x
which shows(parameter) x: { readonly r: 1 | 2; readonly v: 1 | 2; f: () => 1 | 2; }
. Related to setter inferred from union has incorrect varianceΒ #56894.