Search code examples
c#typescriptimmer.js

C# generate TypeScript class with TypeGen and set [immerable] = true


We are using TypeGen to generate TypeScript classes from our C# classes.

It works like this:

[ExportTsClass]
public class MyClass
{
    public int MyProperty { get; set; }
    public string MyField { get; set; }
    public string MyProperty2 { get; set; }
}

Generates:

export class MyClass {
    myField: string;
    myProperty: number;
    myProperty2: string;
}

https://typegen.readthedocs.io/en/latest/whatisgenerated.html

This works really well but we have now started using Immer with React Redux. For classes to work it needs to set [immerable] = true like any of the examples below:

import {immerable} from "immer"

class Foo {
    [immerable] = true // Option 1

    constructor() {
        this[immerable] = true // Option 2
    }
}

Foo[immerable] = true // Option 3

https://immerjs.github.io/immer/docs/complex-objects

Is this possible to generate via TypeGen?

import {immerable} from "immer"

export class MyClass {
    [immerable] = true
    myField: string;
    myProperty: number;
    myProperty2: string;
}

I have looked at all TypeGen attributes but haven't found anything that seems to work. TsTypeAttribute could go a long way but no idea how to solve brackets around immerable.

https://typegen.readthedocs.io/en/latest/attributes.html#tstypeattribute


Solution

  • Solved like this:

    import MyClass from "../MyClass";
    import { immerable } from "immer";
    export class ImmerMyClass extends MyClass {
        [immerable] = true;
    }