Skip to content

Readonly vs. litteral type breaking change? #12148

Closed

Description

@stephanedr

TypeScript Version: 2.1.1

Code

class A {
    readonly n = 0;
    constructor() {
        this.n = 1;  // Type '1' is not assignable to type '0'
    }
}

class B {
    constructor(readonly n = 0) {}
}

class C extends B {
    constructor() {
        super(1);  // Argument of type '1' is not assignable to parameter of type '0 | undefined'.
    }
}

Expected behavior:
Is this behavior expected (breaking change vs 2.0.8)?
To me this should be valid, particularly the 2nd case.

Actual behavior:
Errors displayed in comments.

Activity

  1. added this to the milestone on Nov 10, 2016
  2. falsandtru commented on Nov 10, 2016

    @falsandtru
    Contributor
  3. mhegazy commented on Nov 14, 2016

    @mhegazy
    Contributor

    All parameter initializes should be widened to their base (e.g. class B), property initializes (e.g. class A) are intentional breaking change.

  4. sandersn commented on Nov 16, 2016

    @sandersn
    Member

    Fix is up at #12312

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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