args: any): self in your interface, it would have to be checked in runtime for existence before a call. Already on GitHub? @andy-ms was indicating to @tyteen4a03 how to get a snippet of code working, because it was wrong, not to forgo type checking. Indeed, but mine is not a workaround, it is a justification for your usecase. Having static deserialization and instance serialization implemented on a class by class basis and thereby encapsulated and typesafe. @grantila I have answered your question. So we cannot type check static factory methods in classes that implement the same interface? @Deviad @aluanhaddad Since my post I have been using InversifyJS and it has been absolutely great and definitely a much better way to go. For me it's a deeply saddening and disappointing thing. This also works, and show error at compile time without any extra function call: I ran into this and I also want to use static interface: Most people forget that there are already static interfaces in the sense that a constructor function/class has two interfaces already, the constructor interface and the instance interface. @andy-hanson Thanks for taking the time to correct me. since 2002. Would anybody care to reply with a solution to my question here: http://stackoverflow.com/questions/44047874/dynamically-modify-typescript-classes-through-a-generic-function. Your use case is distinct from those presented in this issue, in that your class type may (or not) provide a method depending on runtime conditions. @rmblstrp that doesn't require this feature. For example, this compiles without error: @Serginho Not a Java user, but it doesn't look like the language allows you to define an interface for the static side of a class (meaning classes implementing the interface would have to implement a static method to conform). static side of the the class), and 2. the instance side (what comes out when calling new). In many languages like PHP and Java, static factories have been like deprecated in favor of Dependency Injection. Sign up for a free GitHub account to open an issue and contact its maintainers and the community. My use case for allowing static methods to use the class generic parameter is for mixin classes. Have a question about this project? There are still two things that give me discomfort (admittedly neither are show stoppers): We still aren't able to declare that Bar explicitly implements or conforms to Factorizable. Which btw, is a hack.
Personally, my recommendation is to not use type annotations explicitly unless you have to; let the types flow through he system. https://github.com/Deviad/virtual-life/blob/master/models/generic.ts, https://github.com/Deviad/virtual-life/blob/master/service/user.ts, https://github.com/Deviad/virtual-life/blob/master/models/user.ts, https://github.com/Deviad/virtual-life/blob/master/utils/sqldb/client.ts, https://github.com/Deviad/virtual-life/blob/master/bootstrap.ts, https://www.youtube.com/watch?v=e-5obm1G_FY&t=1487s, In practice, I imagine this really isn't a problem. https://github.com/Deviad/virtual-life/blob/master/bootstrap.ts. @tyteen4a03 Remove IObject from that example, and it will compile. The world neither starts nor ends with OOP. At the time of my post I had just started using Typescript/Node after having been PHP/C# previously. } Statics are a parallel concept to dynamic behaviour/virtual methods. https://github.com/Deviad/virtual-life/blob/master/models/user.ts I fixed the error in my example. I think this issue (13462) should be looked at again, as solutions like yours, using type casting, is actually not type safe, and if this is the only way to solve the situation of working with the class type as a value, we're losing a lot of the flexibility of a dynamic language. . @aluanhaddad Come on! Well occasionally send you account related emails. I think this would be pretty cool! These are the files where you can see how the whole thing is handled. But actually makes me to implement: There is no technical reason for not allowing to define static methods on interfaces. @rozzzly In this example, abstract class is not valid. Also, I suggest you to look at this video about functional programming that covers this aspect about which one is better: https://www.youtube.com/watch?v=e-5obm1G_FY&t=1487s By clicking Sign up for GitHub, you agree to our terms of service and @rmblstrp Can you show how you would use the proposed feature in your example? No errors .
I think that whole issue would be solved by being able to specify static members on interfaces, and if this issue is closed because it's not needed, I would very much like to see how to solve it instead. As @gcnew said, an interface describes the shape of an individual object, not its class. Thanks for the article! The real issue cannot be fixed by the Object.defineProperty() hack unfortunately. https://github.com/Deviad/virtual-life/blob/master/utils/sqldb/client.ts Your declaration: is irrelevant as it basically re-declares the interface of the JSON object. @andy-ms Yes obviously it works but the entire point of type checking is to.. check types. Pseudo-OOP features like interfaces, overemphasis of classes, inline access modifiers -- C# got all these downgrades of programming language design from Java. We would have features like default method implementations, static interface members etc. C. A small minority dont believe it should be doable at all, and would rather delete the current weird way to do it. Typescript has a wonderful IOC container named InversifyJS. https://github.com/Deviad/virtual-life/blob/master/service/user.ts Failing to meet the signature of either toJson or fromJson results in a compile error.
@Serginho Probably related (since it deals with typing static methods): #5863. And one keyword less. We dont like (each) specific syntax weve seen so far. I am not saying it's a perfect solution (and probably it leaves some scenarios uncovered) but it works also with React, there some examples already you can look at. So, who knows? B.
You don't need both and it would actually be rather redundant. You signed in with another tab or window. And IMO that is more unsafe than the type cast presented in my answer, which was only performed to allow the insertion of a field in an object. can you elaborate on the scenario? Reading this and other related issues, it sounds like it mostly the following issues: A. Its hard The check is done at use site any ways regardless if you have an implements clause or not. Which do you see more clearly? public static fromJson(obj: any); In fact, since you are using a decorator, you can actually is its type to verify that the annotated classes provides required static methods. However, I do remember when I was starting to work with C# (over a decade ago) I tried to do exactly this and found out this was not possible. There might be some programmers who would go down this route. Thank you for providing that! Plus the .NET collections mess caused by broken "interfaces" design would never need to have happened. @Enet4 I updated the question, it was overly-simplified. I was just trying to illustrate that it can be expressed.
Yes obviously it works but the entire point of type checking is to.. check types. :). I want to ensure not to having misspelled make - basically proper static checking. The big difference is that in this case you treat the class as an object, which continues to be logically consistent. You may find programming with functions, plain objects and (and to some degree) prototypes pleasurable and even better. https://github.com/Deviad/virtual-life/blob/master/models/generic.ts See also #17545 (comment). Java added this functionality in the last version. Mocking Current Time with a Simple Time Provider, IReaderFactory.cs file in the StaticMembers project, https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions#provide-parameterization, C# 8 Interfaces: Public, Private, and Protected Members. (Unfortunately, the error is at const _ rather than at the method.). That absolutely does not require static interface methods in any way. I am building an entity framework which uses annotations to define columns, databases, etc and would like to have a static function mixed in to my entity classes which would allow convenient access to the correctly typed repository. The last two decades have mostly just been them trying to detox the language imo. Actual behavior: The currently prevailing patterns count on ducktyping/shape programming, not on rigid classes. It just took a little bit of time getting familiar with the environment and available packages. The text was updated successfully, but these errors were encountered: What do you wan to achieve with this? solutions like yours, using type casting, is actually not type safe, and if this is the only way to solve the situation of working with the class type as a value, we're losing a lot of the flexibility of a dynamic language. Preferably as something verifiable? I believe this is one of those cases where the issue should be explicitly labelled with "wontfix", as the choice of not having static methods in interfaces is by design. I imagine abstract classes are what you're looking for. I have seen code bases, however, where the style guide is the opposite, where everything has an explicit type annotation. Though not being a language designer or compiler implementor I'm not sure if it fits the intended direction of TypeScript or is realistic to be implemented. : @mhegazy that is a relatively good solution. of all the candy and shortcuts that C# could use, I'm flummoxed that they chose to implement statics in interfaces. As previously mentioned in this issue, unless you have further requirements not mentioned there, this is easily solvable by treating classes as objects. Do you think typescript should allow static methods in Interfaces? On a side note, I agree JS is slowly shifting towards OOP-esque patterns (at least on the committee level), but that's because of the huge push of people not being comfortable with different programming paradigms and development techniques. As @aluanhaddad already wrote, TypeScript actually already has a mechanism to express what you desire. ), I want to talk specifically about static factories which are used usually to create a connection to a db or provide some kind of service. Sign in Java allows you to define a static method with a body in an interface, the TypeScript equivalent of which would be: What if you were trying to make a generic factory? Unsupported. Having siad that, this is rather a style issue. This is a pattern I use frequently in Swift (using protocols) and I think it would be really nice in TypeScript. Open your mind. SPOILER: no one is better, it depends on the problem you want to solve. thanks for the example! The two mean different things, var y = x.bar(Bar) declares a new variable y with the same type as x.bar(Bar); where as var y: Bar = x.bar(Bar) declares a new variable y with type Bar and verifies that the type of x.bar(Bar) is assignable to Bar. The reason it compiles successfully is that interface A extends JsonSerializable