#Inaccurate return type inference of generic context-bound function

14 messages · Page 1 of 1 (latest)

median stratus
#

Hi all,
I can't figure out why a bound function won't return the same type as a simple unbound function. Please see repro below.
Is there any way I could have the bound function return the same type?
Any help is appreciated. Thanks!

peak cargoBOT
#
slashrug#0

Preview:ts const myObject = { key1: "value1", key2: "value2", } class MyProxyHandler<T extends object> implements ProxyHandler<T> { constructor(protected log: (input: any) => void) {} public get( target: T, key: string | symbol, receiver: T ) { this.lo ...

cursive violet
#

apparently just binding a function with a declared this loses its generic

peak cargoBOT
#
that_guy977#0

Preview:ts declare function f<T>(this: {}): T const x = f.bind({}) // ^?

cursive violet
#

if the this: {} is omitted, then the generic is preserved
found this open issue about retaining generics in general though #54707

median stratus
#

😦

#

Thanks though!

#

Can anyone come up with a workaround?

#
const proxy1 = createProxy_bound(myObject) as typeof myObject
``` seems to work...
cursive violet
#

the issue i linked has a comment at the end that links to a SO post, which doesn't give any, so i'm not hopeful tbh

cursive violet
median stratus
#

Yeah this would make using my API a pita...

#

Tbh this is a pretty basic omission imo.

#

Anyway thanks.