Skip to content

In JS, property in derived class has wrong type with strict null checks on #23628

@sandersn

Description

@sandersn

With strict null checks and checkJs on:

class Base {
    constructor() {
        this.built = false
    }
}
class Derived extends Base {
    init() {
        this.built = true
    }
    build() {
        if (this.built) {
            console.log('Nothing to do')
        }
        else {
            console.log('Starting build...')
        }
    }
}

Expected behavior:
No error and Derived.built: boolean.

Actual behavior:
Derived.built: boolean | undefined, causing an error on class Derived: "Class Derived incorrectly extends class Base. Types of property 'built' are incompatible: 'boolean | undefined' =/=> 'boolean'.

Found in webpack, where Base=Module and Derived=NormalModule (and many others).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions