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
I was hoping that TS will infer the type of the T array from the intersect, unfortunately that does not happen (it offers both methods, regardless of whether Example is an instance of Example<Array<number>> or of Example<Array<string>>).
Among other uses, this feature would be useful for a variety of database libraries that return internal types that can have their own operations done on them. For example, when I map an DbArray<DbNumber>, I should get a different set of methods than when operating on DbArray<DbString>.
The current workaround is to ask the user to explicitly declare both - the outer and the inner type, e.g.:
interfaceExample<TOuter,TInner>{map<TOut>((item: TInner)=>TOut): TOut;// ...TOuter used in another method}
However, this TInner is redundant (it's already inside of TOuter) and sometimes not relevant at all.
With more complex types this can cause the introduction of even more generic types, that grow like cancer in the declarations (interface Example<A, B, C, D, ...>) - the more possibilities there are, the more explicit declarations need to be done at the interface level. This has been an obstacle to creating proper type definitions for RethinkDB DefinitelyTyped/DefinitelyTyped#4551.
Last example: an interface DbValue can be a generic of any primitive type in the database: an array, a string or a number.
For an instance of an array, you'd need to pass in DbValue<Array<number>, number>, yet for an instance of string, the second parameter is irrelevant: DbValue<string, void>.
What we'd need is to be able to constrain not only by method's own generics, but by the containing interfaces generics, for method signatures themselves, something like:
interfaceExample<T>{// here the constraint is not of the method, but of the typemethod(one):numberwhereTextendsArray<number>;method(two):stringwhereTextendsArray<string>;}
The text was updated successfully, but these errors were encountered:
@DanielRosenwasser Yes! I did a search before posting but nothing came up. Thanks. Brilliant that there's already an issue for that. I'll merge the conversation.
It's currently possible to do this:
But not possible to do this:
This means we cannot have different typings depending on the generic type constraint of the interface.
I've even tried a hack like this:
I was hoping that TS will infer the type of the T array from the intersect, unfortunately that does not happen (it offers both methods, regardless of whether Example is an instance of
Example<Array<number>>
or ofExample<Array<string>>
).Among other uses, this feature would be useful for a variety of database libraries that return internal types that can have their own operations done on them. For example, when I
map
anDbArray<DbNumber>
, I should get a different set of methods than when operating onDbArray<DbString>
.The current workaround is to ask the user to explicitly declare both - the outer and the inner type, e.g.:
However, this
TInner
is redundant (it's already inside of TOuter) and sometimes not relevant at all.With more complex types this can cause the introduction of even more generic types, that grow like cancer in the declarations (
interface Example<A, B, C, D, ...>
) - the more possibilities there are, the more explicit declarations need to be done at the interface level. This has been an obstacle to creating proper type definitions for RethinkDB DefinitelyTyped/DefinitelyTyped#4551.Last example: an interface
DbValue
can be a generic of any primitive type in the database: anarray
, astring
or anumber
.For an instance of an
array
, you'd need to pass inDbValue<Array<number>, number>
, yet for an instance of string, the second parameter is irrelevant:DbValue<string, void>
.What we'd need is to be able to constrain not only by method's own generics, but by the containing interfaces generics, for method signatures themselves, something like:
The text was updated successfully, but these errors were encountered: