Skip to content

Cannot define getter and setter, getting an error: "circularly references itself in mapped type" #44906

Closed
@bgnx

Description

@bgnx

Cannot define getter and setter, getting an error: "Type of property '..' circularly references itself in mapped type ..."

interface User {
    firstName: string,
    level: number,
    get bestFriend(): User
    set bestFriend(user: SerializablePartial<User>)
}

type FilteredKeys<T> = { [K in keyof T]: T[K] extends number ? K : T[K] extends string ? K : T[K] extends boolean ? K : never }[keyof T];

type SerializablePartial<T> = {
    [K in FilteredKeys<T>]: T[K]
};

Error: Type of property 'bestFriend' circularly references itself in mapped type '{ [K in keyof User]: User[K] extends number ? K : User[K] extends string ? K : User[K] extends boolean ? K : never; }'.(2615), Link to playground

And here is almost the same valid example without error where I only changed getter get bestFriend(): User to method getBestFriend(): User and setter set bestFriend(user: SerializablePartial<User>) to method setBestFriend(user: SerializablePartial<User>): void. Link to playground

interface User {
    firstName: string,
    level: number,
    getBestFriend(): User
    setBestFriend(user: SerializablePartial<User>): void
}

type FilteredKeys<T> = { [K in keyof T]: T[K] extends number ? K : T[K] extends string ? K : T[K] extends boolean ? K : never }[keyof T];

type SerializablePartial<T> = {
    [K in FilteredKeys<T>]: T[K]
};

So semantically it's all the same, the difference is only in syntactic sugar - using getter and setter instead of "getX" and "setX" methods and seems like it should have the same type checking path

🔎 Search Terms

circularly references itself in mapped type

🕗 Version & Regression Information

v4.3.5

⏯ Playground Link

Playground link with bug

Playground link with working get-set-methods version

🙁 Actual behavior

getting an error

🙂 Expected behavior

no error

Relevant issue

There already was some issue with "circularly references itself in mapped type" in #38279 and it was fixed in #38653 but seems like it was the fix only for methods not for getter/setters

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions