Skip to content

⬅️ Back to Table of Contents

📄 baseTypeUtils

📊 Analysis Summary

Metric Count
🔧 Functions 5
📦 Imports 5

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/util/baseTypeUtils.ts

📦 Imports

Name Source
ParserServicesWithTypeInformation @typescript-eslint/utils
InterfaceType typescript
Type typescript
isObjectFlagSet ts-api-utils
isObjectType ts-api-utils

Functions

hasBaseTypes(type: Type): type is InterfaceType

Parameters:

  • type Type

Returns: type is InterfaceType

Calls:

  • isObjectType (from ts-api-utils)
  • isObjectFlagSet (from ts-api-utils)
Code
export function hasBaseTypes(type: Type): type is InterfaceType {
  return (
    isObjectType(type) &&
    isObjectFlagSet(type, ts.ObjectFlags.Interface | ts.ObjectFlags.Class)
  );
}

isNumberLike(type: Type): boolean

Parameters:

  • type Type

Returns: boolean

Calls:

  • tsutils .unionConstituents(type) .every
  • tsutils .intersectionConstituents(unionPart) .some
  • tsutils.isTypeFlagSet
Code
export function isNumberLike(type: Type): boolean {
  return tsutils
    .unionConstituents(type)
    .every(unionPart =>
      tsutils
        .intersectionConstituents(unionPart)
        .some(intersectionPart =>
          tsutils.isTypeFlagSet(intersectionPart, ts.TypeFlags.NumberLike),
        ),
    );
}

isStringLike(type: Type): boolean

Parameters:

  • type Type

Returns: boolean

Calls:

  • tsutils .unionConstituents(type) .every
  • tsutils .intersectionConstituents(unionPart) .some
  • tsutils.isTypeFlagSet
Code
export function isStringLike(type: Type): boolean {
  return tsutils
    .unionConstituents(type)
    .every(unionPart =>
      tsutils
        .intersectionConstituents(unionPart)
        .some(intersectionPart =>
          tsutils.isTypeFlagSet(intersectionPart, ts.TypeFlags.StringLike),
        ),
    );
}

matchesTypeOrBaseType(services: ParserServicesWithTypeInformation, matcher: (type: Type) => boolean, type: Type, seen: Set<Type>): boolean

Recursively checks if a type or any of its base types matches the provided matcher function.

Parameters:

  • services any: Parser services with type information
  • matcher any: Function to test if a type matches the desired criteria
  • type any: The type to check
  • seen any: Set of already visited types to prevent infinite recursion

Returns: undefined true if the type or any of its base types match the matcher

Raw JSDoc
/**
 * Recursively checks if a type or any of its base types matches the provided
 * matcher function.
 * @param services Parser services with type information
 * @param matcher Function to test if a type matches the desired criteria
 * @param type The type to check
 * @param seen Set of already visited types to prevent infinite recursion
 * @returns `true` if the type or any of its base types match the matcher
 */

Calls:

  • seen.has
  • seen.add
  • matcher
  • services.program.getTypeChecker
  • getBaseTypesForType(checker, type).some
  • matchesTypeOrBaseType
Code
export function matchesTypeOrBaseType(
  services: ParserServicesWithTypeInformation,
  matcher: (type: Type) => boolean,
  type: Type,
  seen = new Set<Type>(),
): boolean {
  if (seen.has(type)) {
    return false;
  }

  seen.add(type);

  if (matcher(type)) {
    return true;
  }

  const checker = services.program.getTypeChecker();

  return getBaseTypesForType(checker, type).some(base =>
    matchesTypeOrBaseType(services, matcher, base, seen),
  );
}

getBaseTypesForType(checker: ts.TypeChecker, type: ts.Type): readonly ts.Type[]

Parameters:

  • checker ts.TypeChecker
  • type ts.Type

Returns: readonly ts.Type[]

Calls:

  • tsutils.isObjectType
  • tsutils.isTypeReference
  • tsutils.isObjectFlagSet
  • checker.getBaseTypes
Code
function getBaseTypesForType(
  checker: ts.TypeChecker,
  type: ts.Type,
): readonly ts.Type[] {
  if (!tsutils.isObjectType(type)) {
    return [];
  }

  const interfaceTarget = tsutils.isTypeReference(type) ? type.target : type;

  const interfaceType =
    tsutils.isObjectFlagSet(
      interfaceTarget,
      ts.ObjectFlags.Interface | ts.ObjectFlags.Class,
    ) && (interfaceTarget as ts.InterfaceType);

  if (!interfaceType) {
    return [];
  }

  return checker.getBaseTypes(interfaceType);
}

Generated by Syntax Scribe