TS2577

error TS2577: Return type annotation circularly references itself.

Broken Code ❌

1
2
3
4
5
6
7
8
9
10
11
12
13
type OurReply = ReturnType<typeof reply>;

function reply(text: string): OurReply {
if (text === 'Hello') {
return 'Hi!';
}

if (text === 'Goodbye') {
return 'Bye!';
}

return "What's up?";
}

Fixed Code ✔️

You can remove the circular reference by removing the explicit return type (OurReply) from the signature of your function:

1
2
3
4
5
6
7
8
9
10
11
12
13
type OurReply = ReturnType<typeof reply>;

function reply(text: string) {
if (text === "Hello") {
return "Hi!";
}

if (text === "Goodbye") {
return "Bye!";
}

return "What's up?";
}