CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-ts-api-utils

tessl install tessl/npm-ts-api-utils@2.0.0

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

Agent Success

Agent success rate when using this tile

79%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.98x

Baseline

Agent success rate without this tile

40%

task.mdevals/scenario-1/

Type Analysis Reporter

Build a type analysis tool that analyzes TypeScript types and generates detailed reports about their characteristics.

Requirements

The tool should analyze TypeScript types and detect the following properties:

Constituent Types Analysis

When given union or intersection types, the tool should be able to extract and report their constituent types. For unions, list all the types that are combined with |. For intersections, list all the types that are combined with &.

Falsy Type Detection

Identify types that are definitively falsy (e.g., false, null, undefined, 0, empty string literal types).

Readonly Property Analysis

For object types with properties, determine whether specific properties are readonly. The analysis should handle both explicit readonly modifiers and readonly mapped types.

Thenable Type Detection

Detect whether a type represents a thenable/awaitable value (e.g., Promise-like types with a then method).

Literal Type Detection

Identify whether a type is a literal type (string literal, number literal, boolean literal, etc.).

Implementation

Your implementation should provide a function that takes a TypeScript type and returns an analysis report containing:

  • List of constituent types (if union or intersection)
  • Whether the type is falsy
  • For object types: which properties are readonly
  • Whether the type is thenable
  • Whether the type is a literal type

@generates

API

import * as ts from 'typescript';

export interface TypeAnalysisReport {
  constituents?: {
    unions?: ts.Type[];
    intersections?: ts.Type[];
  };
  isFalsy: boolean;
  isThenable: boolean;
  isLiteral: boolean;
  readonlyProperties?: string[];
}

export function analyzeType(
  type: ts.Type,
  typeChecker: ts.TypeChecker,
  node?: ts.Node
): TypeAnalysisReport;

Test Cases

Union type analysis

  • Given a union type string | number | boolean, the constituents should list all three types @test

Intersection type analysis

  • Given an intersection type { a: string } & { b: number }, the constituents should list both object types @test

Falsy type detection

  • The type false should be identified as falsy @test
  • The type null should be identified as falsy @test

Literal type detection

  • The type "hello" (string literal) should be identified as a literal type @test
  • The type 42 (number literal) should be identified as a literal type @test

Dependencies { .dependencies }

typescript { .dependency }

Provides TypeScript compiler API for type checking and analysis.

ts-api-utils { .dependency }

Provides utility functions for working with TypeScript's API, including type analysis capabilities.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ts-api-utils@2.0.x
tile.json