futurGH / ts-to-jsdoc

Transpile TypeScript code to fully compatible JavaScript + JSDoc comments.
MIT License
174 stars 17 forks source link

Careful with class property declarations - diverging behaviour #39

Closed kungfooman closed 6 months ago

kungfooman commented 6 months ago

Given TS code:

class Frame {
  bias: number;
  constructor() {
    this.bias = 0.001;
  }
}
class Multiframe extends Frame {
  bias: number;
}
const frame = new Frame();
const multiframe = new Multiframe();
console.log('frame.bias', frame.bias);
console.log('multiframe.bias', multiframe.bias);

And tsc compiles into this JS:

"use strict";
class Frame {
    constructor() {
        this.bias = 0.001;
    }
}
class Multiframe extends Frame {
}
const frame = new Frame();
const multiframe = new Multiframe();
console.log('frame.bias', frame.bias);
console.log('multiframe.bias', multiframe.bias);

Output:

image

However, ts-to-jsdoc transpiles into:

class Frame {
    bias = undefined;
    constructor() {
        this.bias = 0.001;
    }
}
/** @extends Frame */
class Multiframe extends Frame {
    bias = undefined;
}
const frame = new Frame();
const multiframe = new Multiframe();
console.log('frame.bias', frame.bias);
console.log('multiframe.bias', multiframe.bias);

Output:

image

futurGH commented 6 months ago

It's a combination of useDefineForClassFields, which is true by default but likely false in your project, producing

class Frame {
    bias;
}

and https://github.com/futurGH/ts-to-jsdoc/blob/64f2176cfeab0f9f6cab2ad29c9c4d9db083c598/index.ts#L230-L232

I'm not entirely sure why I had that check in the first place, going to see if it needs tweaking.

futurGH commented 6 months ago

Fixed in 2.0.0!