StoneCypher / jssm

Fast, easy Javascript finite state machines with visualizations; enjoy a one liner FSM instead of pages. MIT; Typescripted; 100% test coverage. Implements the FSL language.
http://fsl.tools/
MIT License
330 stars 23 forks source link

TypeScript type declarations file #392

Closed geopic closed 4 years ago

geopic commented 4 years ago

So what's the feature?

A type declarations file for TypeScript developers to utilise the power of TS with this module.

Is this related to a problem? If so, what?

Many developers write with TypeScript nowadays so it would be beneficial for them to have the types of returned values, arguments etc be made explicit.

Describe the solution you'd like

An easier time using JSSM for a developer writing with TypeScript! :smile:

Describe alternatives you've considered

N/A

Additional context as you see fit

N/A

If you'd like then I can start on this myself this week. Cheers

StoneCypher commented 4 years ago

That would be great. This is something I've wanted for a while now but I didn't know how to get started.

Someone just brought this up on Reddit the other day, and I had asked. Is this related to that, or a coincidence?

Assistance would be lovely. Thank you

geopic commented 4 years ago

That would be great. This is something I've wanted for a while now but I didn't know how to get started.

Very well, I'll get started on it soon.

Someone just brought this up on Reddit the other day, and I had asked. Is this related to that, or a coincidence?

Related, I was the OP of that post.

StoneCypher commented 4 years ago

Okay, this is great.

Some things. I'm trying to be helpful, but I don't really know how, so feel free to ask questions.

  1. I'm open to changes. If a type would be better this other way, or whatever, let me know.
  2. There are two things of interest to outsider users: the source folder /src/js has what I think probably a typescript user would want, the raw source, and the dist folder /dist has the rollup builds (there's an es6, a cjs es5, an iife es5, plus maps for each.)
    1. I kind of assume that for this, only the /src/js folder will matter? If that's not correct that's fine, but please help me understand
  3. I suspect main and module in my package.json are at least slightly wrong on technicality grounds and I'm worried that'll be germane here
    1. I don't know how to differentiate ts from es6 when there's also present es5
    2. I am willing but reluctant to give up on es6 in favor of ts. I would prefer to allow one to be nominated explicitly as a replacement but I don't know how
  4. I believe my typings to be reasonably strong but I'd be delighted to receive improvementts
  5. Yes, they're universally generic on type for a reason, despite that that's currently unused. Eventually that type will optionally parameterize the inner shape of the variable set that doesn't yet exist
    1. Yes, I've had that for so long that it was a futuristic new flow feature
  6. I think probably the only two things you'll care about, other than the test set, are jssm.ts and jssm_types.ts

All you need to do is clone, npm install, npm run build

You should see around 1200 passing tests

Thank you for any help you're ready to offer 😊

StoneCypher commented 4 years ago

Largely the outside primarily only interacts with sm and Machine, and to a much lesser degree make, compile, and parse

I think sm and Machine will be the hard ones

Machine is the type for the machine itself. Largely interaction with it is getters and setters

sm is a tagged template string. Use of it is as follows:

function MakeMyMachine() {
  return sm`Red -> Green;`;
}

Not a particularly useful example, but, the tagged template is going to call the hojo of building and compiling for you, so that you can get on with your day

The interface for a tagged template is ... kinda gross, honestly. It's javascript's fault; I didn't choose this.

But if you want a tagged template to consume inclusions, the first argument is the template string broken into pieces as an array, where the interstitials are the inclusion locations like you'd expect, and ...

... and ...

(sorry I get a little choked up thinking about this, big dramatic fake cry scene, oscar nomination)

... and the things to be included aren't in a second array like you'd want at all. They're spread across the remainder as varargs.

so calling

foo`a${1}b${2}c${3}d`

will come into foo/N as (['a','b','c','d'],1,2,3)

Because I am an upright citizen of good moral character, I've never tried to learn how varargs work in Typescript, and I'm a little worried that just saying the word lowered my credit score

geopic commented 4 years ago

Wow, thank you very much for the help! 😍 It'll all come in very handy, I'm sure.

geopic commented 4 years ago

So I've looked through the files in src/js, they're a little chaotic and I'm not sure if I should write a definition file if something is going to change. Also on line 287 of jssm_types.ts, my IDE is telling me JssmCompileSeStart is not generic. It has a type argument when JssmCompileSeStart has no type parameter. Should it have one? :confused:

StoneCypher commented 4 years ago

So I've looked through the files in src/js, they're a little chaotic

I'm open to criticism 😄

.

I'm not sure if I should write a definition file if something is going to change.

Other than ripping flow out, and additions, nothing significant has changed in around three years

A lot has been added

I will probably major 6 before I make any significant non-additive changes

.

Also on line 287 of jssm_types.ts, my IDE is telling me JssmCompileSeStart is not generic. It has a type argument when JssmCompileSeStart has no type parameter. Should it have one? 😕

Hm. Yes, it should.

It's not immediately apparent why Typescript isn't balking at that. This was a really old project, that was packaged before the type checkers existed, so Typescript is being invoked through Rollup, which may be tree shaking that out

I should probably invert this to my more common modern structure of separate transpilation steps. I wonder how many other bugs that'll shake out

geopic commented 4 years ago

You need to add a simple and easy API reference of everything the developer can use with this project, the traffic light example is the only accessible thing I can see but even then the properties in the object don't actually exist anywhere in the codebase. Let me know when you have full documentation of everything public and I'll write a types declaration file for it.

StoneCypher commented 4 years ago

Could you just do a couple functions? Then I'll do the rest

I just need to see the basics

StoneCypher commented 4 years ago

nevermind, i'll do it

StoneCypher commented 4 years ago

This is available here by the way, in case someone looks this issue up