-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ThisType doesn't work with objects in array #33392
Comments
The this inside var meows = {
subs: [
{
vars () {
this.foo = 'hello' // assigned to display what is this here
// Error happens here
return this.foo === 'bar'
}
}
]
}
console.log(meows) // { subs: [ { vars: [Function: vars] } ] }
meows.subs[0].vars()
console.log(meows) // { subs: [ { vars: [Function: vars], foo: 'hello' } ] } |
It will do the same with: const meow = {
subs: {
vars () {
this.foo = 'hello'
return this.foo === 'bar'
}
}
}
console.log(meows) // { subs: { vars: [Function: vars] } }
meows.subs.vars()
console.log(meows) // { subs: { vars: [Function: vars], foo: 'hello' } } That's not the point of this issue, those methods a bound internally by my library. |
Pretty sure |
I guess it's necessary, otherwise Vue typings wouldn't work 😁 |
@fatcerberus is correct |
FYI, if you want a deeply bound type DeepApplyThisType<T, TThis> = {[K in keyof T]: DeepApplyThisType<T[K], TThis>} & ThisType<TThis>; but it's on you to make sure your library can actually bind the |
@RyanCavanaugh I've put a link in this message showing the inconsistency in nested objects. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Using a custom type |
Could the issue be re-open? It causes me a lot of difficulties for vue-apollo typings. |
@Akryum type DistributionHelper<T, TThis> = T extends Function | string | number | boolean ? T: DeepApplyThisType<T, TThis>
type DeepApplyThisType<T, TThis> = {
[K in keyof T]: DistributionHelper<T[K], TThis>
} & ThisType<TThis>; Other corner cases may arise but they can probably be solved. |
Thanks, will try it |
It's still doesn't work, the types are still broken: No error is thrown here: const apollo2: DeepApplyThisType<ApolloOptions, Instance> = {
$client: '42',
foo: {
// No error thrown here
variableeees () {
return {
bar: this.foo
}
},
update: (data) => data.foo
}
} |
Please can this be reopen? 😅 |
💀 |
TypeScript Version: 3.7.0-dev.20190912
Search Terms: thistype array
Code
Expected behavior:
No errors.
Actual behavior:
Playground Link:
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIgM5jiJyDeAUMsjAPZkBcyA5AEZxQ3IA+tDjNhAvoYWAE8ADigBiIZAF5kACgCUUgHzIcA1ipAC+oSLEQoAygFc6BYsgBujDNXE9t4aPDwB5IWGBlMZkhhM3kY1M2IIBtAF17QgQvLGQAWwgyAHdqNw9Y5AAyZAAVAAtgDFzhCAAedCwcJGVpIl9-anqSS2tZBWaW5CgIMCMoCTBCjAA6cjIpSWl6TnMSXnmomMwwBKTkgPTPbxyCopKRCpXqiFqfZD86ANC5866rKAx2u67u3v7B4bGKSemGJluiy6C2QkV4QA
Related Issues:
The text was updated successfully, but these errors were encountered: