hkts | A simple encoding of higher-kinded types in TypeScript | Natural Language Processing library
kandi X-RAY | hkts Summary
kandi X-RAY | hkts Summary
TypeScript doesn't directly support higher-kinded types yet, but various attempts have been made to simulate them (see related work at the bottom). This project presents a new, greatly simplified approach to encoding HKTs using the power of conditional types. The idea is that, although we can't truly abstract over a type constructor type T = ..., we can abstract over the result T<_> of applying it to a special placeholder type _. Then, if we can somehow substitute all instances of _ within a type, we effectively have the ability to "apply" T at arbitrary types. That is, we can abstract over T! And it turns out we can define a substitution operator $ which does just that.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of hkts
hkts Key Features
hkts Examples and Code Snippets
Community Discussions
Trending Discussions on hkts
QUESTION
I'm having some issues with using io-ts
. I've found that its really lacking documentation, most of the progress I've made is through GitHub issues. And no I don't understand the HKTs so no help there.
Basically I'm creating a type elsewhere, type
which has a certain set of keys:
ANSWER
Answered 2020-Apr-20 at 16:25I'm no expert in io-ts, but looking at this more closely, I'm thinking it isn't a straightforward thing to implement. The methods in a Schemable
build schemables out of other schemables. The type()
method takes a properties
object whose keys are the keys of the desired object, and whose properties are schemables for each of the properties. The partial()
method takes the same properties
object, but builds a schemable where the properties are optional. You can see how these allow you to build the schemable; you have a properties
object with all the keys you need to test for.
But given a schemable for type T
, it isn't possible in general to convert it into one for Partial
. The properties
object that was passed into type()
is not accessible in its result. It's been lost; thrown away. If all you have, say, is a type guard function that returns true
if and only if it's given a value of type T
, you can't use that function to make another one that produces true
if and only if it's given a value of type Partial
. You need more details of T
from some source outside of the guard function, such as that properties
object which is gone now.
So you can't do a straightforward composition of schemables to get this done.
The only way to do this would be to write your own Schemable
that stores enough information about the Schema
that calls it so that it can return a new schema where, say, the top-level call to S.type()
is replaced with a call to S.partial()
. Even that wouldn't be perfect, if you want to see your partial distribute across unions or down into intersections. Essentially it would have to perform surgery on the Schema
, unwrap a top-level call to S.type()
to get at the lost properties
object, and rewrap it with S.partial()
.
It's easy enough to make one that turns all calls to S.type()
into S.partial()
(just replace your old schemable with a new one whose type
property is a copy of the partial
property), but that would end up making any nested objects partial as well as the top-level one.
Here's the horrible thing I did that kind of seems to work:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install hkts
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page