TypeScript
Marko provides built-in TypeScript support for template inputs, reactive variables, native elements, and custom tags, with checking in editors and the CLI.
Enabling TypeScript in your Marko project
For applications, place a tsconfig.json at the project root:
src/
package.json
tsconfig.jsonFor tag libraries, publish the output of @marko/type-check, which emits type-stripped .marko files alongside .d.marko.ts declaration files so consumers get full autocomplete and type checking.
Codebases migrating incrementally between JavaScript and TypeScript can set "script-lang" to "ts" or "js" in a marko.json. Marko walks upward to the nearest marko.json defining script-lang, allowing per-directory opt-in or opt-out.
Typing input
Exporting Input (export interface Input or export type Input) types the template's input object:
<label>
Price in ${input.currency}:${" "}
<input type="number" value=input.amount min=0 step=0.01>
</label>export interface Input {
currency: string;
amount: number;
}
<label>
Price in ${input.currency}:
<input type="number" value=input.amount min=0 step=0.01>
</label>label
-- Price in ${input.currency}:${" "}
input type="number" value=input.amount min=0 step=0.01export interface Input {
currency: string;
amount: number;
}
label
-- Price in ${input.currency}:
input type="number" value=input.amount min=0 step=0.01Because Input is a standard TypeScript export, other .marko and .ts files can import and extend it:
import type { Input as PriceInput } from "<price-field>";
import type { ExtraTypes } from "./utils.ts";
export type Input = PriceInput & ExtraTypes;import type { Input as PriceInput } from "<price-field>";
import type { ExtraTypes } from "./utils.ts";
export type Input = PriceInput & ExtraTypes;import type { Input as PriceInput } from "<price-field>";
export interface Input extends PriceInput {
discounted: boolean;
expiresAt: Date;
}import type { Input as PriceInput } from "<price-field>";
export interface Input extends PriceInput {
discounted: boolean;
expiresAt: Date;
}Generic Input
Type parameters declared on Input are in scope throughout the template body (excluding module-level static statements):
static function staticFn() {
// ❌ `T` is not in scope in module-level statements
}
<const/instanceFn(val) {
// ✅ `T` is in scope in the template body
}>
<select onInput(_evt, el) {
input.onSelect(input.options[+el.value]);
}>
<for|value, i| of=input.options>
<option value=i>${value}</option>
</for>
</select>export interface Input<T> {
options: T[];
onSelect: (newVal: T) => unknown;
}
static function staticFn() {
// ❌ `T` is not in scope in module-level statements
}
<const/instanceFn(val: T) {
// ✅ `T` is in scope in the template body
}>
<select onInput(_evt, el) { input.onSelect(input.options[+el.value]) }>
<for|value, i| of=input.options>
<option value=i>${value}</option>
</for>
</select>static function staticFn() {
// ❌ `T` is not in scope in module-level statements
}
const/instanceFn(val) {
// ✅ `T` is in scope in the template body
}
select onInput(_evt, el) {
input.onSelect(input.options[+el.value]);
}
for|value, i| of=input.options
option value=i -- ${value}export interface Input<T> {
options: T[];
onSelect: (newVal: T) => unknown;
}
static function staticFn() {
// ❌ `T` is not in scope in module-level statements
}
const/instanceFn(val: T) {
// ✅ `T` is in scope in the template body
}
select onInput(_evt, el) { input.onSelect(input.options[+el.value]) }
for|value, i| of=input.options
option value=i -- ${value}Tag Documentation
A JSDoc comment above export interface Input (or the @typedef Input comment in a JavaScript template) documents the tag itself. Editors display it on hover at every call site, including JSDoc tags such as @deprecated and @see:
/**
* Shows a rating out of five as filled stars.
* @see https://example.com/design/rating
*/
<span role="img" aria-label=`${input.value} out of 5`>
${"★".repeat(input.value)}${"☆".repeat(5 - input.value)}
</span>/**
* Shows a rating out of five as filled stars.
* @see https://example.com/design/rating
*/
export interface Input {
/** The rating, from 0 to 5. */
value: number;
}
<span role="img" aria-label=`${input.value} out of 5`>
${"★".repeat(input.value)}${"☆".repeat(5 - input.value)}
</span>/**
* Shows a rating out of five as filled stars.
* @see https://example.com/design/rating
*/
span role="img" aria-label=`${input.value} out of 5` --
${"★".repeat(input.value)}${"☆".repeat(5 - input.value)}/**
* Shows a rating out of five as filled stars.
* @see https://example.com/design/rating
*/
export interface Input {
/** The rating, from 0 to 5. */
value: number;
}
span role="img" aria-label=`${input.value} out of 5` --
${"★".repeat(input.value)}${"☆".repeat(5 - input.value)}Built-in Marko Types
Marko exposes its type definitions under the global Marko namespace:
| Type | Description |
|---|---|
Marko.Template<Input, Return> | Default export of a .marko file (typeof import("./template.marko").default) |
Marko.TemplateOf<Signature> | Generic template type emitted by mtc for parameterized templates |
Marko.TemplateInput<Input> | Object accepted by render and mount, combining Input with $global |
Marko.Body<Params, Return> | Tag content accepting parameter tuple Params and <return> attributes Return |
Marko.Renderable | Any value accepted by <${dynamic}/>: string | Marko.Template | Marko.Body<any, any> | { content: ... } |
Marko.Global | Type of $global, extensible via module augmentation |
Marko.RenderedTemplate | Return type of Template.render |
Marko.MountedTemplate<Input, Ret> | Return type of Template.mount |
Marko.NativeTags | Map of all native tags to Marko.NativeTag entries (Marko.NativeTags["div"]["input"]) |
Marko.NativeTag<Input, Return> | Single entry in Marko.NativeTags: Input types attributes, Return types the element ref |
Marko.HTMLAttributes<T> | Global attributes and events shared by HTML elements (T types handler target, defaulting to Element) |
Marko.SVGAttributes<T> | Global attributes and events shared by SVG elements (T types handler target, defaulting to Element) |
Marko.Input<Tag> / Marko.Return<Tag> | Extract input or return types from a native tag name, custom tag, or Marko.Body |
Marko.BodyParameters<Body> / Marko.BodyReturnType<Body> | Extract parameter tuple or return type from a Marko.Body |
Marko.AttrTag<T> | Attribute tag type combining the first instance with [Symbol.iterator] for repeated tags |
Class API Types
Marko 5 Class API types (Marko.Component, Marko.Out, Marko.Emitter) have been removed; they remain available from marko@5 when using Marko 5 interop.
Typing content
Use Marko.Body to type input.content:
export interface Input {
content?: Marko.Body;
}export interface Input {
content?: Marko.Body;
}<child/>
<child>Text in body</child>
<child>
<div>Any combination of tags</div>
</child>child
child -- Text in body
child
div -- Any combination of tagsPassing a non-Body value (such as a component reference directly to content=) fails type checking.
Typing Tag Parameters
Pass a tuple of parameter types as the first type argument to Marko.Body:
<for|i| from=0 to=input.to step=2>
<${input.content}(i)/>
</for>export interface Input {
to: number;
content: Marko.Body<[number]>;
}
<for|i| from=0 to=input.to step=2>
<${input.content}(i)/>
</for>for|i| from=0 to=input.to step=2
${input.content}(i)export interface Input {
to: number;
content: Marko.Body<[number]>;
}
for|i| from=0 to=input.to step=2
${input.content}(i)<for-by-two|i| to=10>
<div>${i}</div>
</for-by-two>for-by-two|i| to=10
div -- ${i}Custom Elements
First register the custom element as a native tag in marko.json, then augment Marko.NativeTags:
{
"<x-datepicker>": { "html": true }
}export class DatepickerElement extends HTMLElement {
static observedAttributes = ["value"];
get value() {
return this.getAttribute("value") ?? "";
}
}
interface DatepickerAttributes
extends Omit<Marko.HTMLAttributes<DatepickerElement>, "onChange"> {
value?: string;
onChange?: (e: CustomEvent<string>, target: DatepickerElement) => void;
onDateSelect?: (e: CustomEvent<string>, target: DatepickerElement) => void;
}
declare global {
namespace Marko {
interface NativeTags {
"x-datepicker": Marko.NativeTag<DatepickerAttributes, DatepickerElement>;
}
}
}<let/picked="2026-01-01">
<x-datepicker/picker value=picked onChange(evt) { picked = evt.detail }/>
<button onClick() { picker().focus() }>Focus</button>let/picked="2026-01-01"
x-datepicker/picker value=picked onChange(evt) { picked = evt.detail }
button onClick() { picker().focus() } -- FocusExtending Marko.HTMLAttributes<T> inherits global HTML attributes and events with T as the handler's second parameter. Declare CustomEvent<T> handlers directly on the attributes interface; when a custom event name collides with a standard DOM event (such as onChange), Omit that key from Marko.HTMLAttributes<T> first.
Marko writes attributes on registered custom elements via setAttribute, never as JavaScript properties. Declare observedAttributes on the custom element class to observe attribute updates, or assign properties through an element reference inside <script>.
Global HTML Attributes
Augment Marko.HTMLAttributes (or Marko.SVGAttributes for SVG) to add non-standard global attributes:
declare global {
namespace Marko {
interface HTMLAttributes {
"my-non-standard-attribute"?: string;
}
}
}CSS Properties
The style= object uses Marko.CSS.Properties (extending csstype's PropertiesHyphen). Augment it to type CSS custom properties:
declare global {
namespace Marko {
namespace CSS {
interface Properties {
"--accent"?: string;
}
}
}
}Typing $global
Marko.Global includes an index signature so undeclared properties read as unknown. Augment Marko.Global in a declaration file (using a leading export {} to treat the file as a module) to type application-wide $global properties:
export {};
declare global {
namespace Marko {
interface Global {
locale?: string;
requestId?: string;
}
}
}Properties declared without ? become required on every $global passed to render or mount.
TypeScript Syntax in .marko
Any JavaScript expression inside a .marko file accepts TypeScript syntax:
<my-tag foo=1>${input.el.value}</my-tag><my-tag foo=1 as any>${(input.el as HTMLInputElement).value}</my-tag>my-tag foo=1 -- ${input.el.value}my-tag foo=1 as any -- ${(input.el as HTMLInputElement).value}Tag Type Parameters
Declare generic type parameters on tag parameters before the pipe list:
<child|value|>
<div>${value}</div>
</child><child <T>|value: T|>
<div>${value}</div>
</child>child|value|
div -- ${value}child <T>|value: T|
div -- ${value}Tag Type Arguments
Supply explicit type arguments on a tag when inference is insufficient, or when initializing a <let> to null under strictNullChecks:
export interface Input<T> {
value: T;
}export interface Input<T> {
value: T;
}<child value=1/>
<let/selected=null><child<number> value=1/>
<let<string | null>/selected=null>child value=1
let/selected=nullchild<number> value=1
let<string | null>/selected=nullMethod Type Parameters
Method shorthands accept TypeScript type parameters directly after the method name:
<child process(item) {
return item;
}/><child process<T>(item: T) { return item }/>child process(item) {
return item;
}child process<T>(item: T) { return item }Attribute Type Assertions
Narrow inferred attribute or <let> types with TypeScript's as operator:
<let/handler=null>
<some-component number=1 names=[]/><let/handler=null as (() => void) | null>
<some-component number=1 as const names=[] as string[]/>let/handler=null
some-component number=1 names=[]let/handler=null as (() => void) | null
some-component number=1 as const names=[] as string[]JSDoc Support
JavaScript projects can adopt type checking incrementally through JSDoc annotations.
Setup
Enable checking per file by adding // @ts-check at the top, or check all .marko and .js files project-wide with a jsconfig.json setting "checkJs": true (opting individual files out with // @ts-nocheck):
// @ts-check
/**
* @typedef {{
* firstName: string,
* lastName: string,
* }} Input
*/
<div>${input.firstName} ${input.lastName}</div>// @ts-check
/**
* @typedef {{
* firstName: string,
* lastName: string,
* }} Input
*/
div -- ${input.firstName} ${input.lastName}CI Type Checking
Run @marko/type-check via its mtc CLI to check .marko files in CI and emit declarations for libraries:
npx mtcProfiling Performance
Pass --generateTrace to mtc to profile slow type checks:
mtc --generateTrace TRACE_DIRContributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.