Skip to main content

Overview

The Scanner performs lexical analysis (tokenization) of TypeScript source code. It breaks the source text into a stream of tokens that can be used by the parser or for direct analysis.

Creating a Scanner

ts.createScanner()

Creates a new Scanner instance.
languageVersion
ScriptTarget
required
The ECMAScript target version (ES5, ES2015, ES2020, etc.)
skipTrivia
boolean
required
Whether to skip whitespace and comments
languageVariant
LanguageVariant
Standard or JSX variant (defaults to Standard)
textInitial
string
Initial text to scan
onError
ErrorCallback
Error callback function
start
number
Starting position in the text
length
number
Length of text to scan
return
Scanner
A Scanner instance

Example

Scanner Interface Methods

Scanning Methods

scan()

Scans the next token from the input.
return
SyntaxKind
The syntax kind of the scanned token

getText()

Returns the full text being scanned.
return
string
The full source text

setText()

Sets new text for the scanner to scan.
text
string | undefined
required
The text to scan
start
number
Starting position (defaults to 0)
length
number
Length to scan (defaults to entire text)

Token Information Methods

getToken()

Returns the current token’s syntax kind.
return
SyntaxKind
The current token’s syntax kind

getTokenText()

Returns the text of the current token.
return
string
The token’s text

getTokenValue()

Returns the processed value of the current token (for strings and numbers).
return
string
The token’s processed value

getTokenStart()

Returns the starting position of the current token (excluding leading trivia).
return
number
The token’s start position

getTokenEnd()

Returns the ending position of the current token.
return
number
The token’s end position

getTokenFullStart()

Returns the starting position of the current token (including leading trivia).
return
number
The token’s full start position

Token State Methods

isIdentifier()

Returns true if the current token is an identifier.
return
boolean
True if the token is an identifier

isReservedWord()

Returns true if the current token is a reserved keyword.
return
boolean
True if the token is a reserved word

isUnterminated()

Returns true if the current token is unterminated (e.g., unterminated string).
return
boolean
True if the token is unterminated

hasPrecedingLineBreak()

Returns true if there’s a line break before the current token.
return
boolean
True if there’s a preceding line break

hasUnicodeEscape()

Returns true if the current identifier contains a Unicode escape sequence.
return
boolean
True if the token has Unicode escapes

hasExtendedUnicodeEscape()

Returns true if the current identifier contains an extended Unicode escape sequence.
return
boolean
True if the token has extended Unicode escapes

Advanced Scanning Methods

reScanGreaterToken()

Re-scans a greater-than token in JSX or type contexts.
return
SyntaxKind
The re-scanned token kind

reScanSlashToken()

Re-scans a slash token (could be division or regex).
return
SyntaxKind
The re-scanned token kind (SlashToken or RegularExpressionLiteral)

reScanTemplateToken()

Re-scans template literal tokens.
isTaggedTemplate
boolean
required
Whether this is a tagged template literal
return
SyntaxKind
The re-scanned template token kind

scanJsxIdentifier()

Scans a JSX identifier (allows hyphens).
return
SyntaxKind
The JSX identifier token

scanJsxToken()

Scans the next token in JSX mode.
return
JsxTokenSyntaxKind
The JSX token kind

scanJsxAttributeValue()

Scans a JSX attribute value.
return
SyntaxKind
The attribute value token

State Management Methods

resetTokenState()

Resets the scanner to a specific position.
pos
number
required
The position to reset to

lookAhead()

Invokes a callback while saving/restoring scanner state.
callback
() => T
required
Function to call with lookahead
return
T
The result of the callback

tryScan()

Tries a scan operation, only committing if the callback returns truthy.
callback
() => T
required
Function to try
return
T
The result of the callback

scanRange()

Scans a specific range of text.
start
number
required
Start position
length
number
required
Length to scan
callback
() => T
required
Function to call while scanning the range
return
T
The result of the callback

Configuration Methods

setScriptTarget()

Sets the ECMAScript target version.
scriptTarget
ScriptTarget
required
The target version

setLanguageVariant()

Sets the language variant (Standard or JSX).
variant
LanguageVariant
required
The language variant

setScriptKind()

Sets the script kind (TS, JS, JSX, etc.).
scriptKind
ScriptKind
required
The script kind

setOnError()

Sets the error callback function.
onError
ErrorCallback | undefined
required
The error callback

Complete Example

Syntax Kinds

Common token types (SyntaxKind enum):

See Also