kanji-inspector
Version:
A TypeScript library that provides Unihan data as type-safe constants for Kanji and CJK ideographs.
153 lines (152 loc) • 4.57 kB
TypeScript
type PropertyType = 'string' | 'number' | 'boolean' | 'any' | 'string[]' | 'number[]' | 'boolean[]' | 'any[]' | string;
type TypeGenOptions = {
rootTypeName?: string;
indentSize?: number;
arrayElementTypeName?: string;
};
type DeepTypeProperty = {
type: PropertyType;
optional?: boolean;
children?: Record<string, DeepTypeProperty>;
};
declare class TypeGen {
private readonly rootTypeName;
private readonly indentSize;
private readonly indent;
private readonly arrayElementTypeName;
private generatedTypes;
constructor(options?: TypeGenOptions);
/**
* プロパティ名から型名を生成します
*/
private getTypeNameFromProperty;
/**
* 配列要素の型名を生成します
*/
private getArrayElementTypeName;
/**
* 値の型を判定します
*/
private getPropertyType;
/**
* オブジェクトからプロパティの型情報を抽出します
*/
private extractProperties;
/**
* 配列から共通のプロパティを抽出し、オプショナルなプロパティを特定します
*/
private extractCommonProperties;
/**
* 型定義を文字列として生成します
*/
private generateTypeDefinition;
/**
* データからTypeScriptの型定義を生成します
*/
generate(data: Record<string, any> | Record<string, any>[]): string;
/**
* 新しいオプションでTypeGenインスタンスを生成します
*/
withOptions(options: TypeGenOptions): TypeGen;
/**
* 再帰的に型情報を抽出(配列にも対応)
*/
private extractDeepProperties;
/**
* 再帰的な型アサーション(配列にも対応)
*/
assertDeep(data: any, expected: Record<string, DeepTypeProperty>, path?: string): void;
}
/**
* データからTypeScriptの型定義を生成するためのユーティリティ
*
* このモジュールは、データの構造から適切なTypeScript型定義を生成します。
* 入力データの形式に応じて、単一オブジェクト用の型定義または配列用の型定義を生成します。
*
* 特徴:
* - 単一オブジェクトから型定義を生成
* - 配列から共通プロパティを抽出して型定義を生成
* - カスタマイズ可能な型名とインデント
* - プロパティ名に基づく型名の自動生成
*
* 型の判定ルール:
* - 配列の場合:
* - 空配列 → any[]
* - 要素がすべて同じ型 → その型の配列(例: number[])
* - 要素の型が混在 → any[]
* - オブジェクトの場合:
* - ネストしたオブジェクト → プロパティ名から生成された型名(例: user → UserType)
* - null/undefined → any型
*
* @example
* // 単一オブジェクトの場合
* const data = {
* id: 1,
* name: "John Doe",
* email: "john@example.com"
* };
* // 生成される型:
* type RootType = {
* id: number;
* name: string;
* email: string;
* }
*
* @example
* // 配列の場合
* const data = [
* {
* id: 1,
* name: "John Doe",
* email: "john@example.com"
* },
* {
* id: 2,
* name: "Jane Smith",
* phone: "123-456-7890"
* }
* ];
* // 生成される型:
* type ContentType = {
* id: number;
* name: string;
* email?: string;
* phone?: string;
* }
* type RootType = ContentType[];
*
* @example
* // 配列の型混在とネストしたオブジェクト
* const data = {
* numbers: [1, 2, 3], // number[]
* strings: ["a", "b", "c"], // string[]
* mixed: [1, "a", true], // any[]
* empty: [], // any[]
* user: { // UserType
* profile: { // ProfileType
* name: "John"
* }
* }
* };
* // 生成される型:
* type ProfileType = {
* name: string;
* }
* type UserType = {
* profile: ProfileType;
* }
* type RootType = {
* numbers: number[];
* strings: string[];
* mixed: any[];
* empty: any[];
* user: UserType;
* }
*
* @note
* - 配列の場合は、すべてのプロパティをオプショナル(?)として扱います
* - 単一オブジェクトの場合は、すべてのプロパティを必須として扱います
* - ネストしたオブジェクトの型名は、プロパティ名から自動生成されます(user → UserType)
* - 配列要素の型は ContentType として生成されます
*/
export { TypeGen };