CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-api-utils

Utility functions for working with TypeScript's API, providing comprehensive tools for analyzing and manipulating TypeScript AST nodes, types, and compiler APIs.

79

1.97x
Overview
Eval results
Files

task.mdevals/scenario-9/

TypeScript Type Analyzer

Build a TypeScript type analysis utility that categorizes and reports on TypeScript types within a given AST node. The analyzer should identify different type categories and provide statistics about type complexity.

Requirements

Create a TypeScript module that exports a function analyzeTypes which takes a TypeScript AST node and a type checker, then returns an analysis report.

The analyzer must classify types into the following categories:

  1. Union types: Types that use the union operator (A | B)
  2. Intersection types: Types that use the intersection operator (A & B)
  3. Literal types: Specific value types like string literals, number literals, boolean literals, or bigint literals
  4. Object types: Complex object structures including tuples and type references
  5. Intrinsic types: Built-in primitive types like string, number, boolean, etc.

For each type encountered, the analyzer should:

  • Determine which category it belongs to
  • For union and intersection types, count how many constituent parts they have
  • For literal types, identify the specific kind (string, number, boolean, or bigint)

Implementation

@generates

API

export interface TypeAnalysis {
  unionTypes: number;
  intersectionTypes: number;
  literalTypes: {
    string: number;
    number: number;
    boolean: number;
    bigint: number;
  };
  objectTypes: number;
  intrinsicTypes: number;
  totalTypes: number;
}

export function analyzeTypes(
  node: ts.Node,
  typeChecker: ts.TypeChecker
): TypeAnalysis;

Test Cases

  • Given a type string | number, the analyzer reports 1 union type with 2 intrinsic types @test
  • Given a type { name: string } & { age: number }, the analyzer reports 1 intersection type with 2 object types @test
  • Given a type "hello" | "world" | 42, the analyzer reports 1 union type with 2 string literals and 1 number literal @test
  • Given a type [string, number], the analyzer reports 1 object type (tuple) @test

Dependencies { .dependencies }

ts-api-utils { .dependency }

Provides TypeScript type analysis utilities.

typescript { .dependency }

Provides the TypeScript compiler API.

Install with Tessl CLI

npx tessl i tessl/npm-ts-api-utils

tile.json