@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
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).
Source
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
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
Parameter | Type |
---|---|
checker | TypeChecker |
node | Expression |
Returns
ts.Type
| undefined
Source
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
Source
getSourceFileOfNode()
function getSourceFileOfNode(node): ts.SourceFile
Gets the source file for a given node
Parameters
Parameter | Type |
---|---|
node | Node |
Returns
ts.SourceFile
Source
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
Source
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
Source
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
Source
getTypeOfPropertyOfType()
function getTypeOfPropertyOfType(
checker,
type,
property): ts.Type | undefined
Parameters
Parameter | Type |
---|---|
checker | TypeChecker |
type | Type |
property | Symbol |
Returns
ts.Type
| undefined
Source
isAnyOrAnyArrayTypeDiscriminated()
function isAnyOrAnyArrayTypeDiscriminated(node, checker): AnyType
Parameters
Parameter | Type |
---|---|
node | Node |
checker | TypeChecker |
Returns
AnyType.Any
if the type is any
, AnyType.AnyArray
if the type is any[]
or readonly any[]
,
otherwise it returns AnyType.Safe
.
Source
isBuiltinSymbolLike()
function isBuiltinSymbolLike(
program,
type,
symbolName): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
symbolName | string | string [] |
Returns
boolean
Source
isBuiltinSymbolLikeRecurser()
function isBuiltinSymbolLikeRecurser(
program,
type,
predicate): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
predicate | (subType ) => null | boolean |
Returns
boolean
Source
isBuiltinTypeAliasLike()
function isBuiltinTypeAliasLike(
program,
type,
predicate): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
predicate | (subType ) => boolean |
Returns
boolean
Source
isErrorLike()
function isErrorLike(program, type): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
class Foo extends Error {}
new Foo()
// ^ ErrorLike
Source
isNullableType()
function isNullableType(type): boolean
Checks if the given type is (or accepts) nullable
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
boolean
Source
isPromiseConstructorLike()
function isPromiseConstructorLike(program, type): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
const value = Promise
value.reject
// ^ PromiseConstructorLike
Source
isPromiseLike()
function isPromiseLike(program, type): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
class DerivedClass extends Promise<number> {}
DerivedClass.reject
// ^ PromiseLike
Source
isReadonlyErrorLike()
function isReadonlyErrorLike(program, type): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
type | Type |
Returns
boolean
Example
type T = Readonly<Error>
// ^ ReadonlyErrorLike
Source
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
Source
isSymbolFromDefaultLibrary()
function isSymbolFromDefaultLibrary(program, symbol): boolean
Parameters
Parameter | Type |
---|---|
program | Program |
symbol | undefined | Symbol |
Returns
boolean
Source
isSymbolFromDefaultLibrary.ts:3
isTypeAnyArrayType()
function isTypeAnyArrayType(type, checker): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
checker | TypeChecker |
Returns
boolean
true if the type is any[]
Source
isTypeAnyType()
function isTypeAnyType(type): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
boolean
true if the type is any
Source
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
Source
isTypeBigIntLiteralType()
function isTypeBigIntLiteralType(type): type is BigIntLiteralType
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is BigIntLiteralType
Source
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.
Source
isTypeNeverType()
function isTypeNeverType(type): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
boolean
true if the type is never
Source
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
Source
isTypeReferenceType()
function isTypeReferenceType(type): type is TypeReference
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is TypeReference
Source
isTypeTemplateLiteralType()
function isTypeTemplateLiteralType(type): type is TemplateLiteralType
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
type is TemplateLiteralType
Source
isTypeUnknownArrayType()
function isTypeUnknownArrayType(type, checker): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
checker | TypeChecker |
Returns
boolean
true if the type is unknown[]
Source
isTypeUnknownType()
function isTypeUnknownType(type): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
boolean
true if the type is unknown
Source
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
Parameter | Type |
---|---|
type | Type |
receiver | Type |
checker | TypeChecker |
senderNode | null | 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
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
Source
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.
Source
typeMatchesSpecifier()
function typeMatchesSpecifier(
type,
specifier,
program): boolean
Parameters
Parameter | Type |
---|---|
type | Type |
specifier | TypeOrValueSpecifier |
program | Program |
Returns
boolean
Source
Variables
readonlynessOptionsDefaults
const readonlynessOptionsDefaults: ReadonlynessOptions;
Source
readonlynessOptionsSchema
const readonlynessOptionsSchema: object;
Type declaration
Member | Type | Value |
---|---|---|
additionalProperties | false | false |
properties | object | ... |
properties.allow | object | ... |
properties.allow.items | JSONSchema4 | typeOrValueSpecifierSchema |
properties.allow.type | "array" | 'array' |
properties.treatMethodsAsReadonly | object | ... |
properties.treatMethodsAsReadonly.type | "boolean" | 'boolean' |
type | "object" | 'object' |
Source
typeOrValueSpecifierSchema
const typeOrValueSpecifierSchema: JSONSchema4;
Source
Enumerations
AnyType
Enumeration Members
Enumeration Member | Value |
---|---|
Any | 0 |
AnyArray | 1 |
Safe | 2 |
Interfaces
FileSpecifier
Properties
Property | Type |
---|---|
from | "file" |
name | string | string [] |
path? | string |
LibSpecifier
Properties
Property | Type |
---|---|
from | "lib" |
name | string | string [] |
PackageSpecifier
Properties
Property | Type |
---|---|
from | "package" |
name | string | string [] |
package | string |
ReadonlynessOptions
Properties
Property | Modifier | Type |
---|---|---|
allow? | readonly | TypeOrValueSpecifier [] |
treatMethodsAsReadonly? | readonly | boolean |
Type Aliases
TypeOrValueSpecifier
type TypeOrValueSpecifier: FileSpecifier | LibSpecifier | PackageSpecifier | string;