Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. TypeScript Dictionary
  3. Comments (// and /* */)

Comments (// and /* */)

TypeScript has three types of comments: single-line comments (//), block comments (/* */), and JSDoc comments (/** */), which are widely used for documentation. Although TypeScript has a type system, JSDoc comments remain useful for describing intent, providing usage examples, and generating documentation. Editors such as VS Code read JSDoc and display the information on hover.

Syntax

// Single-line comment — everything from // to the end of the line is a comment.
const x = 10; // Inline comment — can also be written after code.

/*
  Block comment — everything between /* and */ is a comment.
  Use this when you want to write a comment that spans multiple lines.
*/

/**
 * JSDoc comment — a block comment that starts with /**.
 * Write this immediately before a function or class.
 * @param paramName - Description of the parameter
 * @returns Description of the return value
 */

Comment Types

TypeSyntaxDescription
Single-line comment// textEverything from // to the end of the line becomes a comment. Also used to add a note to the right of a line of code.
Block comment/* text */Used when writing a comment that spans multiple lines. Cannot be nested.
JSDoc comment/** text */A special block comment that starts with /**. Documents information in a format that editors and documentation tools can parse. In TypeScript, type information lives in the code itself, so JSDoc is mainly used for descriptions and usage examples.

Common JSDoc Tags

JSDoc comments use tags starting with @ to document information. In TypeScript, type annotations in @param (e.g., {string}) can be omitted because the types are already in the code, but the descriptive text is still useful. Editors such as VS Code read JSDoc tags and display them on hover.

TagDescription
@param name - descriptionDocuments a function parameter. In TypeScript, the type annotation can be omitted since it is already in the code.
@returns descriptionDocuments the return value of a function.
@throws descriptionDocuments errors that may be thrown. Because TypeScript does not enforce exception types, making this explicit helps callers.
@template TDocuments a generic type parameter.
@deprecated descriptionMarks the item as deprecated. Most editors display the name with a strikethrough.
@see referencePoints to related functions or documentation.
@exampleProvides a code block showing usage.
@internalUsed as a marker for tools such as TypeDoc to exclude the item from generated documentation.

When to Write Comments and When Not To

DecisionSituationReason
Write a commentThe reason why something was implemented a certain wayDesign decisions and trade-offs that cannot be understood from the code alone help your future self and other developers.
Write a commentComplex algorithms or formulasAdd a brief description of what a piece of logic does when its behavior is not immediately obvious at a glance.
Write a commentPublic API functions and classesJSDoc comments on public APIs appear in editor hover tooltips and can be used by tools like TypeDoc to generate HTML documentation.
Write a commentNon-obvious type casts or type assertionsExplaining why a type assertion (as Type) is used helps reviewers understand when it is safe to remove.
No comment neededLogic that is obvious from reading the codeSelf-evident explanations become noise. Clear variable, function, and type names remove the need for such comments.
No comment neededChange history or deleted codeBecause version control (git) is available, there is no need to keep old code or change dates in comments.

Sample Code

comment_basic.ts
// comment_basic.ts — Basic usage of TypeScript comment syntax.

interface Item {
    name: string;
    price: number;
}

// Define the item list
const items: Item[] = [
    { name: "widget", price: 1200 },
    { name: "gadget", price: 3500 },
    { name: "device", price: 800  },
];

/*
  Calculate the total and print the result.
  Use reduce to sum across the entire array.
*/
const total = items.reduce((sum, item) => sum + item.price, 0);

for (const item of items) {
    console.log(`${item.name.padEnd(10)} ${String(item.price).padStart(5)} yen`);
}

console.log("----------");
console.log(`${"Total".padEnd(10)} ${String(total).padStart(5)} yen`);

Running the above produces the following output:

$ npx ts-node comment_basic.ts
widget      1200 yen
gadget      3500 yen
device       800 yen
----------
Total       5500 yen
comment_jsdoc.ts
// comment_jsdoc.ts — Using JSDoc comments in TypeScript.

/**
 * Computes the average of a numeric array and returns it.
 *
 * Returns `null` if the array is empty (does not throw).
 *
 * @param values - An array of numbers to average
 * @returns The average (number), or null if the array is empty.
 * @example
 * ```ts
 * calculateAverage([85, 92, 78, 95, 60]); // 82
 * calculateAverage([]); // null
 * ```
 */
function calculateAverage(values: number[]): number | null {
    if (values.length === 0) {
        // Empty array cannot be averaged — return null
        return null;
    }

    const sum = values.reduce((acc, v) => acc + v, 0);
    return sum / values.length;
}

/**
 * @deprecated Use `calculateAverage` instead.
 */
function avg(values: number[]): number | null {
    return calculateAverage(values);
}

const scores = [85, 92, 78, 95, 60];
console.log("Average:", calculateAverage(scores));
console.log("Empty:  ", calculateAverage([]));

Running the above produces the following output:

$ npx ts-node comment_jsdoc.ts
Average: 82
Empty:   null

Overview

TypeScript has three comment types: single-line comments (//), block comments (/* */), and JSDoc comments (block comments starting with /**). Because TypeScript has a type system, parameter and return types can already be read from the code itself. Even so, JSDoc comments remain common practice for explaining intent, providing usage examples, and communicating constraints that types alone cannot express.

With JSDoc comments in place, editors such as VS Code display the documentation on hover. Tools like TypeDoc can also generate HTML documentation from JSDoc. The @deprecated tag causes most editors to display the name with a strikethrough, which is an effective way to signal to callers that an API is slated for removal.

If you find any errors or copyright issues, please .