or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

builtin-types.mdindex.mdproperty-types.mdsymbol-analysis.mdtype-analysis.mdtype-constraints.mdtype-predicates.mdtype-safety.mdtype-specifiers.md
tile.json

tessl/npm-typescript-eslint--type-utils

Type utilities for working with TypeScript + ESLint together

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@typescript-eslint/type-utils@8.42.x

To install, run

npx @tessl/cli install tessl/npm-typescript-eslint--type-utils@8.42.0

index.mddocs/

TypeScript ESLint Type Utils

TypeScript ESLint Type Utils provides essential type utilities for working with TypeScript within ESLint rules. This package offers a comprehensive set of functions for type analysis, type checking, and TypeScript compiler API interactions designed specifically for ESLint rule authors who need to build sophisticated type-aware rules.

Package Information

  • Package Name: @typescript-eslint/type-utils
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @typescript-eslint/type-utils

Core Imports

import {
  isTypeReadonly,
  getTypeName,
  isUnsafeAssignment,
  containsAllTypesByName,
  isTypeAnyType,
  getConstrainedTypeAtLocation
} from "@typescript-eslint/type-utils";

For CommonJS:

const {
  isTypeReadonly,
  getTypeName,
  isUnsafeAssignment,
  containsAllTypesByName,
  isTypeAnyType,
  getConstrainedTypeAtLocation
} = require("@typescript-eslint/type-utils");

Basic Usage

import { RuleContext } from "@typescript-eslint/utils";
import { isTypeReadonly, getTypeName, isTypeAnyType } from "@typescript-eslint/type-utils";

export default {
  create(context: RuleContext<string, unknown[]>) {
    const services = context.parserServices;
    const program = services.program;
    const checker = program.getTypeChecker();

    return {
      VariableDeclaration(node) {
        const tsNode = services.esTreeNodeToTSNodeMap.get(node);
        const type = checker.getTypeAtLocation(tsNode);
        
        // Check if type is readonly
        if (isTypeReadonly(program, type)) {
          console.log("Variable is readonly");
        }
        
        // Get type name
        const typeName = getTypeName(checker, type);
        console.log(`Type name: ${typeName}`);
        
        // Check if type is any
        if (isTypeAnyType(type)) {
          context.report({
            node,
            messageId: "noAnyTypes"
          });
        }
      }
    };
  }
};

Architecture

TypeScript ESLint Type Utils is organized around several key functional areas:

  • Type Predicates: Functions to test type characteristics (isTypeAnyType, isNullableType, etc.)
  • Type Analysis: Tools for analyzing type structure and relationships (getTypeName, containsAllTypesByName)
  • Type Constraints: Functions for working with type constraints and contextual types (getConstrainedTypeAtLocation, getContextualType)
  • Safety Checks: Utilities for detecting unsafe type assignments and readonly violations (isUnsafeAssignment, isTypeReadonly)
  • Builtin Types: Specialized checks for built-in TypeScript types (isPromiseLike, isErrorLike, etc.)
  • Symbol Analysis: Functions for working with TypeScript symbols and declarations (getDeclaration, isSymbolFromDefaultLibrary)

Capabilities

Type Predicates

Essential type checking functions that determine characteristics of TypeScript types. These predicates help identify specific type categories and properties.

function isTypeAnyType(type: ts.Type): boolean;
function isTypeUnknownType(type: ts.Type): boolean;
function isTypeNeverType(type: ts.Type): boolean;
function isNullableType(type: ts.Type): boolean;
function isTypeArrayTypeOrUnionOfArrayTypes(type: ts.Type, checker: ts.TypeChecker): boolean;

Type Predicates

Type Analysis

Functions for analyzing type names, structure, and relationships. Used to understand type composition and extract meaningful information from TypeScript types.

function getTypeName(typeChecker: ts.TypeChecker, type: ts.Type): string;
function containsAllTypesByName(type: ts.Type, allowAny: boolean, allowedNames: Set<string>, matchAnyInstead?: boolean): boolean;
function getTypeFlags(type: ts.Type): ts.TypeFlags;
function isTypeFlagSet(type: ts.Type, flagsToCheck: ts.TypeFlags): boolean;

