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
Typescript can't infer types when using Proxy #20846
Comments
|
You're right, our current definition of interface ProxeeHandler<T extends object, TOut extends object> {
get?<K extends keyof TOut>(target: T, p: K, receiver: TOut): TOut[K];
set?<K extends keyof TOut>(target: T, p: K, value: TOut[K], receiver: TOut): boolean;
}
interface ProxeeConstructor {
new <T extends object, TOut extends object>(target: T, handler: ProxeeHandler<T, TOut>): TOut;
}
declare var Proxee: ProxeeConstructor;
let obj = {
prop1: function () { },
prop2: 'hello',
}
// Type inference can't infer what keys the result is supposed to have.
let prox: Record<keyof typeof obj, number> = new Proxee(obj, {
get(target, name) {
return 5;
}
}); |
|
Out of my head I can tell about 3 cases of Proxy changing interface of a target object:
So the idea of interface FooBar {
foo: number;
bar: string;
}
function fooOrBar<K extends keyof FooBar>(prop: K): FooBar[K] {
switch (prop) {
case "foo": return "foo"; //It should cause a type error
case "bar": return 5; // And this too
}
} |
|
I would love proxies to be able to solve what @andy-ms is saying because my use case is where input/output types are different. I'm desperate because I've wanted Scalas underscore in typescript for a long time, I have the implementation but there's no way to type it here's an example. I need a way to tell typescript that _.child is actually the converted output type... |
|
I stumble upon this. Might be too late... but you could try interface MyProxyConstructor {
new <T, H extends object>(target: T, handler: ProxyHandler<H>): H
}
const MyProxy = Proxy as MyProxyConstructor
let obj = {
prop1: function() {},
prop2: 'hello',
}
let prox = new MyProxy<typeof obj, { [name: string]: () => number }>(obj, {
get: function() {
return 5
},
})
prox.prop1 // type () => number |
|
Is there any possibility of preserving a generic call signature on the interface Demo {
a: 1 | 2,
b: 3 | 4,
}
const getter = <T extends unknown>(value: T) => T;
const demo: Demo = {
a: getter( // cursor hereFor this simple example, we get an autocomplete suggestion of It would great to have this behavior when proxies are used to satisfy an interface with stricter typing than that of the |
|
Well its 2022 now. Any updates on this? :) |

justinbc820 commentedDec 21, 2017
•
edited
Code
Expected behavior:
I would expect that when I type
prox.prop1., I would get typescript suggestions forNumber.prototype, but instead, I get suggestions forFunction.prototype.prox.prop1will (according to typescript) still be callable as a function, but in runtime, it will clearly be a number and will throw an exception.Statically evaluate the proxy traps and determine the type of thing being returned to offer proper typescript intellisense.
The text was updated successfully, but these errors were encountered: