Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
bgnx opened this issue Jul 6, 2021 · 0 comments · Fixed by #47818
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@bgnx
Copy link

bgnx commented Jul 6, 2021

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

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 7, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.5.0 milestone Jul 7, 2021
@andrewbranch andrewbranch added the Rescheduled This issue was previously scheduled to an earlier milestone label Dec 7, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
4 participants