Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Advanced TypeScript expert covering type-level programming, branded types, monorepos, and migration strategies.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/ts_diagnostic.py
1#!/usr/bin/env python32"""3TypeScript Project Diagnostic Script4Analyzes TypeScript projects for configuration, performance, and common issues.5"""67import subprocess8import sys9import os10import json11from pathlib import Path1213def run_cmd(cmd: str) -> str:14"""Run shell command and return output."""15try:16result = subprocess.run(cmd, shell=True, capture_output=True, text=True)17return result.stdout + result.stderr18except Exception as e:19return str(e)2021def check_versions():22"""Check TypeScript and Node versions."""23print("\nđŚ Versions:")24print("-" * 40)2526ts_version = run_cmd("npx tsc --version 2>/dev/null").strip()27node_version = run_cmd("node -v 2>/dev/null").strip()2829print(f" TypeScript: {ts_version or 'Not found'}")30print(f" Node.js: {node_version or 'Not found'}")3132def check_tsconfig():33"""Analyze tsconfig.json settings."""34print("\nâď¸ TSConfig Analysis:")35print("-" * 40)3637tsconfig_path = Path("tsconfig.json")38if not tsconfig_path.exists():39print("â ď¸ tsconfig.json not found")40return4142try:43with open(tsconfig_path) as f:44config = json.load(f)4546compiler_opts = config.get("compilerOptions", {})4748# Check strict mode49if compiler_opts.get("strict"):50print("â Strict mode enabled")51else:52print("â ď¸ Strict mode NOT enabled")5354# Check important flags55flags = {56"noUncheckedIndexedAccess": "Unchecked index access protection",57"noImplicitOverride": "Implicit override protection",58"skipLibCheck": "Skip lib check (performance)",59"incremental": "Incremental compilation"60}6162for flag, desc in flags.items():63status = "â " if compiler_opts.get(flag) else "âŞ"64print(f" {status} {desc}: {compiler_opts.get(flag, 'not set')}")6566# Check module settings67print(f"\n Module: {compiler_opts.get('module', 'not set')}")68print(f" Module Resolution: {compiler_opts.get('moduleResolution', 'not set')}")69print(f" Target: {compiler_opts.get('target', 'not set')}")7071except json.JSONDecodeError:72print("â Invalid JSON in tsconfig.json")7374def check_tooling():75"""Detect TypeScript tooling ecosystem."""76print("\nđ ď¸ Tooling Detection:")77print("-" * 40)7879pkg_path = Path("package.json")80if not pkg_path.exists():81print("â ď¸ package.json not found")82return8384try:85with open(pkg_path) as f:86pkg = json.load(f)8788all_deps = {**pkg.get("dependencies", {}), **pkg.get("devDependencies", {})}8990tools = {91"biome": "Biome (linter/formatter)",92"eslint": "ESLint",93"prettier": "Prettier",94"vitest": "Vitest (testing)",95"jest": "Jest (testing)",96"turborepo": "Turborepo (monorepo)",97"turbo": "Turbo (monorepo)",98"nx": "Nx (monorepo)",99"lerna": "Lerna (monorepo)"100}101102for tool, desc in tools.items():103for dep in all_deps:104if tool in dep.lower():105print(f" â {desc}")106break107108except json.JSONDecodeError:109print("â Invalid JSON in package.json")110111def check_monorepo():112"""Check for monorepo configuration."""113print("\nđŚ Monorepo Check:")114print("-" * 40)115116indicators = [117("pnpm-workspace.yaml", "PNPM Workspace"),118("lerna.json", "Lerna"),119("nx.json", "Nx"),120("turbo.json", "Turborepo")121]122123found = False124for file, name in indicators:125if Path(file).exists():126print(f" â {name} detected")127found = True128129if not found:130print(" ⪠No monorepo configuration detected")131132def check_type_errors():133"""Run quick type check."""134print("\nđ Type Check:")135print("-" * 40)136137result = run_cmd("npx tsc --noEmit 2>&1 | head -20")138if "error TS" in result:139errors = result.count("error TS")140print(f" â {errors}+ type errors found")141print(result[:500])142else:143print(" â No type errors")144145def check_any_usage():146"""Check for any type usage."""147print("\nâ ď¸ 'any' Type Usage:")148print("-" * 40)149150result = run_cmd("grep -r ': any' --include='*.ts' --include='*.tsx' src/ 2>/dev/null | wc -l")151count = result.strip()152if count and count != "0":153print(f" â ď¸ Found {count} occurrences of ': any'")154sample = run_cmd("grep -rn ': any' --include='*.ts' --include='*.tsx' src/ 2>/dev/null | head -5")155if sample:156print(sample)157else:158print(" â No explicit 'any' types found")159160def check_type_assertions():161"""Check for type assertions."""162print("\nâ ď¸ Type Assertions (as):")163print("-" * 40)164165result = run_cmd("grep -r ' as ' --include='*.ts' --include='*.tsx' src/ 2>/dev/null | grep -v 'import' | wc -l")166count = result.strip()167if count and count != "0":168print(f" â ď¸ Found {count} type assertions")169else:170print(" â No type assertions found")171172def check_performance():173"""Check type checking performance."""174print("\nâąď¸ Type Check Performance:")175print("-" * 40)176177result = run_cmd("npx tsc --extendedDiagnostics --noEmit 2>&1 | grep -E 'Check time|Files:|Lines:|Nodes:'")178if result.strip():179for line in result.strip().split('\n'):180print(f" {line}")181else:182print(" â ď¸ Could not measure performance")183184def main():185print("=" * 50)186print("đ TypeScript Project Diagnostic Report")187print("=" * 50)188189check_versions()190check_tsconfig()191check_tooling()192check_monorepo()193check_any_usage()194check_type_assertions()195check_type_errors()196check_performance()197198print("\n" + "=" * 50)199print("â Diagnostic Complete")200print("=" * 50)201202if __name__ == "__main__":203main()204