@typescript-eslint/type-utils
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/utilsso that that package does not require a dependency ontypescript
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
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| type | Type | undefined | Type being checked by name. | 
| allowAny | boolean | undefined | Whether to consider anyandunknownto match. | 
| allowedNames | Set<string> | undefined | Symbol names checking on the type. | 
| matchAnyInstead | boolean | false | Whether 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
discriminateAnyType()
function discriminateAnyType(
   type, 
   checker, 
   program, 
   tsNode): AnyType
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
| checker | TypeChecker | 
| program | Program | 
| tsNode | Node | 
Returns
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
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
| Parameter | Type | 
|---|---|
| services | ParserServicesWithTypeInformation | 
| node | Node | 
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
| Parameter | Type | 
|---|---|
| checker | TypeChecker | 
| node | Expression | 
Returns
ts.Type | undefined
Defined in
getDeclaration()
function getDeclaration(services, node): ts.Declaration | null
Gets the declaration for the given variable
Parameters
| Parameter | Type | 
|---|---|
| services | ParserServicesWithTypeInformation | 
| node | Node | 
Returns
ts.Declaration | null
Defined in
getSourceFileOfNode()
function getSourceFileOfNode(node): ts.SourceFile
Gets the source file for a given node
Parameters
| Parameter | Type | 
|---|---|
| node | Node | 
Returns
ts.SourceFile
Defined in
getTypeFlags()
function getTypeFlags(type): ts.TypeFlags
Gets all of the type flags in a type, iterating through unions automatically.
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
ts.TypeFlags
Defined in
getTypeName()
function getTypeName(typeChecker, type): string
Get the type name of a given type.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| typeChecker | TypeChecker | The context sensitive TypeScript TypeChecker. | 
| type | Type | The type to get the name of. | 
Returns
string
Defined in
getTypeOfPropertyOfName()
function getTypeOfPropertyOfName(
   checker, 
   type, 
   name, 
   escapedName?): ts.Type | undefined
Parameters
| Parameter | Type | 
|---|---|
| checker | TypeChecker | 
| type | Type | 
| name | string | 
| escapedName? | __String | 
Returns
ts.Type | undefined
Defined in
getTypeOfPropertyOfType()
function getTypeOfPropertyOfType(
   checker, 
   type, 
   property): ts.Type | undefined
Parameters
| Parameter | Type | 
|---|---|
| checker | TypeChecker | 
| type | Type | 
| property | Symbol | 
Returns
ts.Type | undefined
Defined in
isBuiltinSymbolLike()
function isBuiltinSymbolLike(
   program, 
   type, 
   symbolName): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
| symbolName | string|string[] | 
Returns
boolean
Defined in
isBuiltinSymbolLikeRecurser()
function isBuiltinSymbolLikeRecurser(
   program, 
   type, 
   predicate): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
| predicate | ( subType) =>null|boolean | 
Returns
boolean
Defined in
isBuiltinTypeAliasLike()
function isBuiltinTypeAliasLike(
   program, 
   type, 
   predicate): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
| predicate | ( subType) =>boolean | 
Returns
boolean
Defined in
isErrorLike()
function isErrorLike(program, type): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
Returns
boolean
Example
class Foo extends Error {}
new Foo()
//   ^ ErrorLike
Defined in
isNullableType()
function isNullableType(type): boolean
Checks if the given type is (or accepts) nullable
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
boolean
Defined in
isPromiseConstructorLike()
function isPromiseConstructorLike(program, type): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
Returns
boolean
Example
const value = Promise
value.reject
// ^ PromiseConstructorLike
Defined in
isPromiseLike()
function isPromiseLike(program, type): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
Returns
boolean
Example
class DerivedClass extends Promise<number> {}
DerivedClass.reject
// ^ PromiseLike
Defined in
isReadonlyErrorLike()
function isReadonlyErrorLike(program, type): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
Returns
boolean
Example
type T = Readonly<Error>
//   ^ ReadonlyErrorLike
Defined in
isReadonlyTypeLike()
function isReadonlyTypeLike(
   program, 
   type, 
   predicate?): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| type | Type | 
| predicate? | ( subType) =>boolean | 
Returns
boolean
Example
type T = Readonly<{ foo: 'bar' }>
//   ^ ReadonlyTypeLike
Defined in
isSymbolFromDefaultLibrary()
function isSymbolFromDefaultLibrary(program, symbol): boolean
Parameters
| Parameter | Type | 
|---|---|
| program | Program | 
| symbol | undefined|Symbol | 
Returns
boolean
Defined in
isSymbolFromDefaultLibrary.ts:3
isTypeAnyArrayType()
function isTypeAnyArrayType(type, checker): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
| checker | TypeChecker | 
Returns
boolean
true if the type is any[]
Defined in
isTypeAnyType()
function isTypeAnyType(type): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
boolean
true if the type is any
Defined in
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
| Parameter | Type | 
|---|---|
| type | Type | 
| checker | TypeChecker | 
Returns
boolean
Defined in
isTypeBigIntLiteralType()
function isTypeBigIntLiteralType(type): type is BigIntLiteralType
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
type is BigIntLiteralType
Defined in
isTypeFlagSet()
function isTypeFlagSet(
   type, 
   flagsToCheck, 
   isReceiver?): boolean
Parameters
| Parameter | Type | Description | 
|---|---|---|
| type | Type | - | 
| flagsToCheck | TypeFlags | The composition of one or more ts.TypeFlags. | 
| isReceiver? | boolean | Whether 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
isTypeNeverType()
function isTypeNeverType(type): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
boolean
true if the type is never
Defined in
isTypeReadonly()
function isTypeReadonly(
   program, 
   type, 
   options): boolean
Checks if the given type is readonly
Parameters
| Parameter | Type | Default value | 
|---|---|---|
| program | Program | undefined | 
| type | Type | undefined | 
| options | ReadonlynessOptions | readonlynessOptionsDefaults | 
Returns
boolean
Defined in
isTypeReferenceType()
function isTypeReferenceType(type): type is TypeReference
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
type is TypeReference
Defined in
isTypeTemplateLiteralType()
function isTypeTemplateLiteralType(type): type is TemplateLiteralType
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
type is TemplateLiteralType
Defined in
isTypeUnknownArrayType()
function isTypeUnknownArrayType(type, checker): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
| checker | TypeChecker | 
Returns
boolean
true if the type is unknown[]
Defined in
isTypeUnknownType()
function isTypeUnknownType(type): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
Returns
boolean
true if the type is unknown
Defined in
isUnsafeAssignment()
function isUnsafeAssignment(
   type, 
   receiver, 
   checker, 
   senderNode): {
  receiver: ts.Type;
  sender: ts.Type;
 } | 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
| Parameter | Type | 
|---|---|
| type | Type | 
| receiver | Type | 
| checker | TypeChecker | 
| senderNode | null|Node | 
Returns
{
receiver: ts.Type;
sender: ts.Type;
} | false
false if it's safe, or an object with the two types if it's unsafe
Example
See tests for examples
Defined in
requiresQuoting()
function requiresQuoting(name, target): boolean
- Indicates whether identifiers require the use of quotation marks when accessing property definitions and dot notation.
Parameters
| Parameter | Type | Default value | 
|---|---|---|
| name | string | undefined | 
| target | ScriptTarget | ts.ScriptTarget.ESNext | 
Returns
boolean
Defined in
typeIsOrHasBaseType()
function typeIsOrHasBaseType(type, parentType): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
| parentType | Type | 
Returns
boolean
Whether a type is an instance of the parent type, including for the parent's base types.
Defined in
typeMatchesSomeSpecifier()
function typeMatchesSomeSpecifier(
   type, 
   specifiers, 
   program): boolean
Parameters
| Parameter | Type | Default value | 
|---|---|---|
| type | Type | undefined | 
| specifiers | TypeOrValueSpecifier[] | [] | 
| program | Program | undefined | 
Returns
boolean
Defined in
typeMatchesSpecifier()
function typeMatchesSpecifier(
   type, 
   specifier, 
   program): boolean
Parameters
| Parameter | Type | 
|---|---|
| type | Type | 
| specifier | TypeOrValueSpecifier | 
| program | Program | 
Returns
boolean
Defined in
Variables
readonlynessOptionsDefaults
const readonlynessOptionsDefaults: ReadonlynessOptions;
Defined in
readonlynessOptionsSchema
const readonlynessOptionsSchema: object;
Type declaration
| Name | Type | Default value | Defined in | 
|---|---|---|---|
| additionalProperties | false | false | isTypeReadonly.ts:30 | 
| properties | object | - | isTypeReadonly.ts:31 | 
| properties.allow | object | typeOrValueSpecifiersSchema | isTypeReadonly.ts:32 | 
| properties.allow.items | object | - | TypeOrValueSpecifier.ts:71 | 
| properties.allow.items.oneOf | [ object,object,object,object] | - | TypeOrValueSpecifier.ts:72 | 
| properties.allow.type | "array" | 'array' | TypeOrValueSpecifier.ts:162 | 
| properties.treatMethodsAsReadonly | object | - | isTypeReadonly.ts:33 | 
| properties.treatMethodsAsReadonly.type | "boolean" | 'boolean' | isTypeReadonly.ts:34 | 
| type | "object" | 'object' | isTypeReadonly.ts:37 | 
Defined in
typeOrValueSpecifiersSchema
const typeOrValueSpecifiersSchema: object;
Type declaration
| Name | Type | Default value | Defined in | 
|---|---|---|---|
| items | object | - | TypeOrValueSpecifier.ts:71 | 
| items.oneOf | [ object,object,object,object] | - | TypeOrValueSpecifier.ts:72 | 
| type | "array" | 'array' | TypeOrValueSpecifier.ts:162 | 
Defined in
Enumerations
AnyType
Enumeration Members
| Enumeration Member | Value | Defined in | 
|---|---|---|
| Any | 0 | predicates.ts:112 | 
| AnyArray | 2 | predicates.ts:114 | 
| PromiseAny | 1 | predicates.ts:113 | 
| Safe | 3 | predicates.ts:115 | 
Interfaces
FileSpecifier
Describes specific types or values declared in local files. See TypeOrValueSpecifier > FileSpecifier.
Properties
| Property | Type | Description | Defined in | 
|---|---|---|---|
| from | "file" | - | TypeOrValueSpecifier.ts:16 | 
| name | string|string[] | Type or value name(s) to match on. | TypeOrValueSpecifier.ts:21 | 
| path? | string | A 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
| Property | Type | Description | Defined in | 
|---|---|---|---|
| from | "lib" | - | TypeOrValueSpecifier.ts:34 | 
| name | string|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
| Property | Type | Description | Defined in | 
|---|---|---|---|
| from | "package" | - | TypeOrValueSpecifier.ts:47 | 
| name | string|string[] | Type or value name(s) to match on. | TypeOrValueSpecifier.ts:52 | 
| package | string | Package name the type or value must be declared in. | TypeOrValueSpecifier.ts:57 | 
ReadonlynessOptions
Properties
| Property | Modifier | Type | Defined in | 
|---|---|---|---|
| allow? | readonly | TypeOrValueSpecifier[] | isTypeReadonly.ts:25 | 
| treatMethodsAsReadonly? | readonly | boolean | isTypeReadonly.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.