Skip to main content

@typescript-eslint/type-utils

npm: @typescript-eslint/type-utils v8.15.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

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).

Defined in

containsAllTypesByName.ts:13


discriminateAnyType()

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

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.

Defined in

predicates.ts:121


getConstrainedTypeAtLocation()

function getConstrainedTypeAtLocation(services, node): ts.Type

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

Parameters

ParameterType
servicesParserServicesWithTypeInformation
nodeNode

Returns

ts.Type

Defined in

getConstrainedTypeAtLocation.ts:10


getContextualType()

function getContextualType(checker, node): ts.Type | undefined

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

ts.Type | undefined

Defined in

getContextualType.ts:8


getDeclaration()

function getDeclaration(services, node): ts.Declaration | null

Gets the declaration for the given variable

Parameters

ParameterType
servicesParserServicesWithTypeInformation
nodeNode

Returns

ts.Declaration | null

Defined in

getDeclaration.ts:10


getSourceFileOfNode()

function getSourceFileOfNode(node): ts.SourceFile

Gets the source file for a given node

Parameters

ParameterType
nodeNode

Returns

ts.SourceFile

Defined in

getSourceFileOfNode.ts:6


getTypeFlags()

function getTypeFlags(type): ts.TypeFlags

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

Parameters

ParameterType
typeType

Returns

ts.TypeFlags

Defined in

typeFlagUtils.ts:9


getTypeName()

function getTypeName(typeChecker, type): string

Get the type name of a given type.

Parameters

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

Returns

string

Defined in

getTypeName.ts:8


getTypeOfPropertyOfName()

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

Parameters

ParameterType
checkerTypeChecker
typeType
namestring
escapedName?__String

Returns

ts.Type | undefined

Defined in

propertyTypes.ts:3


getTypeOfPropertyOfType()

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

Parameters

ParameterType
checkerTypeChecker
typeType
propertySymbol

Returns

ts.Type | undefined

Defined in

propertyTypes.ts:25


isBuiltinSymbolLike()

function isBuiltinSymbolLike(
program,
type,
symbolName): boolean

Parameters

ParameterType
programProgram
typeType
symbolNamestring | string[]

Returns

boolean

Defined in

builtinSymbolLikes.ts:121


isBuiltinSymbolLikeRecurser()

function isBuiltinSymbolLikeRecurser(
program,
type,
predicate): boolean

Parameters

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

Returns

boolean

Defined in

builtinSymbolLikes.ts:147


isBuiltinTypeAliasLike()

function isBuiltinTypeAliasLike(
program,
type,
predicate): boolean

Parameters

ParameterType
programProgram
typeType
predicate(subType) => boolean

Returns

boolean

Defined in

builtinSymbolLikes.ts:88


isErrorLike()

function isErrorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

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

Defined in

builtinSymbolLikes.ts:41


isNullableType()

function isNullableType(type): boolean

Checks if the given type is (or accepts) nullable

Parameters

ParameterType
typeType

Returns

boolean

Defined in

predicates.ts:12


isPromiseConstructorLike()

function isPromiseConstructorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

const value = Promise
value.reject
// ^ PromiseConstructorLike

Defined in

builtinSymbolLikes.ts:26


isPromiseLike()

function isPromiseLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

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

Defined in

builtinSymbolLikes.ts:14


isReadonlyErrorLike()

function isReadonlyErrorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

type T = Readonly<Error>
// ^ ReadonlyErrorLike

Defined in

builtinSymbolLikes.ts:52


isReadonlyTypeLike()

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

Parameters

ParameterType
programProgram
typeType
predicate?(subType) => boolean

Returns

boolean

Example

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

Defined in

builtinSymbolLikes.ts:72


isSymbolFromDefaultLibrary()

function isSymbolFromDefaultLibrary(program, symbol): boolean

Parameters

ParameterType
programProgram
symbolundefined | Symbol

Returns

boolean

Defined in

isSymbolFromDefaultLibrary.ts:3


isTypeAnyArrayType()

function isTypeAnyArrayType(type, checker): boolean

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is any[]

Defined in

predicates.ts:88


