TypeScript Compiler API
ts.EmitHint
An enum describing the syntactic role of a node during printing or emission. Use named enum members rather than persisting numeric values; the hint is not a compiler target or an instruction to transpile the expression. Select a role compatible with the node shape. 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
enum ts.EmitHint; ts.EmitHint.Unspecified; ts.EmitHint.Expression; ts.EmitHint.IdentifierNameWorking 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("score");const expression = ts.factory.createNumericLiteral(4);console.log(printer.printNode(ts.EmitHint.IdentifierName, identifier, source));console.log(printer.printNode(ts.EmitHint.Expression, expression, source));
// Run: node example.cjs// Expected stdout: score// 4