Closed
Description
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:
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
j-oliveras commentedon Sep 12, 2019
The this inside
meows
is a reference to the object inside the array, not the object meows. You can test it with node:Akryum commentedon Sep 12, 2019
It will do the same with:
That's not the point of this issue, those methods a bound internally by my library.
fatcerberus commentedon Sep 13, 2019
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
).Akryum commentedon Sep 13, 2019
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIgM5jiJyDeAUMsjAPZkBcyA5AEZxQ3IA+tDjNhAvoYWAE8ADigBiIZAF5kACgCUUgHzIcA1ipAC+oSLEQoAygFc6xugWLIAbowzVxPbeGjw8ZiyRAQsEACbUzM0dCHRd9ZAB5ITBgMkwPZAwTO2R3NjMAbQBdYIQ4rGQAWwgyAHdqKJj85AAyZAAVAAtgDHrhCAAedCwcJGVpIhIkuhTBkmQvH38E8etbWQUx2ZIoCDAjKAkwZowAOnIyKUlpek5LWd4Ly15eQjzMMCKS0pTK2Pi6ppa2kS6H3og-QSwxSGXOM3Gk0g0yWyxsUAwCwhy2Qq3Wm2Q2xa+woRxODCY4KuKMu41JOV4QA
Akryum commentedon Sep 13, 2019
I guess it's necessary, otherwise Vue typings wouldn't work 😁
RyanCavanaugh commentedon Sep 13, 2019
@fatcerberus is correct
weswigham commentedon Sep 13, 2019
FYI, if you want a deeply bound
ThisType
, you can do something likebut it's on you to make sure your library can actually bind the
this
's of those functions up to match that.Akryum commentedon Sep 14, 2019
@RyanCavanaugh I've put a link in this message showing the inconsistency in nested objects.
typescript-bot commentedon Sep 16, 2019
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Akryum commentedon Sep 19, 2019
Using a custom type
DeepApplyThisType
breaks other types:Example
Akryum commentedon Sep 19, 2019
Could the issue be re-open? It causes me a lot of difficulties for vue-apollo typings.
dragomirtitian commentedon Sep 20, 2019
@Akryum
DeepApplyThisType
works but does not cover all cases as it should. It should distribute overT[K]
to correctly apply this in a union, and should not perform the transformation on function or primitives. This works with your example:play
Other corner cases may arise but they can probably be solved.
Akryum commentedon Sep 20, 2019
Thanks, will try it
Akryum commentedon Sep 22, 2019
It's still doesn't work, the types are still broken:
Play
No error is thrown here:
Akryum commentedon Sep 30, 2019
Please can this be reopen? 😅
Akryum commentedon Feb 11, 2021
💀