-
Notifications
You must be signed in to change notification settings - Fork 12.8k
accessor allows this
parameter but is not checked or enforced.
#36883
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
Comments
For reference, what I was doing when I ran into this is that I wanted to write an abstract class with a static method that would work as a valid react component, something like this: abstract class HookCls<P>{
abstract useRender(props: P): React.ReactElement | null;
public static JSX<P>(this: new()=>HookCls<P>, props: P){
const instRef = React.useRef<HookCls<P>>();
const inst = instRef.current ?? (instRef.current = new this());
return inst.useRender(props);
}
} The constraint means that abstract class HookCls<P> {
abstract useRender(props: P): React.ReactElement | null;
public static get JSX<P>(this: new () => HookCls<P>) {
return (props: P) => {
const instRef = React.useRef<HookCls<P>>();
const inst = instRef.current ?? (instRef.current = new this());
return inst.useRender(props);
};
}
} Right away typescript told me that generics on a getter is not valid, but it took me a while to find that it also doesn't support a It would be amazing if typescript was capable of representing that |
We should give an error on the
or
I prefer the first one. |
Thanks @a-tarasyuk! |
TypeScript Version: 3.7.5 and 3.8 (91ffa1c)
Search Terms:
getter, get, set, setter, accessor property, this parameter
Code
In the following,
x.a
andx.b()
are invalid for the same reason butx.a
doesn't give a type error.Expected behavior is one of:
this
parameter on an accessor field (get or set)x.a
gives same type error as callingx.b()
Actual behavior:
this
parameter is syntactically allowed on accessor but has no impact outside the function so it is never checked. Accessingx.a
throws an error at runtime.Playground Link:
Playground Link
Related Issues:
none found, It's quite possible I'm the first one to actually try something like this.
The text was updated successfully, but these errors were encountered: