Skip to main content

@typescript-eslint/type-utils

npm: @typescript-eslint/type-utils v8.32.0

Type utilities for working with TypeScript types ✨

This package contains public utilities for working with TypeScript types. Rules declared in @typescript-eslint/eslint-plugin use these utility functions.

The utilities in this package are both:

  • More generally ESLint-focused than the broad TypeScript utilities in ts-api-utils
  • Separated from @typescript-eslint/utils so that that package does not require a dependency on typescript
tip

See Custom Rules for documentation on creating your own custom ESLint rules for TypeScript code.


The following documentation is auto-generated from source code.

Functions

containsAllTypesByName()

function containsAllTypesByName(
type,
allowAny,
allowedNames,
matchAnyInstead): boolean;

Defined in: containsAllTypesByName.ts:13

Parameters

ParameterTypeDefault valueDescription
typeTypeundefinedType being checked by name.
allowAnybooleanundefinedWhether to consider any and unknown to match.
allowedNamesSet<string>undefinedSymbol names checking on the type.
matchAnyInsteadbooleanfalseWhether to instead just check if any parts match, rather than all parts.

Returns

boolean

Whether the type is, extends, or contains the allowed names (or all matches the allowed names, if mustMatchAll is true).


discriminateAnyType()

function discriminateAnyType(
type,
checker,
program,
tsNode): AnyType;

Defined in: discriminateAnyType.ts:17

Parameters

ParameterType
typeType
checkerTypeChecker
programProgram
tsNodeNode

Returns

AnyType

AnyType.Any if the type is any, AnyType.AnyArray if the type is any[] or readonly any[], AnyType.PromiseAny if the type is Promise<any>, otherwise it returns AnyType.Safe.


getConstrainedTypeAtLocation()

function getConstrainedTypeAtLocation(services, node): Type;

Defined in: getConstrainedTypeAtLocation.ts:16

Resolves the given node's type. Will return the type's generic constraint, if it has one.

Warning - if the type is generic and does not have a constraint, the type will be returned as-is, rather than returning an unknown type. This can be checked for by checking for the type flag ts.TypeFlags.TypeParameter.

Parameters

ParameterType
servicesParserServicesWithTypeInformation
nodeNode

Returns

Type

See

https://github.com/typescript-eslint/typescript-eslint/issues/10438


getContextualType()

function getContextualType(checker, node): undefined | Type;

Defined in: getContextualType.ts:8

Returns the contextual type of a given node. Contextual type is the type of the target the node is going into. i.e. the type of a called function's parameter, or the defined type of a variable declaration

Parameters

ParameterType
checkerTypeChecker
nodeExpression

Returns

undefined | Type


getDeclaration()

function getDeclaration(services, node): null | Declaration;

Defined in: getDeclaration.ts:10

Gets the declaration for the given variable

Parameters

ParameterType
servicesParserServicesWithTypeInformation
nodeNode

Returns

null | Declaration


getSourceFileOfNode()

function getSourceFileOfNode(node): SourceFile;

Defined in: getSourceFileOfNode.ts:6

Gets the source file for a given node

Parameters

ParameterType
nodeNode

Returns

SourceFile


getTypeFlags()

function getTypeFlags(type): TypeFlags;

Defined in: typeFlagUtils.ts:9

Gets all of the type flags in a type, iterating through unions automatically.

Parameters

ParameterType
typeType

Returns

TypeFlags


getTypeName()

function getTypeName(typeChecker, type): string;

Defined in: getTypeName.ts:8

Get the type name of a given type.

Parameters

ParameterTypeDescription
typeCheckerTypeCheckerThe context sensitive TypeScript TypeChecker.
typeTypeThe type to get the name of.

Returns

string


getTypeOfPropertyOfName()

function getTypeOfPropertyOfName(
checker,
type,
name,
escapedName?): undefined | Type;

Defined in: propertyTypes.ts:3

Parameters

ParameterType
checkerTypeChecker
typeType
namestring
escapedName?__String

Returns

undefined | Type


