I'm having problems with how designers work in interfaces. Perhaps I donβt understand anything at all. But I was looking for answers for a while, and I can not find anything related to this.
How to implement the following interface in a TypeScript class:
interface MyInterface { new ( ... ) : MyInterface; }
Anders Halesberg creates an interface containing something similar to this video (approximately 14 minutes). But for the life of me I cannot realize this in the classroom.
I probably misunderstand something that I wonβt get?
EDIT:
To clarify. With "new (...)" I meant "anything." My problem is that I cannot get even the most basic version of this work:
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () { } }
This is not a compilation for me. I get a "Class Test" declaring the "MyInterface" interface but not implementing it. The type "MyInterface" requires a construction signature, but when you try to compile it, the type "test" is missing. "
EDIT:
So, after learning this a bit more, given the feedback.
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () => test { return this; } }
TypeScript is not valid and this does not solve the problem. You cannot determine the return type of the constructor. He will return the "test". Signature: class test {constructor () {}} This seems to be a "new () => test" (obtained by hovering over the "class" in the online editor using only the code that is inserted). And this is what we would like and what I thought it would be.
Can someone provide an example of this or something similar where it actually compiles?
EDIT (again ...):
So, I could come up with why this can be defined in the interface, but not possible to implement in the TypeScript class. The following works:
var MyClass = (function () { function MyClass() { } return MyClass; })(); interface MyInterface { new () : MyInterface; } var testFunction = (foo: MyInterface) : void => { } var bar = new MyClass(); testFunction(bar);
So, is this just a TypeScript function that allows you to interact with javascript? Or can you implement it in TypeScript without having to implement the class using javascript?
javascript constructor interface typescript
Nypan Nov 15 '12 at 22:03 2012-11-15 22:03
source share