disnet / scrap-js

A declarative data type construction and manipulation library for JavaScript
11 stars 0 forks source link

Share scrap type definition with typescript code #1

Open aurium opened 4 years ago

aurium commented 4 years ago

Wonderful! 😍❤️💕

It is not possible to see this and not think about typescript. How to share this type definition with a ts code without repeating that?

Is this possible to have the same feature with decorators and class definitions instead tagged literals? I think it can get highlighting benefits and somehow share typing definition.

let { Node, Leaf } = scrap`
data Node { left: Node | Leaf, right: Node | Leaf }
data Leaf { data: any }
`;

could be

@scrap class Node {
  @data('Node | Leaf') left: Node | Leaf
  @data('Node | Leaf') right: Node | Leaf
}
@scrap class Leaf {
  data: any // A prop without decorator could be inferred as type `any` by `@scrap`.
}

I can't see now how to not repeat union types or non base types, however it is editor friendly and will allow highlighting and autocomplete.

This second example (if i'm right) can use type inference and we don't need to repeat:

let { Pair } = scrap`
data Pair { left: number, right: number }
`

could be

@scrap class Pair {
  @data.number left
  @data.number right
}

...OR...

Do scrap really need to know the data type while using typescript? If it don't, so the double typing problem is gone. A base Scrap class to be extended must be enough.

disnet commented 4 years ago

Yeah as I’ve been using scrap more I’ve been wanting TS integration as well.

I really wanted to avoid reusing/abusing class and decorators because the syntax gets hairy very quickly. Better to have a targeted DSL imo.

It might be possible to add a feature where scrap generates a typescript declaration file.

aurium commented 4 years ago

I agree against abusing decorators. :-)

A typescript declaration file looks valid approach. However that means one more build step. It will get harder to use with a watcher builder.

aurium commented 4 years ago

Another issue: A ts declaration must consider some details from ts code where the scrap is defined, like the var name, namespace and module. May be simpler to make a new file format .scrap and the compiler to export JS or TS. (I'm not ask to replace the current feature with tagged literals, just to add a transpiler option)