Skip to content

Type check for interface does not preserve "readonly" #40145

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
yw662 opened this issue Aug 20, 2020 · 3 comments
Closed

Type check for interface does not preserve "readonly" #40145

yw662 opened this issue Aug 20, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@yw662
Copy link

yw662 commented Aug 20, 2020

TypeScript Version: 3.9.2, 3.9.7 and 4.0.0-beta

Search Terms:
interface, readonly
Code

"use strict"
interface foo {
    // members are defined read-write
    bar: number
    baz: number
}

function zero(f: foo) {
    f.bar = 0
    f.baz = 0
}

class bug implements foo {
    // members implemented read-only
    get bar() {return 0}
    readonly baz = 0
}
const newBug = new bug()
zero(newBug)

Expected behavior:
It should not compile.

Actual behavior:
It compiles, resulting in runtime error "Cannot set property bar of # which has only a getter"

Playground Link:
https://www.typescriptlang.org/play?ts=4.0.0-beta#code/EQVwzgpgBGAuBOBLAxrYAoRA7WF4DMBDZafAezKgG90o6oB6BqAWwhYCM8wpD5oAJhHzYIAqP0ICAtAHckuWvQ58AXFCwhOeJXRUAvdZu3x0AX3Tp8ILKkRksUfXjIAKfOvJkAlNV1R8ADoVeCgAXigABn8gg3Co80tkABtCMB4OEABzKEQWAAdk9ggcHi8-ekZmNhMePMLinDEJCClpB2SAT38siFgoENdfKn5YEHhHSItKyQEOzoHCfXjoi2QHOA0IWQAhbPisbYHsofRneDdD3ezvIA

Related Issues:
#39871, but simpler

@yw662
Copy link
Author

yw662 commented Aug 20, 2020

Explanation about why it should not compile:

class bug does not actually implemented interface foo. class bug provides a readonly bar: number, which does not satisfy the required bar: number in interface foo.

@RyanCavanaugh
Copy link
Member

Duplicate #13347

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 20, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants