Skip to content

⬅️ Back to Table of Contents

📄 getConstraintInfo

📊 Analysis Summary

Metric Count
🔧 Functions 1
📐 Interfaces 3
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

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

Functions

getConstraintInfo(checker: ts.TypeChecker, type: ts.Type): ConstraintTypeInfo

Returns whether the type is a generic and what its constraint is.

If the type is not a generic, isTypeParameter will be false, and constraintType will be the same as the input type.

If the type is a generic, and it is constrained, isTypeParameter will be true, and constraintType will be the constraint type.

If the type is a generic, but it is not constrained, constraintType will be undefined (rather than an unknown type), due to https://github.com/microsoft/TypeScript/issues/60475

Successor to due to https://github.com/typescript-eslint/typescript-eslint/issues/10438

This is considered internal since it is unstable for now and may have breaking changes at any time. Use at your own risk.

Tags: @internal

Raw JSDoc
/**
 * Returns whether the type is a generic and what its constraint is.
 *
 * If the type is not a generic, `isTypeParameter` will be `false`, and
 * `constraintType` will be the same as the input type.
 *
 * If the type is a generic, and it is constrained, `isTypeParameter` will be
 * `true`, and `constraintType` will be the constraint type.
 *
 * If the type is a generic, but it is not constrained, `constraintType` will be
 * `undefined` (rather than an `unknown` type), due to https://github.com/microsoft/TypeScript/issues/60475
 *
 * Successor to {@link getConstrainedTypeAtLocation} due to https://github.com/typescript-eslint/typescript-eslint/issues/10438
 *
 * This is considered internal since it is unstable for now and may have breaking changes at any time.
 * Use at your own risk.
 *
 * @internal
 *
 */

Calls:

  • tsutils.isTypeParameter
  • checker.getBaseConstraintOfType
Code
export function getConstraintInfo(
  checker: ts.TypeChecker,
  type: ts.Type,
): ConstraintTypeInfo {
  if (tsutils.isTypeParameter(type)) {
    const constraintType = checker.getBaseConstraintOfType(type);
    return {
      constraintType,
      isTypeParameter: true,
    };
  }
  return {
    constraintType: type,
    isTypeParameter: false,
  };
}

Interfaces

ConstraintTypeInfoUnconstrained

Interface Code
export interface ConstraintTypeInfoUnconstrained {
  constraintType: undefined;
  isTypeParameter: true;
}

Properties

Name Type Optional Description
constraintType undefined not shown
isTypeParameter true not shown

ConstraintTypeInfoConstrained

Interface Code
export interface ConstraintTypeInfoConstrained {
  constraintType: ts.Type;
  isTypeParameter: true;
}

Properties

Name Type Optional Description
constraintType ts.Type not shown
isTypeParameter true not shown

ConstraintTypeInfoNonGeneric

Interface Code
export interface ConstraintTypeInfoNonGeneric {
  constraintType: ts.Type;
  isTypeParameter: false;
}

Properties

Name Type Optional Description
constraintType ts.Type not shown
isTypeParameter false not shown

Type Aliases

ConstraintTypeInfo

type ConstraintTypeInfo = | ConstraintTypeInfoConstrained
  | ConstraintTypeInfoNonGeneric
  | ConstraintTypeInfoUnconstrained;

Generated by Syntax Scribe