-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: implement "implements" operator #15174
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
You might want to try https://www.npmjs.com/package/dtsgenerator or similar tools |
Yep, but... you know... implements would be nicer. I get you don't want to add so much additional code. |
This also just isn't doable. interface I {
m(x: number): number;
}
function f(x: {}): x is I {
...
} There's no way we can fill in the body of that function for you automatically. Also, we would be doing an arbitrary amount of work. |
I am sorry, but i don't understand what you mean by this. Where do you say that x implements something? in my case, i just wanted to know if members defined by the interface exists on the object. |
Just because an object has all the same keys doesn't mean that it actually implements the interface. |
Thats true :) But there is no another option, how to check it right? If we will not take reflection into account. |
Why would you want that, though? If you don't trust an object, no amount of checking will suffice. If you do trust an object, you should give it an O(1) way of identifying itself as a member of the interface, as in: interface Foo {
__fooBrand: true;
}
class C implements Foo {
get __fooBrand(): true { return true; }
}
function isFoo(obj: {}): obj is Foo {
return !!(obj as Foo).__fooBrand;
} |
Its complicated. I am using it together with generics so I am good at compile time but I wanted to make sure the same at runtime. Nice workaround, thanks for recommendation! I'll try to implement it like that. |
@novakf
This is so true that even cases like class Credentials) { }
class A extends Credentials { } // prototype chain will preserve basic heritage.
const a = new A();
console.log(a instanceof Credentials); // true It does guarantee any runtime argument validation, let alone security, at all. class Credentials { }
class A extends Credentials { } // prototype chain will preserve basic heritage.
const credentials = {
__proto__: Credentials.prototype
};
console.log(credentials instanceof Credentials); // true |
Hello,
I would like to ask if it would be possible to implement the "implements" operator for run-time returning true or false based on evaluation if object implements an interface.
I can imagine there will be a lot of edge cases but for simple class - implements statements it would be simply doable.
Lets take a look on the following example:
for such cases we need to write something like:
It is quiet a lot of code to be written manually if we want to test multiple interfaces. I can imagine the compiler could generate such functions for us.
Thanks for response.
Regards,
Franta
The text was updated successfully, but these errors were encountered: