You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expected ThisParameterType helper to be able to distinguish between methods (unbound function properties) and callbacks (bound functions), but it doesn't want to do it:
classCls{method(){}callback=()=>{}}typeaaa=ThisParameterType<Cls["callback"]>;// Aaa = unknowntypeBbb=ThisParameterType<Cls["method"]>;// Bbb = unknown -- WHY? How to distinguish?
method property from a bound callback property this
💻 Use Cases
I want to enumerate all plain properties of some type (e.g. a class instance), but skip methods. Currently I do it with:
export type NonMethodPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
export type NonMethodProperties<T> = Pick<T, NonMethodPropertyNames<T>>;
but unfortunately, this NonMethodProperties also skips callback properties. E.g. in this example:
class Cls {
constructor(public a = 10, public callback: () => void) {}
method() {}
}
...I would expect NonMethodProperties<Cls> to return { a: number; callback: () => void }, but unfortunately it skips not only method, but also callback since I couldn't find a way to distinguish them.
The text was updated successfully, but these errors were encountered:
The method in your class don't have a this argument, so ThisParameterType<> does not work on them. There's the open issue #28548 to automatically add a this argument to class methods.
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.
Suggestion
I expected
ThisParameterType
helper to be able to distinguish between methods (unbound function properties) and callbacks (bound functions), but it doesn't want to do it:Playground link
🔍 Search Terms
method property from a bound callback property this
💻 Use Cases
I want to enumerate all plain properties of some type (e.g. a class instance), but skip methods. Currently I do it with:
but unfortunately, this
NonMethodProperties
also skips callback properties. E.g. in this example:...I would expect
NonMethodProperties<Cls>
to return{ a: number; callback: () => void }
, but unfortunately it skips not onlymethod
, but alsocallback
since I couldn't find a way to distinguish them.The text was updated successfully, but these errors were encountered: