TypeScript Compiler API

NodeFactory.createIdentifier()

Creates an identifier node from text. It neither declares nor resolves a symbol, and the caller must supply a valid identifier for the intended syntax position. For collision-sensitive generated names consider the factory’s unique-name helpers; renaming an identifier textually is not a semantic rename. Baseline: the Node.js API of TypeScript 5.8.3. The examples are CommonJS tooling scripts, not browser or CodeRunner snippets. TypeScript must be installed locally.

Formal Syntax & Structure

typescript syntaxtypescript
factory.createIdentifier(text: string): ts.Identifier

Parameters & Attributes

NameTypeDescription
textstringIdentifier spelling.

Working Example

Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const count: number = 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const identifier = ts.factory.createIdentifier("total");assert(ts.isIdentifier(identifier));assert.equal(identifier.text, "total");console.log(printer.printNode(ts.EmitHint.IdentifierName, identifier, source));
// Run: node example.cjs// Expected stdout: total
TypeScript Reference