Repository navigation
Closed
Labels
area: typescripti: questionoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue
Description
💻
- Would you like to work on a fix?
How are you using Babel?
@babel/cli
Input code
interface BaseOptions {
test1?: boolean;
}
abstract class BaseClass {
public readonly options: BaseOptions;
public constructor (options: BaseOptions) {
this.options = options;
console.log('BaseClass this', this);
}
}
interface ChildOptions extends BaseOptions {
test2?: number;
}
class ChildClass extends BaseClass {
public readonly options!: ChildOptions;
public constructor (options: ChildOptions) {
super(options);
console.log('ChildClass this', this);
}
}
export const t = new ChildClass({
test1: true,
test2: 2,
});Configuration file name
babel.config.json
Configuration
{
"parserOpts": {
"strictMode": true
},
"sourceMaps": "inline",
"presets": [
[
"@babel/preset-env",
{
"targets": { "node": "current" },
"modules": "commonjs"
}
],
"@babel/preset-typescript"
],
"plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }], "@babel/plugin-proposal-class-properties"]
}Current and expected behavior
Current behavior:
_defineProperty is present in the generated code in the constructor after super call
class BooleanResolvableType extends _Base.BaseType {
constructor(options) {
super(options);
_defineProperty(this, "options", void 0);
console.log('BooleanResolvableType constructor options', options);
console.log('BooleanResolvableType constructor this', this);
}
async run(val, context) {
console.log('BooleanResolvableType run this.options', this.options);
const lval = val.toLowerCase();
return await this.options.resolver(lval, context);
}
}Expected behavior:
_defineProperty should not be present in the constructor for the fields defined in a parent class
Environment
System:
OS: Windows 10 10.0.19044
Binaries:
Node: 16.13.0
npm: 8.3.1
npmPackages:
@babel/plugin-proposal-class-properties: 7.16.7
@babel/plugin-proposal-decorators: 7.17.2
@babel/preset-env: 7.16.11
@babel/preset-typescript: 7.16.7
eslint: 8.6.0
jest: 27.4.7
Possible solution
No response
Additional context
No response
Metadata
Metadata
Assignees
Labels
area: typescripti: questionoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue
Activity
babel-bot commentedon Feb 21, 2022
Hey @nowifi4u! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.
If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.
nicolo-ribaudo commentedon Feb 21, 2022
This is the expected behavior to match what JavaScript does. You can test it by running this code in your browser's console:
The current TypeScript behavior of ignoring
xinBis wrong, but you can enable theuseDefineForClassFieldsin your TS config to make it work correctly.If you want to ignore
xin the subclass, you can use thedeclaremodifier:TS playground