Skip to content

⬅️ Back to Table of Contents

📄 src/predicates

📊 Analysis Summary

Metric Count
🔧 Functions 11
📦 Imports 2
📊 Variables & Constants 2

📚 Table of Contents

🛠️ File Location:

📂 packages/type-utils/src/predicates.ts

📦 Imports

Name Source
debug debug
isTypeFlagSet ./typeFlagUtils

Variables & Constants

Name Type Kind Value Exported
Nullable number const ts.TypeFlags.Undefined \| ts.TypeFlags.Null
ObjectFlagsType number const ts.TypeFlags.Any \| Nullable \| ts.TypeFlags.Never \| ts.TypeFlags.Object \| ...

Functions

isNullableType(type: ts.Type): boolean

Checks if the given type is (or accepts) nullable

Raw JSDoc
/**
 * Checks if the given type is (or accepts) nullable
 */

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
Code
export function isNullableType(type: ts.Type): boolean {
  return isTypeFlagSet(
    type,
    ts.TypeFlags.Any |
      ts.TypeFlags.Unknown |
      ts.TypeFlags.Null |
      ts.TypeFlags.Undefined |
      ts.TypeFlags.Void,
  );
}

isTypeArrayTypeOrUnionOfArrayTypes(type: ts.Type, checker: ts.TypeChecker): boolean

Checks if the given type is either an array type, or a union made up solely of array types.

Raw JSDoc
/**
 * Checks if the given type is either an array type,
 * or a union made up solely of array types.
 */

Calls:

  • tsutils.unionConstituents
  • checker.isArrayType
Code
export function isTypeArrayTypeOrUnionOfArrayTypes(
  type: ts.Type,
  checker: ts.TypeChecker,
): boolean {
  for (const t of tsutils.unionConstituents(type)) {
    if (!checker.isArrayType(t)) {
      return false;
    }
  }

  return true;
}

isTypeNeverType(type: ts.Type): boolean

Returns: undefined true if the type is never

Raw JSDoc
/**
 * @returns true if the type is `never`
 */

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
Code
export function isTypeNeverType(type: ts.Type): boolean {
  return isTypeFlagSet(type, ts.TypeFlags.Never);
}

isTypeUnknownType(type: ts.Type): boolean

Returns: undefined true if the type is unknown

Raw JSDoc
/**
 * @returns true if the type is `unknown`
 */

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
Code
export function isTypeUnknownType(type: ts.Type): boolean {
  return isTypeFlagSet(type, ts.TypeFlags.Unknown);
}

isTypeReferenceType(type: ts.Type): type is ts.TypeReference

Parameters:

  • type ts.Type

Returns: type is ts.TypeReference

Code
export function isTypeReferenceType(type: ts.Type): type is ts.TypeReference {
  if ((type.flags & ObjectFlagsType) === 0) {
    return false;
  }
  const objectTypeFlags = (type as ts.ObjectType).objectFlags;
  return (objectTypeFlags & ts.ObjectFlags.Reference) !== 0;
}

isTypeAnyType(type: ts.Type): boolean

Returns: undefined true if the type is any

Raw JSDoc
/**
 * @returns true if the type is `any`
 */

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
  • log
Code
export function isTypeAnyType(type: ts.Type): boolean {
  if (isTypeFlagSet(type, ts.TypeFlags.Any)) {
    if (type.intrinsicName === 'error') {
      log('Found an "error" any type');
    }
    return true;
  }
  return false;
}

isTypeAnyArrayType(type: ts.Type, checker: ts.TypeChecker): boolean

Returns: undefined true if the type is any[]

Raw JSDoc
/**
 * @returns true if the type is `any[]`
 */

Calls:

  • checker.isArrayType
  • isTypeAnyType
  • checker.getTypeArguments
Code
export function isTypeAnyArrayType(
  type: ts.Type,
  checker: ts.TypeChecker,
): boolean {
  return (
    checker.isArrayType(type) &&
    isTypeAnyType(checker.getTypeArguments(type)[0])
  );
}

isTypeUnknownArrayType(type: ts.Type, checker: ts.TypeChecker): boolean

Returns: undefined true if the type is unknown[]

Raw JSDoc
/**
 * @returns true if the type is `unknown[]`
 */

Calls:

  • checker.isArrayType
  • isTypeUnknownType
  • checker.getTypeArguments
Code
export function isTypeUnknownArrayType(
  type: ts.Type,
  checker: ts.TypeChecker,
): boolean {
  return (
    checker.isArrayType(type) &&
    isTypeUnknownType(checker.getTypeArguments(type)[0])
  );
}

typeIsOrHasBaseType(type: ts.Type, parentType: ts.Type): boolean

Returns: undefined Whether a type is an instance of the parent type, including for the parent's base types.

Raw JSDoc
/**
 * @returns Whether a type is an instance of the parent type, including for the parent's base types.
 */

Calls:

  • parentType.getSymbol
  • type.getSymbol
  • type.getBaseTypes
  • typeAndBaseTypes.push
  • baseType.getSymbol
Code
export function typeIsOrHasBaseType(
  type: ts.Type,
  parentType: ts.Type,
): boolean {
  const parentSymbol = parentType.getSymbol();
  if (!type.getSymbol() || !parentSymbol) {
    return false;
  }

  const typeAndBaseTypes = [type];
  const ancestorTypes = type.getBaseTypes();

  if (ancestorTypes) {
    typeAndBaseTypes.push(...ancestorTypes);
  }

  for (const baseType of typeAndBaseTypes) {
    const baseSymbol = baseType.getSymbol();
    if (baseSymbol?.name === parentSymbol.name) {
      return true;
    }
  }

  return false;
}

isTypeBigIntLiteralType(type: ts.Type): type is ts.BigIntLiteralType

Parameters:

  • type ts.Type

Returns: type is ts.BigIntLiteralType

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
Code
export function isTypeBigIntLiteralType(
  type: ts.Type,
): type is ts.BigIntLiteralType {
  return isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral);
}

isTypeTemplateLiteralType(type: ts.Type): type is ts.TemplateLiteralType

Parameters:

  • type ts.Type

Returns: type is ts.TemplateLiteralType

Calls:

  • isTypeFlagSet (from ./typeFlagUtils)
Code
export function isTypeTemplateLiteralType(
  type: ts.Type,
): type is ts.TemplateLiteralType {
  return isTypeFlagSet(type, ts.TypeFlags.TemplateLiteral);
}

Generated by Syntax Scribe