# TypeScript

> Create the TX Sheet Control spreadsheet editor in TypeScript with typed SpreadsheetOptions, write cells and a formula, save XLSX bytes and type backend jobs.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Getting Started > Spreadsheet Editor > TypeScript
- **1675 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/typescript/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/typescript/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/typescript/llms-full.txt

---

TypeScript applications use the same editor npm package as JavaScript applications, `@txtextcontrol/tx-sheet-control`. It includes declarations for the browser, workbook, dashboard and font APIs and for import and export, so the compiler checks how you call the editor.

Read on to create the editor in a module with typed options, to tell the exports that exist at run time from the types and to use the server package from backend TypeScript.

### Install the Package

Install the same package as JavaScript users, as described in [Install the npm Packages](https://docs.textcontrol.com/tx-sheet-control/installation/npm/llms-full.txt). The type declarations do not copy assets into your application's public folder: deploy the font and language assets; editor styles are bundled and installed inside Shadow DOM as described in [JavaScript](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/javascript/llms-full.txt).

### Write the Code

The following module creates the editor in the element with the ID `sheet`, fills a column with three revenue values and saves the workbook as XLSX:

```typescript
import { Spreadsheet, StreamType, type SpreadsheetOptions } from '@txtextcontrol/tx-sheet-control';

const host = document.querySelector<HTMLElement>('#sheet');

if (!host) throw new Error('The spreadsheet host is missing.');

const options: SpreadsheetOptions = { license: 'YOUR_SIGNED_LICENSE', locale: 'en-US', height: 640, fontCatalogURL: '/assets/sheet/fonts/catalog.json' };
const editor = new Spreadsheet(host, options);
await editor.ready;

const sheet = editor.workbook.sheets.at(0);
sheet.cell('A1').setText('Quarterly revenue');
sheet.range('B2:B4').setValues([[12500], [18200], [15400]]);
sheet.cell('B5').setFormula('=SUM(B2:B4)');

const bytes: Uint8Array = await editor.save(StreamType.SpreadsheetML);
```

The code works as follows:

- `document.querySelector<HTMLElement>()` finds the host element, and the code throws an error when it is missing.
- `options` has the type [SpreadsheetOptions](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheetoptions/llms-full.txt), so the compiler validates its keys: [locale](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheetoptions/locale-property/llms-full.txt) is `en-US`, [height](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheetoptions/height-property/llms-full.txt) is 640 and [fontCatalogURL](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheetoptions/fontcatalogurl-property/llms-full.txt) is the URL of the deployed font catalog. `SpreadsheetOptions` is an interface, and the import marks it with `type`.
- `new Spreadsheet(host, options)` creates the editor, a [Spreadsheet](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheet/llms-full.txt), and `await editor.ready` waits until [ready](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheet/ready-property/llms-full.txt) resolves.
- [workbook](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheet/workbook-property/llms-full.txt), [sheets](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/workbook/sheets-property/llms-full.txt) and [at](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/sheetcollection/at-method/llms-full.txt) return the first worksheet.
- [cell](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/worksheet/cell-method/llms-full.txt) and [setText](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/cell/settext-method/llms-full.txt) write the literal text `Quarterly revenue` into A1. Literal text keeps leading zeros and leading equals signs.
- [range](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/worksheet/range-method/llms-full.txt) and [setValues](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/range/setvalues-method/llms-full.txt) write the three values into B2:B4, one value per row.
- [setFormula](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/cell/setformula-method/llms-full.txt) puts the formula `=SUM(B2:B4)` into B5.
- [save](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/spreadsheet/save-method/llms-full.txt) with `StreamType.SpreadsheetML`, a value of [StreamType](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/streamtype/llms-full.txt), returns the workbook as XLSX bytes in a `Uint8Array`.

### How Runtime Values and Types Differ

Not every name that the package exports exists at run time:

- **Runtime exports.** `Spreadsheet`, [Workbook](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/workbook/llms-full.txt), `StreamType` and the collection classes are runtime exports.
- **Interfaces.** Interfaces such as `SpreadsheetOptions`, [DashboardWidget](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/dashboardwidget/llms-full.txt) and [Format](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/format/llms-full.txt) describe API values. Import interfaces with `import type` when you use verbatim module syntax. Do not call an interface as a constructor.
- **Handles.** Obtain worksheet, table, chart and range handles from their owning collections, the way the code above gets the worksheet from `sheets`.

The [TypeScript API](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/llms-full.txt) reference includes the types and the runtime APIs. The [JavaScript API](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/llms-full.txt) reference focuses on the callable runtime exports.

### Use TypeScript in a Backend

Backend jobs run without the editor. The rules for backend TypeScript:

- **Package.** Import the backend controls from `@txtextcontrol/tx-sheet-control-server` and use that package's declarations. The public editor package is not required.
- **Module settings.** Choose module settings compatible with your Node.js application, such as NodeNext for an ES module application.
- **Compiler checks.** Let the compiler validate option keys, string unions, event names and return types.
- **Save options.** Use the `SaveOptions` of the package that performs the save: [SaveOptions](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/saveoptions/llms-full.txt) of the editor package in the browser, [SaveOptions](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/nodejs/saveoptions/llms-full.txt) of the server package in a backend job.
- **Browser-only parts.** Browser host elements and [EditorUi](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/editorui/llms-full.txt), the editor-only state and commands, do not belong in backend jobs.

[Node.js](https://docs.textcontrol.com/tx-sheet-control/getting-started/document-processing/nodejs/llms-full.txt) shows a first backend job with the server package.

### Next Steps

- [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt): react to workbook changes and file operations, and cancel them.
- [Lifetime and Concurrency](https://docs.textcontrol.com/tx-sheet-control/document-processing/lifetime/llms-full.txt): find out who owns a document and when its handles become invalid.
- [Node.js](https://docs.textcontrol.com/tx-sheet-control/getting-started/document-processing/nodejs/llms-full.txt): load, change and save a workbook with the server package.
- [Static Assets and Offline Hosting](https://docs.textcontrol.com/tx-sheet-control/deployment/static-assets/llms-full.txt): deploy font and language assets, which the declarations do not copy. Editor styles are bundled and installed inside Shadow DOM.

Before running the example, replace `YOUR_SIGNED_LICENSE` with the contents of your license file, `tx-sheet-control.license`, which the Text Control Account issues for your Trial, Developer or OEM serial number.

---

## Nearby

- **Previous:** [JavaScript](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/javascript/llms-full.txt)
- **Next:** [ASP.NET Core](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/aspnet-core/llms-full.txt)
