Skip to content

TypeScript definite assignment emit #10311

@matt-tingen

Description

@matt-tingen

Bug Report

Current Behavior
In loose mode, @babel/plugin-proposal-class-properties emits a property assignment for TypeScript definite assignments.

Input Code
REPL (I'm not sure if it's possible to turn turn on loose mode for the plugin)
TS Playground

// Like Object.assign but will not assign properties which are already defined
declare const assignDefaults: typeof Object.assign

class Foo {
  bar!: number;

  constructor(overrides: { bar: number }) {
    assignDefaults(this, overrides);
  }
}

Expected behavior/code
A definite assignment should not emit any JS.

// Like Object.assign but will not assign properties which are already defined
class Foo {
  constructor(overrides) {
    assignDefaults(this, overrides);
  }

}

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": ["@babel/preset-typescript"],
  "plugins": [["@babel/plugin-proposal-class-properties", { "loose": true }]]
}

Environment

  • Babel version(s): 7.5.5
  • Node/npm version: Node 12.6 / yarn 1.15.2
  • OS: macOS 10.14.5
  • Monorepo: no
  • How you are using Babel: cli

Additional context/Screenshots
This was previously raised in #7997, but considered expected behavior. It was mentioned that it could be reconsidered for loose mode, however.

I do feel like it should be reconsidered one way or another; emitting for these declarations is problematic in the same way emitting for a declare would be. Additionally, this syntax is not valid JS so there the only applicable spec compliance is that of TS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions