CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tsutils

Utility functions for working with TypeScript's Abstract Syntax Tree (AST) and type system

Pending
Overview
Eval results
Files

type-guards.mddocs/

Type System Guards

Type guard functions for TypeScript's type system using ts.TypeFlags. These functions provide type-safe narrowing of ts.Type union types to specific type system types, enabling safe access to type-specific properties and methods in the TypeScript compiler's type system.

Capabilities

Union and Intersection Types

Type guards for composite types that combine multiple types.

/**
 * Check if type is a union type (A | B)
 * @param type - Type to check
 * @returns true if type is ts.UnionType
 */
function isUnionType(type: ts.Type): type is ts.UnionType;

/**
 * Check if type is an intersection type (A & B)
 * @param type - Type to check
 * @returns true if type is ts.IntersectionType
 */
function isIntersectionType(type: ts.Type): type is ts.IntersectionType;

/**
 * Check if type is either union or intersection type
 * @param type - Type to check
 * @returns true if type is ts.UnionOrIntersectionType
 */
function isUnionOrIntersectionType(type: ts.Type): type is ts.UnionOrIntersectionType;

Object and Interface Types

Type guards for object-based types including interfaces and object literals.

/**
 * Check if type is an object type
 * @param type - Type to check
 * @returns true if type is ts.ObjectType
 */
function isObjectType(type: ts.Type): type is ts.ObjectType;

/**
 * Check if type is an interface type
 * @param type - Type to check
 * @returns true if type is ts.InterfaceType
 */
function isInterfaceType(type: ts.Type): type is ts.InterfaceType;

Reference and Generic Types

Type guards for type references and generic type constructs.

/**
 * Check if type is a type reference (reference to named type)
 * @param type - Type to check
 * @returns true if type is ts.TypeReference
 */
function isTypeReference(type: ts.Type): type is ts.TypeReference;

/**
 * Check if type is a generic type
 * @param type - Type to check
 * @returns true if type is ts.GenericType
 */
function isGenericType(type: ts.Type): type is ts.GenericType;

Type Parameters and Variables

Type guards for type parameters and type variables.

/**
 * Check if type is a type parameter (T in generic)
 * @param type - Type to check
 * @returns true if type is ts.TypeParameter
 */
function isTypeParameter(type: ts.Type): type is ts.TypeParameter;

/**
 * Check if type is a type variable (type parameter or indexed access)
 * @param type - Type to check
 * @returns true if type is ts.TypeParameter or ts.IndexedAccessType
 */
function isTypeVariable(type: ts.Type): type is ts.TypeParameter | ts.IndexedAccessType;

Literal and Primitive Types

Type guards for literal values and primitive type checking.

/**
 * Check if type is a literal type (string/number/boolean literal)
 * @param type - Type to check
 * @returns true if type is ts.LiteralType
 */
function isLiteralType(type: ts.Type): type is ts.LiteralType;

/**
 * Check if type is a unique ES symbol type
 * @param type - Type to check
 * @returns true if type is ts.UniqueESSymbolType
 */
function isUniqueESSymbolType(type: ts.Type): type is ts.UniqueESSymbolType;

Advanced Type Constructs

Type guards for advanced TypeScript type system features.

/**
 * Check if type is an indexed access type (T[K])
 * @param type - Type to check
 * @returns true if type is ts.IndexedAccessType
 */
function isIndexedAccessType(type: ts.Type): type is ts.IndexedAccessType;

/**
 * Check if type is a conditional type (T extends U ? X : Y)
 * @param type - Type to check
 * @returns true if type is ts.ConditionalType
 */
function isConditionalType(type: ts.Type): type is ts.ConditionalType;

/**
 * Check if type is a substitution type
 * @param type - Type to check
 * @returns true if type is ts.SubstitutionType
 */
function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType;

/**
 * Check if type is an instantiable type
 * @param type - Type to check
 * @returns true if type is ts.InstantiableType
 */
function isInstantiableType(type: ts.Type): type is ts.InstantiableType;

Enum Types

Type guards for enum type checking.

/**
 * Check if type is an enum type
 * @param type - Type to check
 * @returns true if type is ts.EnumType
 */
function isEnumType(type: ts.Type): type is ts.EnumType;

Tuple Types

Type guards for tuple type checking (TypeScript 3.0+).

/**
 * Check if type is a tuple type (TypeScript 3.0+)
 * @param type - Type to check
 * @returns true if type is ts.TupleType
 */
function isTupleType(type: ts.Type): type is ts.TupleType;

/**
 * Check if type is a tuple type reference (TypeScript 3.0+)
 * @param type - Type to check
 * @returns true if type is ts.TypeReference with ts.TupleType target
 */
function isTupleTypeReference(type: ts.Type): type is ts.TypeReference & {target: ts.TupleType};

Usage Examples:

import * as ts from "typescript";
import { isUnionType, isLiteralType, isObjectType } from "tsutils/typeguard";

function analyzeType(checker: ts.TypeChecker, type: ts.Type) {
  if (isUnionType(type)) {
    // type is now typed as ts.UnionType
    console.log("Union type with", type.types.length, "members");
    type.types.forEach((memberType, index) => {
      console.log(`Member ${index}:`, checker.typeToString(memberType));
    });
  }
  
  if (isLiteralType(type)) {
    // type is now typed as ts.LiteralType
    console.log("Literal type value:", type.value);
  }
  
  if (isObjectType(type)) {
    // type is now typed as ts.ObjectType
    console.log("Object type flags:", type.objectFlags);
    console.log("Properties:", type.symbol?.members?.size || 0);
  }
}

// Example with type checking
function checkTypeProperties(checker: ts.TypeChecker, node: ts.Expression) {
  const type = checker.getTypeAtLocation(node);
  
  if (isUnionType(type)) {
    // Analyze each union member
    const stringTypes = type.types.filter(t => 
      t.flags & ts.TypeFlags.String || isLiteralType(t) && typeof t.value === 'string'
    );
    
    if (stringTypes.length > 0) {
      console.log("Union contains string types");
    }
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-tsutils

docs

ast-traversal.md

code-analysis.md

control-flow.md

index.md

node-typeguards.md

text-processing.md

type-guards.md

type-utilities.md

variable-usage.md

tile.json