isTypeAnyType()

function isTypeAnyType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is any

Defined in

predicates.ts:75


isTypeArrayTypeOrUnionOfArrayTypes()

function isTypeArrayTypeOrUnionOfArrayTypes(type, checker): boolean

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

Defined in

predicates.ts:27


isTypeBigIntLiteralType()

function isTypeBigIntLiteralType(type): type is BigIntLiteralType

Parameters

ParameterType
typeType

Returns

type is BigIntLiteralType

Defined in

predicates.ts:182


isTypeFlagSet()

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

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.

Defined in

typeFlagUtils.ts:27


isTypeNeverType()

function isTypeNeverType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is never

Defined in

predicates.ts:43


isTypeReadonly()

function isTypeReadonly(
program,
type,
options): boolean

Checks if the given type is readonly

Parameters

ParameterTypeDefault value
programProgramundefined
typeTypeundefined
optionsReadonlynessOptionsreadonlynessOptionsDefaults

Returns

boolean

Defined in

isTypeReadonly.ts:334


isTypeReferenceType()

function isTypeReferenceType(type): type is TypeReference

Parameters

ParameterType
typeType

Returns

type is TypeReference

Defined in

predicates.ts:64


isTypeTemplateLiteralType()

function isTypeTemplateLiteralType(type): type is TemplateLiteralType

Parameters

ParameterType
typeType

Returns

type is TemplateLiteralType

Defined in

predicates.ts:188


isTypeUnknownArrayType()

function isTypeUnknownArrayType(type, checker): boolean

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is unknown[]

Defined in

predicates.ts:101


isTypeUnknownType()

function isTypeUnknownType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is unknown

Defined in

predicates.ts:50


isUnsafeAssignment()

function isUnsafeAssignment(
type,
receiver,
checker,
senderNode): object | false

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

object | false

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

Example

See tests for examples

Defined in

isUnsafeAssignment.ts:19


requiresQuoting()

function requiresQuoting(name, target): boolean
  • 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

Defined in

requiresQuoting.ts:3


typeIsOrHasBaseType()

function typeIsOrHasBaseType(type, parentType): boolean

Parameters

ParameterType
typeType
parentTypeType

Returns

boolean

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

Defined in

predicates.ts:156


typeMatchesSomeSpecifier()

function typeMatchesSomeSpecifier(
type,
specifiers,
program): boolean

Parameters

ParameterTypeDefault value
typeTypeundefined
specifiersTypeOrValueSpecifier[][]
programProgramundefined

Returns

boolean

Defined in

TypeOrValueSpecifier.ts:199


typeMatchesSpecifier()

function typeMatchesSpecifier(
type,
specifier,
program): boolean

Parameters

ParameterType
typeType
specifierTypeOrValueSpecifier
programProgram

Returns

boolean

Defined in

TypeOrValueSpecifier.ts:165

Variables

readonlynessOptionsDefaults

const readonlynessOptionsDefaults: ReadonlynessOptions;

Defined in

isTypeReadonly.ts:40


readonlynessOptionsSchema

const readonlynessOptionsSchema: object;

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[object, object, object, 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

Defined in

isTypeReadonly.ts:29


typeOrValueSpecifiersSchema

const typeOrValueSpecifiersSchema: object;

Type declaration

NameTypeDefault valueDefined in
itemsobject-TypeOrValueSpecifier.ts:71
items.oneOf[object, object, object, object]-TypeOrValueSpecifier.ts:72
type"array"'array'TypeOrValueSpecifier.ts:162

Defined in

TypeOrValueSpecifier.ts:70

Enumerations

AnyType

Enumeration Members

Enumeration MemberValueDefined in
Any0predicates.ts:112
AnyArray2predicates.ts:114
PromiseAny1predicates.ts:113
Safe3predicates.ts:115

Interfaces

FileSpecifier

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

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

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

Properties

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

Type Aliases

TypeOrValueSpecifier

type TypeOrValueSpecifier: string | FileSpecifier | LibSpecifier | PackageSpecifier;

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

Defined in

TypeOrValueSpecifier.ts:64