TypeScript Compiler API

ts.getPositionOfLineAndCharacter()

Converts valid zero-based line and UTF-16 character coordinates into a source offset. Use coordinates from the same source snapshot; validate editor inputs before calling. This is not a conversion from rendered grapheme or tab-expanded visual columns. Baseline: TypeScript 5.8.3 and Node.js. The example is a local tooling script in a disposable directory; it writes temporary project files, not a ByteMind CodeRunner exercise.

Formal Syntax & Structure

typescript syntaxtypescript
ts.getPositionOfLineAndCharacter(sourceFile: ts.SourceFileLike, line: number, character: number): number

Parameters & Attributes

NameTypeDescription
sourceFilets.SourceFileLikeSource text and line map.
linenumberZero-based line.
characternumberZero-based UTF-16 character within the line.

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 fs = require("node:fs");const path = require("node:path");const text="// 😀\r\nconst x = 1;\n";const file=ts.createSourceFile("input.ts",text,ts.ScriptTarget.ES2022,true);const offset=ts.getPositionOfLineAndCharacter(file,1,6); assert.equal(text[offset],"x");assert.equal(offset,13); assert.deepEqual(ts.getLineAndCharacterOfPosition(file,offset),{line:1,character:6});console.log("line 1, character 6 -> offset 13");
// Run: node example.cjs// Expected stdout: line 1, character 6 -> offset 13
TypeScript Reference