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
So I often tend to end up with type definition and implementation of it, which leads to naming collisions. There are multiple ways around it:
Rename RangeSelector class for instance to avoid collision. Which requires finding an appropriate name for one, but even then it's not actually encoded anywhere that class implements that type so you tend to only find incompatibilities once you've wrote exported function that claims to return type while returning a class instance and mismatch becomes apparent.
Just use classes in place of types. This is also not ideal because that way your functions become dependent on those classes & can't accept structurally equivalent instances just because of the annotations.
I think it would be really great if there was a way to define a structured type / interface and a class implementing with a same name and decide whether to export type / interface, class or both in export statement. Here is how it may look:
Declare type / interface as per RangeSelector definition.
Declare that RangeSelector class, that must comply with a declared RangeSelector if it is in the scope, otherwise declare RenageSelector class as type.
Refer to RangeSelector as type from type position and as class from value position. There for selector argument of refineBy function will refer to RangeSelector type definition. If there was new RangeSelector(...) expression that would refer to class instead.
If there is a export type {RangeSelector} that would export type / interface definition. If there is a export {RangeSelector} that would export class.
Alternative syntax maybe used, but what's important here that it addresses following issues:
Compliance between type and intended implementation class can be checked early, without necessarily creating a function that would declare type but use class instead.
It would not require naming same thing twice, which in my experience often leads to mistakes where one named was intended to be used instead of the other, further delaying discovery of the mismatch (shows up only when dependent library, uses function and passes structurally equivalent type while exported function used class in annotation instead of a type it meant to).
The text was updated successfully, but these errors were encountered:
Often times I would like to define the interface of my library with structured types like:
So that library functions could use those types in argument annotations, like
More often than not libraries also tend to have implementations of those via classes:
So I often tend to end up with type definition and implementation of it, which leads to naming collisions. There are multiple ways around it:
RangeSelector
class for instance to avoid collision. Which requires finding an appropriate name for one, but even then it's not actually encoded anywhere that class implements that type so you tend to only find incompatibilities once you've wrote exported function that claims to return type while returning a class instance and mismatch becomes apparent.I think it would be really great if there was a way to define a structured type / interface and a class implementing with a same name and decide whether to export type / interface, class or both in export statement. Here is how it may look:
This is how I'd propose flow to handle this:
RangeSelector
definition.RangeSelector
class, that must comply with a declaredRangeSelector
if it is in the scope, otherwise declareRenageSelector
class as type.RangeSelector
as type from type position and as class from value position. There forselector
argument ofrefineBy
function will refer toRangeSelector
type definition. If there wasnew RangeSelector(...)
expression that would refer to class instead.export type {RangeSelector}
that would export type / interface definition. If there is aexport {RangeSelector}
that would export class.Alternative syntax maybe used, but what's important here that it addresses following issues:
The text was updated successfully, but these errors were encountered: