Skip to content

Class-returning-function breaks nominal typing of instances #4497

Open
@popham

Description

@popham

Flow currently returns the same nominal type for functions returning fresh classes, e.g.

function create() {
  return class K {};
}

let K1 = create();
(create(): Class<K1>); // NG, but Flow yields a false positive

I currently have a memoizing polymorphic function that depends on this behavior, i.e.

export class PointerList {
  static _specials: { [hash: string]: Class<ByteAlignedList<any>> };
  static of<R: AnyPointer | ByteAlignedList<*> | BoolList>(_R: Class<R>): Class<ByteAlignedList<R>> {
    const hash = _R._hash();
    const cached = this._specials[hash];
    if (cached) {
      return ((cached: any): Class<ByteAlignedList<R>>);
    }

    class List extends ByteAlignedList<R> {
      static _hash(): string {
        return "L("+_R._hash()+")";
      }
      // ...
    }

    return this._specials[hash] = List;
  }
}

Can I rely on this pattern?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions