Skip to content

A class with only getters satisfies some type without checking for the possibility of changing these fieldsΒ #58141

Closed as not planned
@Crinax

Description

@Crinax

πŸ”Ž Search Terms

getters, types, classes, classes with getters

πŸ•— Version & Regression Information

  • This is incorrect type inference
  • This is the behavior in every version I tried, I tried on Nightly, 5.4.4, 3.3.3, 4.3.5

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.4#code/C4TwDgpgBAggCgJwPZgM5QLxQN4CgoFQCWAJgFxQB2ArgLYBGECA3LgL664DGANgIap0MHPkJcklVMATUuwJAgAUYBEQBufYNAQQ+JCTxBQVKVBXjI0AShwdRBAOYRgxEoqsUaDJiMJ+oOsDUCJRQwAAWRKgAdCZo0aSsfhx24pIuEOaIpphUEADusIrYrhQAjFBsVqy4EAkkuQDMzEA

πŸ’» Code

type AProps = {
    id: number;
}

class A {
    constructor(private readonly props: AProps) {}

    get id(): number {
        return this.props.id;
    }
}

const e: AProps = new A({ id: 1 });

e.id = 3;

πŸ™ Actual behavior

Fails on execute without typescript errors

πŸ™‚ Expected behavior

It is necessary to notify the user that the class type and the user-defined type of the function argument are incompatible with each other

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions