Re:Free-types: Higher kinded types in TypeScript
by noobdev9000 on Friday June 09, 2023 @02:43AM
(#36254178)
Why
[ Parent
| on HN ]
Re:Free-types: Higher kinded types in TypeScript
by mbwgh on Friday June 09, 2023 @03:31AM
(#36254538)
A simple example I could recall from the other day is something like this:
export type LinkedWorksheetsRecord = Record<WorksheetId, Record<WorksheetId, ReferenceTypeId[]>>;
export type LinkedWorksheetsMap = Map<WorksheetId, Map<WorksheetId, ReferenceTypeId[]>>;
What I would rather have written instead is however something like this: export type LinkedWorksheets<T> = T<WorksheetId, T<WorksheetId, ReferenceTypeId[]>>;
...
const myMap: LinkedWorksheets<Map> = ...;
This is however not possible, because `Map` is a type constructor which expects two more type arguments `K, V` until it is a fully applied, concrete type `Map<K, V>`.With a library like this, this is probably possible (unless I've missed something which wouldn't surprise me). It would unfortunately surely be more verbose.
Still, I would be against pulling in a dependency only for something like this. The above example is simple I believe, but not exactly a "killer-app". And no, Monads aren't either (if you don't limit effects and don't have do-notation) :P
Re:Free-types: Higher kinded types in TypeScript
by ivxvm on Friday June 09, 2023 @04:03AM
(#36254757)
Not really possible, because Record and Map aren't compatible at all.
At best they both have something like `toString`.
You'll need to define at least something like RecordFunctor<T> and MapFunctor<T> to make this useful.
Re:Free-types: Higher kinded types in TypeScript
by mbwgh on Friday June 09, 2023 @04:10AM
(#36254800)
Only if you want to abstract over them at usage-site.
In my case I only ever used the concrete types and converted between them at some point.