rules icon indicating copy to clipboard operation
rules copied to clipboard

Using with TypeScript / Tools that parse Javascript like Jest

Open thekevinbrown opened this issue 5 years ago • 4 comments

I noticed that at some point there was Typescript support (https://github.com/jruizgit/rules/issues/15).

I don't see any type definitions in the NPM package, and @types/durable doesn't seem to work. Is there an example of how to do this somewhere?

I'm also really struggling in general because the examples don't look like normal Javascript to me. (Functions with kind of object bodies in them?) So both typescript and javascript tools like Jest are giving me syntax errors. I noticed there's mention of strict mode, but for example, how could I define something like this in TypeScript?

d.statechart('expense3', function() {
    input: {
        to: 'denied'
        whenAll: m.subject == 'approve' && m.amount > 1000
        run: s.status = 'expense3: Denied amount: ' + m.amount

        to: 'pending'
        whenAll: m.subject == 'approve' && m.amount <= 1000
        run: s.status = 'expense3: sid ' + m.sid + ' requesting approve amount: ' + m.amount
    }

    pending: {
        to: 'approved'
        whenAll: m.subject == 'approved'
        run: s.status = 'expense3: Expense approved for sid ' + m.sid

        to: 'denied'
        whenAll: m.subject == 'denied'
        run: s.status = 'expense3: Expense denied for sid ' + m.sid
    }
    
    denied: {}
    approved: {}
});

console.log(d.post('expense3', { subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { subject: 'approved' }).status);
console.log(d.post('expense3', { sid: 1, subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { sid: 1, subject: 'denied' }).status);
console.log(d.post('expense3', { sid: 2, subject: 'approve', amount: 10000 }).status);

thekevinbrown avatar Mar 24 '20 10:03 thekevinbrown

Hi, thanks for asking the question. TypeScript was supported in the very early versions of the framework. However, after publishing version 2, I haven't added TypeScript support back. I will look into this scenario and report back in a few days.

jruizgit avatar Mar 30 '20 04:03 jruizgit

Thanks @jruizgit, that'd be awesome. Also we're happy to help with contributing and/or maintaining a set of types for use with TypeScript.

The main question / difficulty at this point is around the syntax errors in Jest and TypeScript (basically any tools that want to parse the input). We intend to write a suite of tests for our rules in Jest so that we can see if rule changes in the future cause any unexpected behaviour regressions in other areas of the system that the rule author wasn't intending.

thekevinbrown avatar Mar 30 '20 11:03 thekevinbrown

Hey @jruizgit,

I think I can figure out most of this if I can get past the syntax errors. Any hints on a syntactically valid way to express the above?

thekevinbrown avatar Apr 09 '20 02:04 thekevinbrown

@jruizgit anything I can do to help here?

thekevinbrown avatar Aug 11 '20 11:08 thekevinbrown