Skip to content

Missing type check on field initializer when exporting a class without constrtuctor #1150

Closed
@saulecabrera

Description

@saulecabrera

While working on #482 I found out that when an exported class without constructor defines a property with an explicit initializer there's no explicit typecheck.

Reproduce

  • Export a class
  • Do not define a constructor
  • Add a field with an incorrect initializer
export class Foo {
  a: i32 = "44";
}

The previous example compiles without errors.

Expected behaviour

The expectation would be to report a type error back, just like when the class defines a constructor:

export class Foo {
  a: i32 = "44";
  constructor() {}
}
ERROR TS2322: Type '~lib/string/String' is not assignable to type 'i32'.

Analysis

From my investigation, this happens only in exported class definitions without constructor because the compilation path for an exported class is slightly different than an instantiation.

When compiling an instantiation, if the class doesn't define a constructor a default one is created, on the other hand, when compiling a class export with no constructor, field initialization is never triggered and consequently, no type errors are reported back.

Should a default constructor be created in this case as well? Or are there any drawbacks with this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions