-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
TypeScript Version: 3.0.1
Search Terms:
- "implicitly has return type 'any'"
- TS7023
Code
interface Identifiable {
name: string;
nickname: string;
isNameSameAsNickname: boolean;
}
class Person implements Identifiable {
public name = "Baz";
public nickname = "Baz";
get isNameSameAsNickname() {
return compareNames(this);
}
}
function compareNames(identifiable: Identifiable) {
return identifiable.name === identifiable.nickname;
}
Expected behavior:
Compiles without error
Actual behavior:
The following error occurs:
TS7023: 'isNameSameAsNickname' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
Comment: This error appears to be incorrect (or at least worded ambiguously) because isNameSameAsNickname
is not referenced directly or indirectly in one of its return expressions. Specifically, isNameSameAsNickname
does not appear directly in the return expression compareNames(this)
nor does it appear indirectly in identifiable.name === identifiable.nickname
.
Workaround:
Declare the return type explicitly:
get isNameSameAsNickname(): boolean {
return compareNames(this);
}
Playground Link:
This error is indicated in the playground with a red squiggle below isNameSameAsNickname()
.
Note: Error only occurs if noImplicitAny
option is checked
Related Issues:
- Open: Over-eager inference with ThisType & homomorphic mapped types #18805
- Closed: Suggestion: Try to avoid forcing any type on types that reference themselves (especially around JSX) #22390, noImplicitAny reports incorrect error? #8546, Type assertion with
as
operator does not work on recursive functions #5403