Skip to content

ThisType doesn't work with objects in array #33392

Closed
@Akryum

Description

@Akryum

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:

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