kintoneの型情報を軽くブラウザから取得してくる

対象

  • kintoneでのプラグイン開発でTypeScriptを使う人で軽く型情報を知りたい人

方法

  • Kintoneアプリ上のコンソール画面でスクリプトを走らせる
const generateKintoneTypes = (obj, currentDepth = 0, visited = new Set()) => {
  const indent = "  ".repeat(currentDepth);
  const nextIndent = "  ".repeat(currentDepth + 1);

  if (currentDepth > 10) return "any;";
  if (obj === null) return "any;";

  const jsType = typeof obj;

  if (jsType === "function") {
    const argCount = obj.length || 0;
    const args = Array.from(
      { length: argCount },
      (_, i) => `arg${i}: any`,
    ).join(", ");
    return `(${args}) => any;`;
  }

  if (jsType !== "object") return "any;";
  if (Array.isArray(obj)) return "any[];";
  if (visited.has(obj)) return "any;";

  visited.add(obj);

  const props = [];
  const keys = Reflect.ownKeys(obj);

  for (const key of keys) {
    let propType = "any;";
    let keyStr = typeof key === "symbol" ? `[${key.toString()}]` : String(key);

    if (!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(keyStr) && !/^\d+$/.test(keyStr)) {
      keyStr = `"${keyStr.replace(/"/g, '\\"')}"`;
    }

    try {
      propType = generateKintoneTypes(obj[key], currentDepth + 1, visited);
    } catch (e) {
      console.warn(`Could not access property '${String(key)}': ${e.message}`);
    }
    props.push(`${nextIndent}${keyStr}: ${propType}`);
  }

  visited.delete(obj);
  if (props.length === 0) return "{};";
  return `{\n${props.join("\n")}\n${indent}};`;
};

(() => {
  if (typeof kintone === "undefined") {
    console.error("Error: 'kintone' object not found");
    return;
  }

  console.log("--- Generating Kintone Structure ---");
  const typeDefinition = generateKintoneTypes(kintone);
  const finalOutput = `declare namespace kintone ${typeDefinition}`;
  console.log(finalOutput);
})();