GithubHelp home page GithubHelp logo

Comments (13)

gcanti avatar gcanti commented on August 12, 2024

Hi,
it's difficult to say at the moment. For now I'm just playing with babel's plugin APIs.
Luckily there is a plugin which I can take as reference implementation:

https://github.com/codemix/babel-plugin-typecheck

from babel-plugin-tcomb.

zerkalica avatar zerkalica commented on August 12, 2024

Why not to parse flowtypes to tcombtypes for run-time checking?

Runtime checking - only small part of the problem, what about ide support (like nuclide ide), autocomplete, static checking, how to use together tcomb and flow ?

from babel-plugin-tcomb.

majhork avatar majhork commented on August 12, 2024

Flow provides static checking for Flow types. Otherwise converting those types to tcomb and preserving the unions and intersections would allow very interesting introspective capabilities. Autocomplete and light ide support could likely be added with some very simple hacks.

from babel-plugin-tcomb.

zerkalica avatar zerkalica commented on August 12, 2024

In codemix/babel-plugin-typecheck#21 (comment) i try to describe some problems about runtime checking reflection and external libraries.

from babel-plugin-tcomb.

gcanti avatar gcanti commented on August 12, 2024

Hi all,
So I wrote the basic code in order to handle the main constructs of tcomb README.
It's already usable but now I wonder if this plugin should handle Flow types and convert them to corresponding tcomb types:

  • string -> t.String
  • number -> t.Number
  • boolean -> t.Boolean
  • Array -> t.Array
  • Function -> t.Function
  • Object -> t.Object
  • any, mixed -> t.Any

As example the following function:

function sum(x: number, y: ?number): number {
  return x + (y || 0);
}

will be compiled by the plugin to something like:

function sum(x: number, y: ?number): number {
  x = t.Number(x);
  y = t.maybe(t.Number)(y);

  const ret = function (x, y) {
    return x + (y || 0);
  }(x, y);

  return t.Number(ret);
}

from babel-plugin-tcomb.

chrisui avatar chrisui commented on August 12, 2024

I would love to see the basic flow types be implemented (ie. moving towards something like the intention of your flowcheck library.

Would be interesting to discuss how to implement the more complex types such as the flow equivilent of a Struct etc.

Maybe parsing Object type declarations look good for struct's?

from babel-plugin-tcomb.

gcanti avatar gcanti commented on August 12, 2024

Converting the basic Flow types to tcomb should be straightforward. The main problem is that AFAIK Flow can't understand structs, just classes and Object types. This weekend I'm going to play with Flow v0.17, maybe I can find something new/useful.

Maybe parsing Object type declarations look good for struct's?

It depends on your goal. If you want to preserve the Flow semantic I should add a new combinator: Object types are structural while structs are "nominal" (they use instanceof internally). Moreover I should handle classes, now this would raise an error:

class A {}

function f(a: A) {
  return a;
}

f(new A());

since the plugin will convert f to something like:

function f(a: A) {
  a = A(a); // all tcomb types are glorified identity functions

  return a;
}

from babel-plugin-tcomb.

majhork avatar majhork commented on August 12, 2024

Could you not compile to this instead?

t.Function([t.Number, t.maybe(t.Number)], t.Number, "sum").of(
    function sum(x: number, y: ?number): number {
        return x + (y || 0);
    }
);

I can see some potential problems but it would be really nice to get runtime type information for functions.
That and +1 for "this plugin should handle Flow types". A Flow user could leverage your entire collection of libraries nearly for free. Inferring forms (using tcomb-form) from functions would be awesome. I think that's the right place to explore deeper type symmetry too. Thanks for this. It looks exciting.

from babel-plugin-tcomb.

chrisui avatar chrisui commented on August 12, 2024

Ah. Had no idea Structs were nominal in this case. Still a bit naive about types and such so bear with me :)

For my use case I am after structural typing. We have no classes but just pass around state as immutable objects.

NB: A lot of what I want to do in the future is currently being inspired by the F# "design with types" series which you're probably familiar with. tcomb basically does the runtime side of this dynamically but I'm trying to find a way to bend Flow to offer the static side we need which I think is almost there.

from babel-plugin-tcomb.

ivan-kleshnin avatar ivan-kleshnin commented on August 12, 2024

What's the current status of this? If Tcomb syntax is Flow-compatible can we have both sides of the coin (static type checking + first-class types)?

P.S.

Readme does not mention that "syntax-flow" and "transform-flow-strip-types" plugins are required.

from babel-plugin-tcomb.

gcanti avatar gcanti commented on August 12, 2024

What's the current status of this?

Only tcomb types are supported. i.e this is ok:

function sum(a: t.Number, n: t.Number): t.Number {
  return a + b
}

this is not ok (throws):

function sum(a: number, n: number): number {
  return a + b
}

If Tcomb syntax is Flow-compatible can we have both sides of the coin

Yes, it would be great. Unfortunately all my attempts are failed. I suspect it's not possible but I'm open to suggestions.

All I could do is add support for primitive types:

  • string -> t.String
  • number -> t.Number
  • boolean -> t.Boolean
  • Array -> t.Array
  • Function -> t.Function
  • Object -> t.Object
  • any, mixed -> t.Any

That would be easy, the real problem comes when you consider first-class types:

const Person = t.struct({
  name: t.String
})

// Flow and TypeScript don't understand Person here
const foo = (person: Person) => ...

Readme does not mention that "syntax-flow" and "transform-flow-strip-types" plugins are required.

Thanks for pointing out

from babel-plugin-tcomb.

ivan-kleshnin avatar ivan-kleshnin commented on August 12, 2024

Thanks for the answer.

I suspect it's not possible but I'm open to suggestions.

Unfortunately, I don't even know Flow 😞
But I also guess it's close to impossible until Flow exposes some kind of toolkit for that.

from babel-plugin-tcomb.

gcanti avatar gcanti commented on August 12, 2024

Closing in favour of #11

from babel-plugin-tcomb.

Related Issues (20)

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.