TypeScript: ThisType doesn't work with objects in array

TypeScript Version: 3.7.0-dev.20190912

Search Terms: thistype array

Code

interface Instance {
  foo: 'bar' | 'baar'
}

type Fn = () => any | any

interface Sub {
  vars: Fn
}

interface Options {
  subs: Sub | Sub[]
}

const meow: Options & ThisType<Instance> = {
  subs: {
    vars () {
      return this.foo === 'bar'
    }
  }
}

const meows: Options & ThisType<Instance> = {
  subs: [
    {
      vars () {
        // Error happens here
        return this.foo === 'bar'
      }
    }
  ]
}

Expected behavior:

No errors.

Actual behavior:

error TS2339: Property 'foo' does not exist on type 'Sub'.

Playground Link:

https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIgM5jiJyDeAUMsjAPZkBcyA5AEZxQ3IA+tDjNhAvoYWAE8ADigBiIZAF5kACgCUUgHzIcA1ipAC+oSLEQoAygFc6BYsgBujDNXE9t4aPDwB5IWGBlMZkhhM3kY1M2IIBtAF17QgQvLGQAWwgyAHdqNw9Y5AAyZAAVAAtgDFzhCAAedCwcJGVpIl9-anqSS2tZBWaW5CgIMCMoCTBCjAA6cjIpSWl6TnMSXnmomMwwBKTkgPTPbxyCopKRCpXqiFqfZD86ANC5866rKAx2u67u3v7B4bGKSemGJluiy6C2QkV4QA

Related Issues:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Pretty sure ThisType only affects the object directly assigned to it, not sub-objects, whose properties have their own types (and therefore don’t inherit the ThisType).