Skip to content

[TypeScript] Typescript plugin overwrites parent class properties when the child narrows that property's type #9105

@ethernet8023

Description

@ethernet8023

Bug Report

Current Behavior
When a class (B) extends another class (A) and includes a type definition to narrow the type of something specified in class A, it will set the narrowed property's value to undefined. This diverges from the TSC behavior.
The output from the below code under babel is a, undefined.
Input Code

  class A {
    value: any
    constructor(value: any) {
      this.value = value
    }
  }

  class B extends A {
    value: string
  }

document.write(`${new A('a').value}, ${new B('b').value}`)

Expected behavior/code
The output from the code should be a, b.

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

.babelrc:

{
  "presets": [
    "@babel/env",
    "@babel/typescript"
  ],
  "plugins": [
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread"
  ]
}

Environment

  • Babel version(s): 7.1.6
  • Node/npm version: Node 10 / NPM 6
  • OS: Any OS
  • Monorepo: doesn't matter
  • How you are using Babel: loader

Possible Solution
It seems like babel is setting value = undefined in the class property - it needs to check if that value is defined in the parent before overwriting it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions