Skip to content

Computed properties do not respect setter signatures #55168

Closed
@miyaokamarina

Description

@miyaokamarina

Bug Report

Computed property assignments do not respect corresponding setter signatures.

🔎 Search Terms

variant accessors, setter, computed properties, write type

🕗 Version & Regression Information

  • Affected versions: 4.3 to 5.1.6 (current stable).

⏯ Playground Link

Playground Link (5.1.6)

💻 Code

const k = Symbol();

const enum Props {
    k = 'k',
}

interface Foo {
    get k(): Set<string>;
    set k(v: Iterable<string>);

    get [k](): Set<string>;
    set [k](v: Iterable<string>);
}

declare const foo: Foo

// OK:
foo.k        = ['it', 'works'];

// Errors:
foo['k']     = ['should', 'work'];
foo[Props.k] = ['but', 'actually'];
foo[k]       = ['does', 'not']

// Getters work as expected:
const values = [foo.k, foo['k'], foo[Props.k], foo[k]] as const;
//    ^? const values: readonly [Set<string>, Set<string>, Set<string>, Set<string>]

🙁 Actual behavior

Getter type is used at the LHS of an indexed/computed property assignment.

🙂 Expected behavior

Setters should accept their declared types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions