Ts-bridge - Call PureScript code from TypeScript

Hi!

I’m happy to announce the release of the ts-bridge library. It’s a type generator that lets you call PureScript code from TypeScript. I’ve been interested in this topic since a while and tried several approaches along the way. I was faced with this problem first when I was working on a project that was a hybrid of PureScript and TypeScript. In this case TypeScript React was used for rendering the views and the whole state machine logic was written in PureScript. So we needed a solution for smooth interoperability between the languages. Another usecase would be to integrate PureScript gradually into an existing TypeScript codebase.

Library:
purescript-ts-bridge

Demo Project:
purescript-ts-bridge.demo

Hope it’s helpful to increase the adoption of PureScript, any kind of feedback welcome!

13 Likes

The library is very good and useful. I have come across some of your source code( Types.purs), and I have a few observations that might be helpful:

1.- Instead of using unsafeRegex, I suggest using fromJust to handle Nothing in a more explicit way, to avoid runtime exceptions:

import Data.Maybe (fromJust)

pursModuleNameRegex :: Regex
pursModuleNameRegex = fromJust $ regex "^([A-Z][A-Za-z0-9_]*)(\\.[A-Z][A-Za-z0-9_]*)*$" noFlags

2.- Instead of using a wildcard, I suggest using a named argument to make the code more clear. Here’s an example:

printError :: AppError -> Doc
printError err = case err of

3.- In Typescript, undefined is not a reserved word. Therefore, I would remove it.

4.- To improve code readability, I suggest simplifying conditional checks in mkName by using String.startsWith and String.contains from the Data.String module.

Congrats for the excellent job!

Hi @pepitoscrespo !

Thanks for your feedback! Let me respond to the points one by one…

  1. This use of unsafeRegex is intentional. If the regex is wrong it’s not a user error, but a developer error. One that I cannot easily capture statically. So I want it to throw an exception as soon as possible. Change the regex to something invalid and the test suite does not succeed anymore. The code that you provide does not compile (regex returns an Either, fromJust vs. fromRight, unsafePartial handling)

  2. I think that’s a matter of style. In my view it’s very clear in this case due to the type signature.

  3. That’s a good point! I actually added it manually to the end of the reserved words list that I copied from somewhere. Maybe I will remove it in a future release. I think it does not harm too much.

  4. I’m not sure If I understand correctly what you propose here. Keep in mind that I need to check each character against a regex, don’t know how startsWith would help here.

If you have concrete suggestions, you can always create a GH issue and/or a PR and we can discuss it there in detail!

Thank you for your answer, and again for your excellent library. Yes, the code should handle an Either, not Just. Then, I would do:

tsNameRegexRest :: Regex
tsNameRegexRest = fromEither (error "Invalid regex for tsNameRegexRest") $ regex "[A-Za-z0-9_$]*" noFlags

But if you say that your suite catches the stuff and doesn’t leak to runtime, then it would be okay as you have done.

This is super cool, I’ve yet to try it.
Is there any chance you’d feel comfortable making a video demo of how to use it? Vielleicht even in German :smiley: