Skip to content

Flow is improperly narrowing getter valuesΒ #56899

Closed as not planned
Closed as not planned
@craigphicks

Description

@craigphicks

πŸ”Ž 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

playground

πŸ’» 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.

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