日本語

How to Fix TS2774 When You Forget to Call a Function in TypeScript

​​TypeScript​Troubleshooting

The TypeScript error error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? appears when you put a function in an if condition and forget to call it with (). The function always exists, so the condition is always true. Calling the function fixes the error.

You usually see this with functions like isLoggedIn(), which returns whether the user is logged in, or user.isAdmin(), which returns whether the user is an admin, when they are used to decide what to show on screen or to check permissions. It appears in editor diagnostics and in tsc type checking. If you leave the call out and the code runs anyway, you get a bug where non-admin users see a link to the admin screen.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, npm 10.9.9, and TypeScript 7.0.2. I also checked the editor quick fixes with the TypeScript 5.9.3 language service (tsserver). I did not test in the screens of editors such as VS Code. I ran the language server that editors use directly from a script. The example that uses navigator.clipboard was checked only with type checking; I did not run it in a browser.

Trigger TS2774 and read the message

I installed TypeScript 7.0.2 in a folder created with npm init -y and put the following tsconfig.json in it.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "esnext",
    "moduleResolution": "bundler",
    "strict": true,
    "noEmit": true
  },
  "include": ["src"]
}

I wrote a function in src/menu.ts that returns the menu items shown to the user. The intent was to add an "admin screen" item only for admins, but I forgot the () on user.isAdmin on line 8.

ts
type User = {
  name: string;
  isAdmin(): boolean;
};

export function menuItems(user: User): string[] {
  const items = ["ホーム", "設定"];
  if (user.isAdmin) {
    items.push("管理画面");
  }
  return items;
}

When I ran npx tsc, the following error appeared, and the exit code was 1.

console
$ npx tsc
src/menu.ts(8,7): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

(8,7) is line 8, column 7, which is where user.isAdmin starts. The message means "This function is always defined, so this condition is always true. Did you mean to call it instead?" With --pretty, a squiggly underline marks the part of the condition being reported (I removed the control characters used for color).

console
$ npx tsc --pretty
src/menu.ts:8:7 - error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

8   if (user.isAdmin) {
        ~~~~~~~~~~~~


Found 1 error in src/menu.ts:8

I also checked what happens if the code runs as it is. As the Node.js 22 documentation describes, Node.js can run .ts files directly from version 22.18.0 by stripping the type annotations. So in a folder whose package.json contains only {"type":"module"}, I called menuItems without type checking. I passed a non-admin user whose isAdmin returns false.

ts
import { menuItems } from "./menu.ts";

const guest = { name: "たま", isAdmin: () => false };
console.log(menuItems(guest));
console.log(!guest.isAdmin);
console
$ node run-menu.ts
[ 'ホーム', '設定', '管理画面' ]
false

The admin screen item was added even though the user is not an admin. The if checks the function itself, and a function is treated as true. The false on the second line is the function negated with !. I use this result later when discussing !isLoggedIn. The TypeScript 3.7 release notes also give an example of this check's purpose: a function that tells whether the user is an admin is left uncalled, so non-admin users can change settings.

Calling the function fixes it

I changed line 8 to user.isAdmin() and ran npx tsc. It printed nothing and exited with code 0.

ts

if (user.isAdmin()) {

TS2774 is not limited to if conditions. I put several forms in one file, using a declared isLoggedIn().

ts
declare function isLoggedIn(): boolean;

if (isLoggedIn) {
  console.log("マイページ");
}

const label = isLoggedIn ? "ログアウト" : "ログイン";

const greeting = isLoggedIn && "おかえりなさい";

if (isLoggedIn || location.hash === "#preview") {
  console.log("下書きを表示");
}

if (!isLoggedIn) {
  console.log("ログイン画面へ");
}

while (isLoggedIn) {
  break;
}

console.log(label, greeting);
console
$ npx tsc
src/places.ts(3,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
src/places.ts(7,15): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
src/places.ts(9,18): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
src/places.ts(11,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

Besides the if condition (line 3), errors appeared for the ternary condition (line 7), the left side of && (line 9), and the left side of || (line 11). The check for ternary expressions was added in TypeScript 3.9. When I changed each of them to isLoggedIn(), the exit code was 0.

Warning

!isLoggedIn (line 15) and while (isLoggedIn) (line 19) gave no error even without the call. As I confirmed with Node.js in the first section, the function negated with ! is false. So the body of that if never runs. Because there is no error, check for () yourself.

Cases with no error, and checking that a function exists on purpose

A function on an optional property does not cause an error when you check it in an if. Calling that function inside the if does not cause an error either. The TypeScript 3.7 release notes list both as cases where no error is reported.

ts
type Options = {
  onDone?: () => void;
  validate: () => boolean;
};

export function save(options: Options) {
  if (options.onDone) {
    console.log("終わったら知らせる");
  }
  if (options.validate) {
    options.validate();
  }
}

npx tsc on this file exited with code 0. For comparison, I replaced line 11, options.validate();, with console.log("確認してから保存");, and line 10 then became a TS2774 error.

console
$ npx tsc
src/no-error.ts(10,7): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

For a function that may not be passed, such as a callback, the way to match the type to real usage is to make it optional, like onDone?: () => void.

With strictNullChecks turned off, this error also did not appear. When I ran npx tsc --strictNullChecks false on places.ts from the previous section, the exit code was 0. Hiding the error does not mean the missing call is fixed. It only means the mistake goes unnoticed.

Sometimes you want to check at runtime whether a function that the types say always exists is really there. The following code was meant to show a copy button only when navigator.clipboard.writeText exists. In TypeScript's DOM types, this function is always treated as present, so it produced TS2774.

ts
export function setupCopyButton(button: HTMLButtonElement) {
  if (navigator.clipboard.writeText) {
    button.hidden = false;
  }
}
console
$ npx tsc
src/feature.ts(2,7): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

Adding () here would call the function. I only want to check whether it is a function, so I compared it with typeof.

ts

if (typeof navigator.clipboard.writeText === "function") {

With this form, npx tsc exited with code 0. Converting to a boolean with !!, as in if (!!navigator.clipboard.writeText), also exited with code 0. The TypeScript 3.7 release notes describe !! as the way to write a check that intentionally treats the function as a boolean.

A forgotten await gives TS2801

A similar mistake is calling an async function and forgetting await. A Promise is also always truthy, so it gave a different error code, TS2801.

ts
async function canEdit(): Promise<boolean> {
  return false;
}

export async function openEditor() {
  if (canEdit()) {
    console.log("編集画面を開く");
  }
}
console
$ npx tsc --pretty
src/async.ts:6:7 - error TS2801: This condition will always return true since this 'Promise<boolean>' is always defined.

6   if (canEdit()) {
        ~~~~~~~~~

  src/async.ts:6:7 - Did you forget to use 'await'?
    6   if (canEdit()) {
            ~~~~~~~~~


Found 1 error in src/async.ts:6

Without --pretty, only the first line was printed, and Did you forget to use 'await'? did not appear. When I changed line 6 to if (await canEdit()), the exit code was 0. This check was added in TypeScript 4.3.

Fixing it with editor quick fixes

I opened menu.ts in the TypeScript 5.9.3 tsserver and asked for quick fixes. It returned one fix. I printed the returned JSON with a small script I wrote, arranged as follows.

text
typescript: 5.9.3
diagnostics (1):
  8:7-8:19 code=2774 This condition will always return true since this function is always defined. Did you mean to call it instead?
    codeFix [fixMissingCallParentheses] Add missing call parentheses
      edit src/menu.ts 8:12-8:19 -> "isAdmin()"

Add missing call parentheses replaces isAdmin on line 8 with isAdmin(). The four places in places.ts each got the same quick fix. For TS2801 in async.ts, the fix returned was Add 'await', which replaces canEdit() with await canEdit().

I sent the same kind of query to the TypeScript 7.0.2 language server (tsc --lsp --stdio). The diagnostic messages matched tsc, and the TS2801 Did you forget to use 'await'? also came back as related information. However, no quick fixes were returned at all. In the 7.0.2 source, only three kinds of quick fixes are registered, such as adding an import (typescript-go v7.0.2 source (codeactions.go)). In editors that use this language server, you need to add () or await by hand.

​Posted by 86