TypeScript decoder
The TypeScript package is a standalone reader for MapLibre Tiles (MLT). It turns tile bytes into feature tables with layer names, extents, IDs, geometries, and properties. Its public API also exposes the data types and small encoding support used by applications and format checks.
Package role
The package gives JavaScript and TypeScript users a direct path from an MLT tile to feature data. It is published as @maplibre/mlt, builds declaration files for typed use, and can be built and checked without the other language implementations. ts/package.json• L1-22
Tile decoding
decodeTile accepts tile bytes and returns one FeatureTable for each decoded layer. A feature table presents the layer name and extent together with feature IDs, geometry, and properties. Callers can use the table view or read individual features. ts/…/mltDecoder.ts• L37-49 ts/…/featureTable.ts• L16-25
The decoder reads each length-bounded tile block and uses the embedded schema to match streams to columns. It skips block tags that it does not know, but rejects a block whose declared size extends past the tile. This keeps the reader aligned while it moves through the tile. ts/…/mltDecoder.ts• L50-68
The feature table keeps geometry and column data in typed vectors until a caller asks for features. getFeatures() then joins geometry, ID, and property values by feature position. This gives callers both a direct feature view and access to the table-level data model. ts/…/featureTable.ts• L31-66 ts/…/featureTable.ts• L68-95
References: ts/…/mltDecoder.ts• L45-68 ts/…/featureTable.ts• L16-29 ts/…/featureTable.ts• L66-95
Decode flow
The decode flow moves from tile framing to schema discovery and then to feature data. Metadata controls how the decoder reads each stream. The feature table provides the stable output boundary for downstream code.
This diagram shows how tile bytes become feature data.
flowchart LR
A[MLT tile bytes] --> B[Length-bounded block]
B --> C[Embedded layer schema and extent]
C --> D[ID, geometry, and property streams]
D --> E[FeatureTable]
E --> F[Features or GeoJSON-shaped output]
When the tile contains geometry scaling information, optional scaling input lets the decoder map tile-local coordinates to the caller's requested range. Without it, the feature table exposes decoded tile coordinates and its declared extent. ts/…/mltDecoder.ts• L108-123 ts/…/geometryScaling.ts• L1-6
The decoder also handles IDs that do not fit safely in a JavaScript number. By default, it limits IDs to the safe integer range for convenient application use. Callers that need the full 64-bit value can disable that limit and receive wide integer values where needed. ts/…/mltDecoder.ts• L41-49 ts/…/mltDecoder.ts• L207-228
References: ts/…/mltDecoder.ts• L50-68 ts/…/mltDecoder.ts• L108-123 ts/…/mltDecoder.ts• L207-232
Metadata handling
MLT metadata is the contract between encoded streams and the values returned to the caller. The decoder reads one feature-table schema at a time. The schema includes the layer name, extent, columns, nullability, scalar or complex type, and nested fields. ts/…/embeddedTilesetMetadataDecoder.ts• L101-127 ts/…/tilesetMetadata.ts• L34-58
Embedded schema
The embedded schema gives the decoder the names and roles of the ID, geometry, and property columns. It supports scalar columns and nested complex fields such as structures and maps. A missing layer name or unknown type code is a schema error because the decoder cannot build a valid feature table. ts/…/embeddedTilesetMetadataDecoder.ts• L68-98 ts/…/embeddedTilesetMetadataDecoder.ts• L42-63
References: ts/…/embeddedTilesetMetadataDecoder.ts• L42-63 ts/…/embeddedTilesetMetadataDecoder.ts• L68-98 ts/…/tilesetMetadata.ts• L46-96
Stream description
Each column's stream metadata tells the decoder how many values to read, how many bytes they occupy, and how the values are represented. It can also describe run-length or Morton-ordered data. The decoder uses this information to recover the logical value count before it reads the stream body. ts/…/streamMetadataDecoder.ts• L11-35 ts/…/streamMetadataDecoder.ts• L84-98
References: ts/…/streamMetadataDecoder.ts• L11-35 ts/…/streamMetadataDecoder.ts• L84-138 ts/…/mltDecoder.ts• L76-149
Public interface
The package root is the supported entry point for consumers. It exposes tile decoding and the feature-table model. It also exposes selected geometry and type definitions so callers can work with decoded data without importing internal modules. ts/…/index.ts• L1-20
| Export | Purpose |
|---|---|
decodeTile | Decode MLT bytes into feature tables. |
FeatureTable | Read a layer's name, extent, features, IDs, geometry, and properties. |
GeometryVector, GpuVector, GeometryScaling, GEOMETRY_TYPE | Work with decoded geometry and optional coordinate scaling. |
TileSetMetadata, Geometry, Feature | Type decoded metadata and feature values. |
encodeTile and encode types | Create plain MLT tiles for round-trip and fixture checks. |
The package entry points target generated JavaScript and declaration files under dist. Source files under ts/src are the development surface. Consumers should use the package root rather than internal source paths. ts/package.json• L3-8 ts/tsconfig.json• L1-12
The package keeps its TypeScript toolchain separate from its runtime dependency. The manifest selects TypeScript 6 through the typescript package alias and also includes the native TypeScript 7 package. The lock file resolves TypeScript 6.0.3, native TypeScript 7.0.2, Biome 2.6.8, and their platform-specific packages. These files must stay aligned so a fresh install uses the same build and format tools as the package manifest. ts/package.json• L31-51 ts/package-lock.json• L7-32 ts/package-lock.json• L94-121 ts/package-lock.json• L1793-1826 ts/package-lock.json• L3644-3656
References: ts/…/index.ts• L1-20 ts/package.json• L3-8 ts/package.json• L16-22 ts/package.json• L31-51 ts/package-lock.json• L7-32 ts/package-lock.json• L94-121 ts/package-lock.json• L1793-1826 ts/package-lock.json• L3644-3656 ts/tsconfig.json• L1-12
Synthetic fixture validation
Synthetic fixtures provide small, focused MLT tiles with a matching JSON form. The TypeScript decoder reads each fixture, converts feature tables to a common GeoJSON-shaped representation, and compares the result with the expected logical output. This checks format behavior without tying the expected result to one encoder's byte layout. ts/…/synthetic.spec.ts• L19-38 test/…/README.md• L1-11
These fixtures are part of the wider format check described in cross-language validation and use the shared assets described in shared conformance assets.
The fixture helper discovers .mlt files and pairs each one with a same-name .json file. Active fixtures must match their expected output within the shared numeric tolerance. Excluded fixtures must remain unsupported. If an excluded fixture starts matching, the test fails until the exclusion is removed. test/…/index.ts• L11-37 test/…/index.ts• L48-85 test/…/index.ts• L90-104
The shared representation carries the layer name and extent in feature properties because GeoJSON has no layer-level fields for them. It also converts wide integers to JSON-safe numbers for fixture comparison. The current TypeScript test marks the 0x02 fixture family as unsupported. New support must change both the decoder result and that explicit exclusion. ts/…/featureTablesToGeoJson.ts• L6-34 ts/…/featureTablesToGeoJson.ts• L61-74 ts/…/synthetic.spec.ts• L12-17
For broader format checks, the decoder also compares MLT tiles with matching Mapbox Vector Tiles from simple, Amazon, OMT, and Bing data sets. These checks compare layer names, feature counts, IDs, geometry, and properties while allowing known numeric and representation differences. ts/…/mltDecoder.spec.ts• L14-36 ts/…/mltDecoder.spec.ts• L61-80 ts/…/mltDecoder.spec.ts• L91-121
References: ts/…/synthetic.spec.ts• L12-38 test/…/index.ts• L48-104 ts/…/featureTablesToGeoJson.ts• L6-34 ts/…/mltDecoder.spec.ts• L14-36
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to root exports must keep the generated package entry point and declaration surface aligned.
- Changes to embedded column types must stay aligned with stream metadata and feature-table column joins.
- Changes to the shared fixture shape must apply to both encoder round trips and decoder synthetic checks.
- Removing an unsupported synthetic family requires removing its exclusion and making its expected JSON pass.
- Changes to ID number handling must be checked against JavaScript safe integers and cross-language MVT comparisons.
- Changes to the TypeScript or Biome tool versions must keep ts/package.json and ts/package-lock.json in sync. This includes aliased and platform-specific packages.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A block declares a length beyond the tile bytes | Decoding stops with a block overrun error. | Validate tile framing before reading the block body. |
| Embedded metadata has no layer name or an unknown type code | The decoder cannot create a valid feature table. | Treat schema errors as unsupported or invalid input. |
| A synthetic fixture is added without its matching JSON file | Fixture discovery fails before the decoder result can be checked. | Add the .mlt and same-name .json pair together. |
| An excluded fixture starts matching its expected JSON | The unsupported test fails and shows that support is now available. | Remove the matching exclusion when support is complete. |
| A 64-bit ID is converted to an unsafe JavaScript number | The ID can lose precision in application output or comparisons. | Keep wide IDs as bigint when full precision matters. |
| The manifest and lock file specify different compiler or formatter versions | Fresh installs can use different package tooling from local development. | Update dependency declarations and lock entries together. |
Key files
| File | Why to read it |
|---|---|
| ts/…/index.ts | Defines the supported package exports. |
| ts/…/mltDecoder.ts | Shows the top-level tile-to-feature-table flow. |
| ts/…/featureTable.ts | Defines the decoded layer and feature view. |
| ts/…/embeddedTilesetMetadataDecoder.ts | Shows how layer and column metadata enter the decode flow. |
| ts/…/streamMetadataDecoder.ts | Defines the stream information used to read encoded values. |
| ts/…/synthetic.spec.ts | Shows the TypeScript decoder's synthetic fixture contract and current exclusions. |
| test/…/index.ts | Defines fixture discovery, numeric comparison, and unsupported-case checks. |
| ts/…/mltDecoder.spec.ts | Shows cross-checks against MVT fixtures and feature iteration. |
| test/…/README.md | Explains the shared purpose of synthetic MLT and JSON fixtures. |
| ts/package.json | Defines package entry points, scripts, runtime dependency, and tool declarations. |
| ts/package-lock.json | Locks the resolved compiler, formatter, and platform-specific tool packages. |