Type Analysis

Type Constraints and Context

Tools for working with TypeScript's type constraint system and contextual typing, essential for understanding generic types and inference.

function getConstrainedTypeAtLocation(services: ParserServicesWithTypeInformation, node: TSESTree.Node): ts.Type;
function getContextualType(checker: ts.TypeChecker, node: ts.Expression): ts.Type | undefined;

Type Constraints

Type Safety Utilities

Functions for detecting potentially unsafe type operations, particularly around readonly properties and any type assignments.

function isTypeReadonly(program: ts.Program, type: ts.Type, options?: ReadonlynessOptions): boolean;
function isUnsafeAssignment(type: ts.Type, receiver: ts.Type, checker: ts.TypeChecker, senderNode: TSESTree.Node | null): false | { receiver: ts.Type; sender: ts.Type };

interface ReadonlynessOptions {
  readonly allow?: TypeOrValueSpecifier[];
  readonly treatMethodsAsReadonly?: boolean;
}

Type Safety

Builtin Type Checks

Specialized predicates for identifying built-in TypeScript types like Promise, Error, and readonly utility types.

function isPromiseLike(program: ts.Program, type: ts.Type): boolean;
function isPromiseConstructorLike(program: ts.Program, type: ts.Type): boolean;
function isErrorLike(program: ts.Program, type: ts.Type): boolean;
function isReadonlyTypeLike(
  program: ts.Program, 
  type: ts.Type, 
  predicate?: (subType: { aliasSymbol: ts.Symbol; aliasTypeArguments: readonly ts.Type[] } & ts.Type) => boolean
): boolean;

Builtin Types

Symbol and Declaration Analysis

Tools for working with TypeScript symbols, declarations, and their relationships to source files and libraries.

function getDeclaration(services: ParserServicesWithTypeInformation, node: TSESTree.Node): ts.Declaration | null;
function isSymbolFromDefaultLibrary(program: ts.Program, symbol: ts.Symbol | undefined): boolean;
function getSourceFileOfNode(node: ts.Node): ts.SourceFile;

Symbol Analysis

Property Type Utilities

Functions for extracting and analyzing property types from complex type structures.

function getTypeOfPropertyOfName(checker: ts.TypeChecker, type: ts.Type, name: string, escapedName?: ts.__String): ts.Type | undefined;
function getTypeOfPropertyOfType(checker: ts.TypeChecker, type: ts.Type, property: ts.Symbol): ts.Type | undefined;

Property Types

Type Specifiers

Advanced type matching system using specifiers to identify types from specific packages, files, or the TypeScript standard library.

type TypeOrValueSpecifier = string | FileSpecifier | LibSpecifier | PackageSpecifier;

function typeMatchesSpecifier(type: ts.Type, specifier: TypeOrValueSpecifier, program: ts.Program): boolean;
function typeMatchesSomeSpecifier(type: ts.Type, specifiers: TypeOrValueSpecifier[], program: ts.Program): boolean;
function valueMatchesSpecifier(node: TSESTree.Node, specifier: TypeOrValueSpecifier, program: ts.Program, type: ts.Type): boolean;
function valueMatchesSomeSpecifier(node: TSESTree.Node, specifiers: TypeOrValueSpecifier[], program: ts.Program, type: ts.Type): boolean;

Type Specifiers

Common Types

interface FileSpecifier {
  from: 'file';
  name: string | string[];
  path?: string;
}

interface LibSpecifier {
  from: 'lib';
  name: string | string[];
}

interface PackageSpecifier {
  from: 'package';
  name: string | string[];
  package: string;
}

enum AnyType {
  Any,
  PromiseAny,
  AnyArray,
  Safe,
}

// Constants
const readonlynessOptionsDefaults: ReadonlynessOptions;
const readonlynessOptionsSchema: JSONSchema4;
const typeOrValueSpecifiersSchema: JSONSchema4;