Skip to main content

@typescript-eslint/type-utils

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

Source

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.

Source

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

Source

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

Source

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

Source

getDeclaration.ts:10


getSourceFileOfNode()

function getSourceFileOfNode(node): ts.SourceFile

Gets the source file for a given node

Parameters

ParameterType
nodeNode

Returns

ts.SourceFile

Source

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

Source

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

Source

getTypeName.ts:8


getTypeOfPropertyOfName()

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

Parameters

ParameterType
checkerTypeChecker
typeType
namestring
escapedName?__String

Returns

ts.Type | undefined

Source

propertyTypes.ts:3


getTypeOfPropertyOfType()

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

Parameters

ParameterType
checkerTypeChecker
typeType
propertySymbol

Returns

ts.Type | undefined

Source

propertyTypes.ts:25


isBuiltinSymbolLike()

function isBuiltinSymbolLike(
program,
type,
symbolName): boolean

Parameters

ParameterType
programProgram
typeType
symbolNamestring | string[]

Returns

boolean

Source

builtinSymbolLikes.ts:120


isBuiltinSymbolLikeRecurser()

function isBuiltinSymbolLikeRecurser(
program,
type,
predicate): boolean

Parameters

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

Returns

boolean

Source

builtinSymbolLikes.ts:146


isBuiltinTypeAliasLike()

function isBuiltinTypeAliasLike(
program,
type,
predicate): boolean

Parameters

ParameterType
programProgram
typeType
predicate(subType) => boolean

Returns

boolean

Source

builtinSymbolLikes.ts:87


isErrorLike()

function isErrorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

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

Source

builtinSymbolLikes.ts:40


isNullableType()

function isNullableType(type): boolean

Checks if the given type is (or accepts) nullable

Parameters

ParameterType
typeType

Returns

boolean

Source

predicates.ts:12


isPromiseConstructorLike()

function isPromiseConstructorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

const value = Promise
value.reject
// ^ PromiseConstructorLike

Source

builtinSymbolLikes.ts:25


isPromiseLike()

function isPromiseLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

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

Source

builtinSymbolLikes.ts:13


isReadonlyErrorLike()

function isReadonlyErrorLike(program, type): boolean

Parameters

ParameterType
programProgram
typeType

Returns

boolean

Example

type T = Readonly<Error>
// ^ ReadonlyErrorLike

Source

builtinSymbolLikes.ts:51


isReadonlyTypeLike()

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

Parameters

ParameterType
programProgram
typeType
predicate?(subType) => boolean

Returns

boolean

Example

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

Source

builtinSymbolLikes.ts:71


isSymbolFromDefaultLibrary()

function isSymbolFromDefaultLibrary(program, symbol): boolean

Parameters

ParameterType
programProgram
symbolundefined | Symbol

Returns

boolean

Source

isSymbolFromDefaultLibrary.ts:3


isTypeAnyArrayType()

function isTypeAnyArrayType(type, checker): boolean

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is any[]

Source

predicates.ts:88


isTypeAnyType()

function isTypeAnyType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is any

Source

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

Source

predicates.ts:27


isTypeBigIntLiteralType()

function isTypeBigIntLiteralType(type): type is BigIntLiteralType

Parameters

ParameterType
typeType

Returns

type is BigIntLiteralType

Source

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.

Source

typeFlagUtils.ts:27


isTypeNeverType()

function isTypeNeverType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is never

Source

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

Source

isTypeReadonly.ts:332


isTypeReferenceType()

function isTypeReferenceType(type): type is TypeReference

Parameters

ParameterType
typeType

Returns

type is TypeReference

Source

predicates.ts:64


isTypeTemplateLiteralType()

function isTypeTemplateLiteralType(type): type is TemplateLiteralType

Parameters

ParameterType
typeType

Returns

type is TemplateLiteralType

Source

predicates.ts:188


isTypeUnknownArrayType()

function isTypeUnknownArrayType(type, checker): boolean

Parameters

ParameterType
typeType
checkerTypeChecker

Returns

boolean

true if the type is unknown[]

Source

predicates.ts:101


isTypeUnknownType()

function isTypeUnknownType(type): boolean

Parameters

ParameterType
typeType

Returns

boolean

true if the type is unknown

Source

predicates.ts:50


isUnsafeAssignment()

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

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

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

Example

See tests for examples

Source

isUnsafeAssignment.ts:18


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

Source

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.

Source

predicates.ts:156


typeMatchesSomeSpecifier()

function typeMatchesSomeSpecifier(
type,
specifiers,
program): boolean

Parameters

ParameterTypeDefault value
typeTypeundefined
specifiersTypeOrValueSpecifier[][]
programProgramundefined

Returns

boolean

Source

TypeOrValueSpecifier.ts:198


typeMatchesSpecifier()

function typeMatchesSpecifier(
type,
specifier,
program): boolean

Parameters

ParameterType
typeType
specifierTypeOrValueSpecifier
programProgram

Returns

boolean

Source

TypeOrValueSpecifier.ts:164

Variables

readonlynessOptionsDefaults

const readonlynessOptionsDefaults: ReadonlynessOptions;

Source

isTypeReadonly.ts:38


readonlynessOptionsSchema

const readonlynessOptionsSchema: object;

Type declaration

MemberTypeValue
additionalPropertiesfalsefalse
propertiesobject...
properties.allowobjecttypeOrValueSpecifiersSchema
properties.allow.itemsobject...
properties.allow.items.oneOf[object, object, object, object]...
properties.allow.type"array"'array'
properties.treatMethodsAsReadonlyobject...
properties.treatMethodsAsReadonly.type"boolean"'boolean'
type"object"'object'

Source

isTypeReadonly.ts:27


typeOrValueSpecifiersSchema

const typeOrValueSpecifiersSchema: object;

Type declaration

MemberTypeValue
itemsobject...
items.oneOf[object, object, object, object]...
type"array"'array'

Source

TypeOrValueSpecifier.ts:69

Enumerations

AnyType

Enumeration Members

Enumeration MemberValue
Any0
AnyArray2
PromiseAny1
Safe3

Interfaces

FileSpecifier

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

Properties

PropertyTypeDescription
from"file"-
namestring | string[]Type or value name(s) to match on.
path?stringA specific file the types or values must be declared in.

LibSpecifier

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

Properties

PropertyTypeDescription
from"lib"-
namestring | string[]Type or value name(s) to match on.

PackageSpecifier

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

Properties

PropertyTypeDescription
from"package"-
namestring | string[]Type or value name(s) to match on.
packagestringPackage name the type or value must be declared in.

ReadonlynessOptions

Properties

PropertyModifierType
allow?readonlyTypeOrValueSpecifier[]
treatMethodsAsReadonly?readonlyboolean

Type Aliases

TypeOrValueSpecifier

type TypeOrValueSpecifier: FileSpecifier | LibSpecifier | PackageSpecifier | string;

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

Source

TypeOrValueSpecifier.ts:63