Interop with JS/TS classes

I can’t seem to find anything regarding interop with classes defined in a JS/TS library. Specifically, is it possible to use ReScript to extend a class defined in an external lib?

I’m asking because I’m attempting to use the AWS CDK, which defines everything via classes (ugh!), and in order to define a new stack, I have to extend the cdk.Stack class defined in the @aws-cdk/core library. For example, here is how I would start out defining my own stack in TS:

import * as cdk from '@aws-cdk/core';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here
  }
}

Is it possible to translate this into ReScript somehow?

If this is a futile task, no worries. If so, I guess I’ll have to stick with TS for this particular effort. I was just wondering if there were any way for me to use ReScript here instead of TS, as I’m looking for some smallish tasks to help me learn ReScript.

Unfortunately it’s not possible to define/extend JS classes in idiomatic ReScript. However, it’s pretty easy to bind to an existing class. I would recommend defining them in JS or TS for now, and then writing some ReScript bindings to the class if you need it.

3 Likes

Thanks for suggestion!