getTypeOfPropertyOfType()

function getTypeOfPropertyOfType(
checker,
type,
property): undefined | Type;

Defined in: propertyTypes.ts:25

Parameters

ParameterType
checkerTypeChecker
typeType
propertySymbol

Returns

undefined | Type


isBuiltinSymbolLike()

function isBuiltinSymbolLike(
program,
type,
symbolName): boolean;

Defined in: builtinSymbolLikes.ts:121

Parameters

ParameterType
programProgram
typeType
symbolNamestring | string[]

Returns

boolean


isBuiltinSymbolLikeRecurser()

function isBuiltinSymbolLikeRecurser(
program,
type,
predicate): boolean;

Defined in: builtinSymbolLikes.ts:147

Parameters

ParameterType
programProgram
typeType
predicate(subType) => null | boolean

Returns

boolean


isBuiltinTypeAliasLike()

function isBuiltinTypeAliasLike(
program,
type,
predicate): boolean;

Defined in: builtinSymbolLikes.ts:88

Parameters

ParameterType
programProgram
typeType
predicate(subType) => boolean

Returns

boolean


isErrorLike()

function isErrorLike(program, type): boolean;

Defined in: builtinSymbolLikes.ts:41

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

class Foo extends Error {}
new Foo()
// ^ ErrorLike

isNullableType()

function isNullableType(type): boolean;

Defined in: predicates.ts:12

Checks if the given type is (or accepts) nullable

Parameters

ParameterType
typeType

Returns

boolean


isPromiseConstructorLike()

function isPromiseConstructorLike(program, type): boolean;

Defined in: builtinSymbolLikes.ts:26

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

const value = Promise
value.reject
// ^ PromiseConstructorLike

isPromiseLike()

function isPromiseLike(program, type): boolean;

Defined in: builtinSymbolLikes.ts:14

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

class DerivedClass extends Promise<number> {}
DerivedClass.reject
// ^ PromiseLike

isReadonlyErrorLike()

function isReadonlyErrorLike(program, type): boolean;

Defined in: builtinSymbolLikes.ts:52

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

type T = Readonly<Error>
// ^ ReadonlyErrorLike

isReadonlyTypeLike()

function isReadonlyTypeLike(
program,
type,
predicate?): boolean;

Defined in: builtinSymbolLikes.ts:72

Parameters

ParameterType
programProgram
typeType
predicate?(subType) => boolean

Returns

boolean

Example

type T = Readonly<{ foo: 'bar' }>
// ^ ReadonlyTypeLike

isSymbolFromDefaultLibrary()

function isSymbolFromDefaultLibrary(program, symbol): boolean;

Defined in: isSymbolFromDefaultLibrary.ts:3

Parameters

ParameterType
programProgram
symbolundefined | Symbol

Returns

boolean


isTypeAnyArrayType()

function isTypeAnyArrayType(type, checker): boolean;

Defined in: predicates.ts:88

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is any[]


isTypeAnyType()

function isTypeAnyType(type): boolean;

Defined in: predicates.ts:75

Parameters

ParameterType
typeType

Returns

boolean

true if the type is any


isTypeArrayTypeOrUnionOfArrayTypes()

function isTypeArrayTypeOrUnionOfArrayTypes(type, checker): boolean;

Defined in: predicates.ts:27

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

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean


isTypeBigIntLiteralType()

function isTypeBigIntLiteralType(type): type is BigIntLiteralType;

Defined in: predicates.ts:140

Parameters

ParameterType
typeType

Returns

type is BigIntLiteralType


isTypeFlagSet()

function isTypeFlagSet(
type,
flagsToCheck,
isReceiver?): boolean;

Defined in: typeFlagUtils.ts:27

Parameters

ParameterTypeDescription
typeType-
flagsToCheckTypeFlagsThe composition of one or more ts.TypeFlags.
isReceiver?booleanWhether the type is a receiving type (e.g. the type of a called function's parameter).

Returns

boolean

Remarks

Note that if the type is a union, this function will decompose it into the parts and get the flags of every union constituent. If this is not desired, use the isTypeFlag function from tsutils.


isTypeNeverType()

function isTypeNeverType(type): boolean;

Defined in: predicates.ts:43

Parameters

ParameterType
typeType

Returns

boolean

true if the type is never


isTypeReadonly()

function isTypeReadonly(
program,
type,
options): boolean;

Defined in: isTypeReadonly.ts:334

Checks if the given type is readonly

Parameters

ParameterTypeDefault value
programProgramundefined
typeTypeundefined
optionsReadonlynessOptionsreadonlynessOptionsDefaults

Returns

boolean


isTypeReferenceType()

function isTypeReferenceType(type): type is TypeReference;

Defined in: predicates.ts:64

Parameters

ParameterType
typeType

Returns

type is TypeReference


isTypeTemplateLiteralType()

function isTypeTemplateLiteralType(type): type is TemplateLiteralType;

Defined in: predicates.ts:146

Parameters

ParameterType
typeType

Returns

type is TemplateLiteralType


isTypeUnknownArrayType()

function isTypeUnknownArrayType(type, checker): boolean;

Defined in: predicates.ts:101

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is unknown[]


isTypeUnknownType()

function isTypeUnknownType(type): boolean;

Defined in: predicates.ts:50

Parameters

ParameterType
typeType

Returns

boolean

true if the type is unknown


isUnsafeAssignment()

function isUnsafeAssignment(
type,
receiver,
checker,
senderNode):
| false
| {
receiver: Type;
sender: Type;
};

Defined in: isUnsafeAssignment.ts:19

Does a simple check to see if there is an any being assigned to a non-any type.

This also checks generic positions to ensure there's no unsafe sub-assignments. Note: in the case of generic positions, it makes the assumption that the two types are the same.

Parameters

ParameterType
typeType
receiverType
checkerTypeChecker
senderNodenull | Node

Returns

| false | { receiver: Type; sender: Type; }

false if it's safe, or an object with the two types if it's unsafe

Example

See tests for examples

requiresQuoting()

function requiresQuoting(name, target): boolean;

Defined in: requiresQuoting.ts:3

  • Indicates whether identifiers require the use of quotation marks when accessing property definitions and dot notation.

Parameters

ParameterTypeDefault value
namestringundefined
targetScriptTargetts.ScriptTarget.ESNext

Returns

boolean


typeIsOrHasBaseType()

function typeIsOrHasBaseType(type, parentType): boolean;

Defined in: predicates.ts:114

Parameters

ParameterType
typeType
parentTypeType

Returns

boolean

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


typeMatchesSomeSpecifier()

function typeMatchesSomeSpecifier(
type,
specifiers,
program): boolean;

Defined in: TypeOrValueSpecifier.ts:216

Parameters

ParameterTypeDefault value
typeTypeundefined
specifiersTypeOrValueSpecifier[][]
programProgramundefined

Returns

boolean


typeMatchesSpecifier()

function typeMatchesSpecifier(
type,
specifier,
program): boolean;

Defined in: TypeOrValueSpecifier.ts:165

Parameters

ParameterType
typeType
specifierTypeOrValueSpecifier
programProgram

Returns

boolean

Variables

readonlynessOptionsDefaults

const readonlynessOptionsDefaults: ReadonlynessOptions;

Defined in: isTypeReadonly.ts:40


readonlynessOptionsSchema

const readonlynessOptionsSchema: object;

Defined in: isTypeReadonly.ts:29

Type declaration

NameTypeDefault valueDefined in
additionalPropertiesfalsefalseisTypeReadonly.ts:30
propertiesobject-isTypeReadonly.ts:31
properties.allowobjecttypeOrValueSpecifiersSchemaisTypeReadonly.ts:32
properties.allow.itemsobject-TypeOrValueSpecifier.ts:71
properties.allow.items.oneOf[{ type: "string"; }, { additionalProperties: false; properties: { from: { enum: ["file"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: ...; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; path: { type: "string"; }; }; required: ["from", "name"]; type: "object"; }, { additionalProperties: false; properties: { from: { enum: ["lib"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: ...; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; }; required: ["from", "name"]; type: "object"; }, { additionalProperties: false; properties: { from: { enum: ["package"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: ...; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; package: { type: "string"; }; }; required: ["from", "name", "package"]; type: "object"; }]-TypeOrValueSpecifier.ts:72
properties.allow.type"array"'array'TypeOrValueSpecifier.ts:162
properties.treatMethodsAsReadonlyobject-isTypeReadonly.ts:33
properties.treatMethodsAsReadonly.type"boolean"'boolean'isTypeReadonly.ts:34
type"object"'object'isTypeReadonly.ts:37

typeOrValueSpecifiersSchema

const typeOrValueSpecifiersSchema: object;

Defined in: TypeOrValueSpecifier.ts:70

Type declaration

NameTypeDefault valueDefined in
itemsobject-TypeOrValueSpecifier.ts:71
items.oneOf[{ type: "string"; }, { additionalProperties: false; properties: { from: { enum: ["file"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: "string"; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; path: { type: "string"; }; }; required: ["from", "name"]; type: "object"; }, { additionalProperties: false; properties: { from: { enum: ["lib"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: "string"; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; }; required: ["from", "name"]; type: "object"; }, { additionalProperties: false; properties: { from: { enum: ["package"]; type: "string"; }; name: { oneOf: [{ type: "string"; }, { items: { type: "string"; }; minItems: 1; type: "array"; uniqueItems: true; }]; }; package: { type: "string"; }; }; required: ["from", "name", "package"]; type: "object"; }]-TypeOrValueSpecifier.ts:72
type"array"'array'TypeOrValueSpecifier.ts:162

Enumerations

AnyType

Defined in: discriminateAnyType.ts:7

Enumeration Members

Enumeration MemberValueDefined in
Any0discriminateAnyType.ts:8
AnyArray2discriminateAnyType.ts:10
PromiseAny1discriminateAnyType.ts:9
Safe3discriminateAnyType.ts:11

Interfaces

FileSpecifier

Defined in: TypeOrValueSpecifier.ts:15

Describes specific types or values declared in local files. See TypeOrValueSpecifier > FileSpecifier.

Properties

PropertyTypeDescriptionDefined in
from"file"-TypeOrValueSpecifier.ts:16
namestring | string[]Type or value name(s) to match on.TypeOrValueSpecifier.ts:21
path?stringA specific file the types or values must be declared in.TypeOrValueSpecifier.ts:26

LibSpecifier

Defined in: TypeOrValueSpecifier.ts:33

Describes specific types or values declared in TypeScript's built-in lib definitions. See TypeOrValueSpecifier > LibSpecifier.

Properties

PropertyTypeDescriptionDefined in
from"lib"-TypeOrValueSpecifier.ts:34
namestring | string[]Type or value name(s) to match on.TypeOrValueSpecifier.ts:39

PackageSpecifier

Defined in: TypeOrValueSpecifier.ts:46

Describes specific types or values imported from packages. See TypeOrValueSpecifier > PackageSpecifier.

Properties

PropertyTypeDescriptionDefined in
from"package"-TypeOrValueSpecifier.ts:47
namestring | string[]Type or value name(s) to match on.TypeOrValueSpecifier.ts:52
packagestringPackage name the type or value must be declared in.TypeOrValueSpecifier.ts:57

ReadonlynessOptions

Defined in: isTypeReadonly.ts:24

Properties

PropertyModifierTypeDefined in
allow?readonlyTypeOrValueSpecifier[]isTypeReadonly.ts:25
treatMethodsAsReadonly?readonlybooleanisTypeReadonly.ts:26

Type Aliases

TypeOrValueSpecifier

type TypeOrValueSpecifier = 
| string
| FileSpecifier
| LibSpecifier
| PackageSpecifier;

Defined in: TypeOrValueSpecifier.ts:64

A centralized format for rule options to describe specific types and/or values. See TypeOrValueSpecifier.