function foo1(o: { x: number, ... }) {
  if (o.p1) { // Error, testing for unknown property
    o.x;
  }
}

function foo2(o: { x: number, ... }) {
  if (o.p2) { // Error, testing for unknown property
    o.p2.x; // error, since o.p2's type is unknown (e.g., could be `number`)
  }
}

function foo3(o: { x: number, ... }) {
  o.p3.x; // usual error outside conditional
}

function foo4(o: { x: number }) {
  if (o.p4) { // Error, testing for unknown property
    o.p4.x; // currently OK, should be unreachable
  } else {
    o.p4.x; // error
  }
}

function foo6(o: unknown) {
  if (o.bar) {} // error, any lookup on mixed is unsafe
}

function foo7(o: unknown) {
  if (typeof o.bar === 'string') {} // error
  if (o && typeof o.bar === 'string') {} // ok
  if (o != null && typeof o.bar === 'string') {} // ok
  if (o !== null && o !== undefined && typeof o.bar === 'string') {} // ok
}

function foo8(o: { p: unknown, ... }) {
  if (o.p && o.p.q) {} // this is ok because o.p is truthy, so o.p.q is safe
  if (o.p && o.p.q && o.p.q.r) {}
}

type Foo9Expected = {
  foo: string,
 ...}

function foo10() {
  if (null.q) {} // error: property `q` on null
}

function foo11() {
  declare function invariant(a: unknown): void;
  declare const b: unknown;
  invariant(b != null && b.foo != null); // ok
}

function foo12() {
  declare const a: unknown;
  if (a != null && a.foo instanceof Set) {
    a.foo as Set<unknown>;
  }
}

function foo13() {
  declare const a: unknown;
  if (a != null && Array.isArray(a.foo)) {
    a.foo as ReadonlyArray<unknown>;
  }
}
