Giter VIP home page Giter VIP logo

free-types's Introduction

free-types

free-types | Use Cases | Documentation | Guide | Algebraic data types

A type-level library enabling the creation and the manipulation of type constructors which can be detached from their type parameters.

Such "free" type constructors can be passed around and composed, making known programming patterns also available at the type level.

The goal of this library is to support type-level programming and dependency inversion. Distinctive features are support for: type constraints, partial application, composition with built-in adapters and higher order types, type constructor inference, all sorts of arities and a few experimental features such as pattern matching with destructuring.

Related projects: free-types-transform, type-lenses.

How to install

npm install free-types

How to use

Given a generic class Foo with some type constraint:

class Foo<T extends number> {
    constructor(private value: T) {}
}

We can create a free type constructor:

import { Type, A } from 'free-types'

interface $Foo extends Type<[number]> {
    type: Foo<A<this>>
}

Which can then be passed as an argument to another type responsible for applying it.

import { apply } from 'free-types';

type Foo42 = Apply42<$Foo>; // Foo<42>

type Apply42<$T extends Type<[number]>> = apply<$T, [42]>;

If you don't know what to do with this, here are a few use cases. More utils can be found in the documentation and you can also follow the guide if you want to make the most out of the library and understand how it works.

Happy typing!

Limitations and pain points

Workarounds for the following problems can be found in the guide and in the documentation:

  1. The expansion/evaluation of free types is not deferred until they receive arguments;

  2. Types using procedural type constraints don't work well with generics;

  3. Free types currently support neither dependent type constraints nor procedural type constraints;

  4. Free types arguments are covariant;

  5. Intersected tuples don't spread properly.

Type safety

I was a bit heavy on type safety, in part to prove that it could be made type safe. As a result, some types are harder to use than they should, especially given that some limitations make correct expression not compile. I usually deal with this with an optional parameter, which is terrible. I am very open to discussion on that matter.

Performance

My experience with free-types is that the overhead is not an issue unless you nest them like crazy. A good example is Show which I purposefully wrote with combinators staked on top of combinators in a big recursive switch. Benchmarking it against a vanilla implementation is on my todo list but it's clearly a monster type. My advice is to be reasonable and to remember that real programming happens at runtime.

I experimented a lot with this library, not everything is optimised, which is why I divided it into subsets to clarify their level of performance and value.

Querying a subset also has an impact on the initial type instantiation duration, so you are advised to import only what you need if you can't rely on incremental builds.

  • free-types/core
    everything you need to create, apply and unwrap free types
    takes 125-210ms to instantiate with tsc --generateTrace

  • free-types/essential
    composed of core + everything you need to compose, adapt and lift free types
    takes 450-500ms to instantiate with tsc --generateTrace

  • free-types
    composed of essential + utility-types
    takes 660-725ms to instantiate with tsc --generateTrace

  • utility-types
    companion types divided into subsets such as /strings, /arithmentic, /logic or /tuples

  • experimental
    companion types: From, FromDeep, Match, Widen and Show

free-types's People

Contributors

geoffreytools avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

brookshuihualee

free-types's Issues

Can the documentation be made a little less opaque?

I think this library is one that I'm looking for, but I'm finding it extremely difficult to reason about the core types and the problems they solve. Mostly what's missing are examples, like real-world data where the resulting type is narrowed (or expanded? ๐Ÿค” ) by using a free type in a way that TS's utility types don't do.

For example, in the documentation, you jump straight into Type, and it says we're typing arguments. Arguments of what? A function? A function defined where? How does this type affect the input and output types of a given function? How do functions that have a type with just Input in Type differ from those with Output?

I feel like this is somewhat like allowing generics to infer generics, but I can't really tell. I know it's difficult to write documentation be putting aside that you already know exactly what's happening, but even with an advanced knowledge of TypeScript types, I feel like there isn't a lot of bridging to what this library is for and how it's used.

Create a free type constructor

First off, thanks for the work you've put in in this library. It's quite incredible. The documentation is on par of libraries with 100x stars than you have. ๐Ÿ™

At the bottom of your Guide page you have this little line:

Another option for removing boilerplate would be to contribute to the introduction of free type constructors into the language natively ๐Ÿ˜

This is the only thing that is holding me back when using this library. I'm able to encapsulate nearly everything regarding the challenging typing I'm trying to accomplish, except that I require consumers of my function to create a free type for each of their use-cases. Would be awesome if there was a way for them to provide the type they already have to some sort of constructor (better if I can do that for them somehow) instead of having to know about/understand/maintain a free type.

Anyways, I have no idea how to accomplish this, but I wanted to start the thread for discussion and maybe someone way smarter than me can come along and contribute or provide some insight.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.