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
In typescript you can define and export type aliases.
I am curious what your thoughts are on picking interfaces instead of type aliases for go structs.
I am trying to use the instanceof keyword but it doesn't work on interfaces, so during runtime theres no way to determine if the object returned from my function matches the interface type annotation.
The text was updated successfully, but these errors were encountered:
Type aliases also do not exist at runtime so what difference does that make? AFAIK the only difference between interfaces and type aliases is that interfaces can be extended (adding new properties).
There are advantages and disadvantages to using type or interface, both could work here. I personally tend to use interface to describe a simple object for interop (~duck typing), whereas type is reserved for more complex expressions. So I prefer the simpler interface type if that does the job. That has worked for me so far, but of course other people may prefer different flavors. If you Google it you will find arguments for either, there is not one better choice.
As tim-we commented, both do not have any runtime leftovers, so instanceof will not work on either. I would recommend a library like zod for runtime type validation.
I am happy to accept a PR that allows one to output type expressions instead for Golang structs, but I'm not sure what its advantage will be, maybe there is something Tim or myself are missing?
I'll close this topic and re-open it if there is a good usecase for the ability to switch between type and interface.
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases
In typescript you can define and export type aliases.
I am curious what your thoughts are on picking interfaces instead of type aliases for go structs.
I am trying to use the
instanceof
keyword but it doesn't work on interfaces, so during runtime theres no way to determine if the object returned from my function matches the interface type annotation.The text was updated successfully, but these errors were encountered: