@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/utils
so 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
Defined in: containsAllTypesByName.ts:13
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
type | Type | undefined | Type being checked by name. |
allowAny | boolean | undefined | Whether to consider any and unknown to 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).
discriminateAnyType()
function discriminateAnyType(
type,
checker,
program,
tsNode): AnyType
Defined in: predicates.ts:121
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
.
getConstrainedTypeAtLocation()
function getConstrainedTypeAtLocation(services, node): ts.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
Parameter | Type |
---|---|
services | ParserServicesWithTypeInformation |
node | Node |
Returns
ts.Type
See
https://github.com/typescript-eslint/typescript-eslint/issues/10438
getContextualType()
function getContextualType(checker, node): ts.Type | undefined
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
Parameter | Type |
---|---|
checker | TypeChecker |
node | Expression |
Returns
ts.Type
| undefined
getDeclaration()
function getDeclaration(services, node): ts.Declaration | null
Defined in: getDeclaration.ts:10
Gets the declaration for the given variable
Parameters
Parameter | Type |
---|---|
services | ParserServicesWithTypeInformation |
node | Node |
Returns
ts.Declaration
| null
getSourceFileOfNode()
function getSourceFileOfNode(node): ts.SourceFile
Defined in: getSourceFileOfNode.ts:6
Gets the source file for a given node
Parameters
Parameter | Type |
---|---|
node | Node |
Returns
ts.SourceFile
getTypeFlags()
function getTypeFlags(type): ts.TypeFlags
Defined in: typeFlagUtils.ts:9
Gets all of the type flags in a type, iterating through unions automatically.
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
ts.TypeFlags
getTypeName()
function getTypeName(typeChecker, type): string
Defined in: getTypeName.ts:8
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
getTypeOfPropertyOfName()
function getTypeOfPropertyOfName(
checker,
type,
name,
escapedName?): ts.Type | undefined
Defined in: propertyTypes.ts:3
Parameters
Parameter | Type |
---|---|
checker | TypeChecker |
type | Type |
name | string |
escapedName ? | __String |
Returns
ts.Type
| undefined
getTypeOfPropertyOfType()
function getTypeOfPropertyOfType(
checker,
type,
property): ts.Type | undefined
Defined in: propertyTypes.ts:25
Parameters
Parameter | Type |
---|---|
checker | TypeChecker |
type | Type |
property | Symbol |
Returns
ts.Type
| undefined
isBuiltinSymbolLike()
function isBuiltinSymbolLike(
program,
type,
symbolName): boolean
Defined in: builtinSymbolLikes.ts:121
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
symbolName | string | string [] |
Returns
boolean
isBuiltinSymbolLikeRecurser()
function isBuiltinSymbolLikeRecurser(
program,
type,
predicate): boolean
Defined in: builtinSymbolLikes.ts:147
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
predicate | (subType ) => null | boolean |
Returns
boolean
isBuiltinTypeAliasLike()
function isBuiltinTypeAliasLike(
program,
type,
predicate): boolean
Defined in: builtinSymbolLikes.ts:88
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
predicate | (subType ) => boolean |
Returns
boolean
isErrorLike()
function isErrorLike(program, type): boolean
Defined in: builtinSymbolLikes.ts:41
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
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
Parameter | Type |
---|---|
type | Type |
Returns
boolean
isPromiseConstructorLike()
function isPromiseConstructorLike(program, type): boolean
Defined in: builtinSymbolLikes.ts:26
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
const value = Promise
value.reject
// ^ PromiseConstructorLike
isPromiseLike()
function isPromiseLike(program, type): boolean
Defined in: builtinSymbolLikes.ts:14
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
class DerivedClass extends Promise<number> {}
DerivedClass.reject
// ^ PromiseLike
isReadonlyErrorLike()
function isReadonlyErrorLike(program, type): boolean
Defined in: builtinSymbolLikes.ts:52
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
type T = Readonly<Error>
// ^ ReadonlyErrorLike
isReadonlyTypeLike()
function isReadonlyTypeLike(
program,
type,
predicate?): boolean
Defined in: builtinSymbolLikes.ts:72
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
predicate ? | (subType ) => boolean |
Returns
boolean
Example
type T = Readonly<{ foo: 'bar' }>
// ^ ReadonlyTypeLike
isSymbolFromDefaultLibrary()
function isSymbolFromDefaultLibrary(program, symbol): boolean
Defined in: isSymbolFromDefaultLibrary.ts:3
Parameters
Parameter | Type |
---|---|
program | Program |
symbol | undefined | Symbol |
Returns
boolean
isTypeAnyArrayType()
function isTypeAnyArrayType(type, checker): boolean
Defined in: predicates.ts:88
Parameters
Parameter | Type |
---|---|
type | Type |
checker | TypeChecker |
Returns
boolean
true if the type is any[]
isTypeAnyType()
function isTypeAnyType(type): boolean
Defined in: predicates.ts:75
Parameters
Parameter | Type |
---|---|
type | Type |
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
Parameter | Type |
---|---|
type | Type |
checker | TypeChecker |
Returns
boolean
isTypeBigIntLiteralType()
function isTypeBigIntLiteralType(type): type is BigIntLiteralType
Defined in: predicates.ts:182
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is BigIntLiteralType
isTypeFlagSet()
function isTypeFlagSet(
type,
flagsToCheck,
isReceiver?): boolean
Defined in: typeFlagUtils.ts:27
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.
isTypeNeverType()
function isTypeNeverType(type): boolean
Defined in: predicates.ts:43
Parameters
Parameter | Type |
---|---|
type | Type |
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
Parameter | Type | Default value |
---|---|---|
program | Program | undefined |
type | Type | undefined |
options | ReadonlynessOptions | readonlynessOptionsDefaults |
Returns
boolean
isTypeReferenceType()
function isTypeReferenceType(type): type is TypeReference
Defined in: predicates.ts:64
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is TypeReference
isTypeTemplateLiteralType()
function isTypeTemplateLiteralType(type): type is TemplateLiteralType
Defined in: predicates.ts:188
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is TemplateLiteralType
isTypeUnknownArrayType()
function isTypeUnknownArrayType(type, checker): boolean
Defined in: predicates.ts:101
Parameters
Parameter | Type |
---|---|
type | Type |
checker | TypeChecker |
Returns
boolean
true if the type is unknown[]
isTypeUnknownType()
function isTypeUnknownType(type): boolean
Defined in: predicates.ts:50
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
boolean
true if the type is unknown
isUnsafeAssignment()
function isUnsafeAssignment(
type,
receiver,
checker,
senderNode):
| {
receiver: ts.Type;
sender: ts.Type;
}
| false
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
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
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
Parameter | Type | Default value |
---|---|---|
name | string | undefined |
target | ScriptTarget | ts.ScriptTarget.ESNext |
Returns
boolean
typeIsOrHasBaseType()
function typeIsOrHasBaseType(type, parentType): boolean
Defined in: predicates.ts:156
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.
typeMatchesSomeSpecifier()
function typeMatchesSomeSpecifier(
type,
specifiers,
program): boolean
Defined in: TypeOrValueSpecifier.ts:199
Parameters
Parameter | Type | Default value |
---|---|---|
type | Type | undefined |
specifiers | TypeOrValueSpecifier [] | [] |
program | Program | undefined |
Returns
boolean
typeMatchesSpecifier()
function typeMatchesSpecifier(
type,
specifier,
program): boolean
Defined in: TypeOrValueSpecifier.ts:165
Parameters
Parameter | Type |
---|---|
type | Type |
specifier | TypeOrValueSpecifier |
program | Program |
Returns
boolean
Variables
readonlynessOptionsDefaults
const readonlynessOptionsDefaults: ReadonlynessOptions;
Defined in: isTypeReadonly.ts:40
readonlynessOptionsSchema
const readonlynessOptionsSchema: object;
Defined in: isTypeReadonly.ts:29
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 | [{ 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.treatMethodsAsReadonly | object | - | 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
Name | Type | Default value | Defined in |
---|---|---|---|
items | object | - | 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: predicates.ts:111
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
Defined in: TypeOrValueSpecifier.ts:15
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
Defined in: TypeOrValueSpecifier.ts:33
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
Defined in: TypeOrValueSpecifier.ts:46
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
Defined in: isTypeReadonly.ts:24
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;
Defined in: TypeOrValueSpecifier.ts:64
A centralized format for rule options to describe specific types and/or values. See TypeOrValueSpecifier.