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:
Related Issues:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 16 (4 by maintainers)
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 theThisType
).