🍃このブログは移転しました。
3秒後、自動的に移動します・・・。

JSDoc TSで、ユニオン型を改行して書く場合は、括弧が必要

この画像のとおり。


こういうこと

/**
 * @typedef {{ type: "x", data: number }} X
 * @typedef {{ type: "y", data: string }} Y
 *
 * @typedef {X | Y} XY1
 * @typedef {
 *   | X
 *   | Y
 * } XY2
 * @typedef {(
 *   | X
 *   | Y
 * )} XY3
 */

/** @type {XY1} */
const xy1 = { type: "x", data: "-" }; // 型エラー
/** @type {XY2} */
const xy2 = { type: "x", data: "-" }; // 型エラーにならない
/** @type {XY3} */
const xy3 = { type: "x", data: "-" }; // 型エラー

あろうことか`any`になっちゃうので注意・・・。