TypstDocumentation

dictionary

A map from string keys to values.

You can construct a dictionary by enclosing comma-separated key: value pairs in parentheses. The values do not have to be of the same type.

A dictionary is conceptually similar to an array, but it is indexed by strings instead of integers. You can access and create dictionary entries with the .at() method. If you know the key statically, you can alternatively use field access notation (.key) to access the value. Dictionaries can be added with the + operator and joined together.

You can iterate over the pairs in a dictionary using a for loop. Dictionaries are always ordered by key.

Since empty parentheses already yield an empty array, you have to use the special (:) syntax to create an empty dictionary.

Example

#let dict = (
  name: "Typst",
  born: 2019,
)

#dict.name \
#(dict.launch = 20)
#dict.len() \
#dict.keys() \
#dict.values() \
#dict.at("born") \
#dict.insert("city", "Berlin ")
Preview

Methods
Question mark

len method

The number of pairs in the dictionary.

value.len(
) -> integer

at method

Returns the value associated with the specified key in the dictionary. May be used on the left-hand side of an assignment if the key is already present in the dictionary. Fails with an error if the key is not part of the dictionary.

value.at() -> any

index
integerRequiredPositional
Question mark

The index at which to retrieve the element.

insert method

Insert a new pair into the dictionary and return the value.

value.insert(
string,any,
)

key
stringRequiredPositional
Question mark

The key of the pair that should be inserted.

value
anyRequiredPositional
Question mark

The value of the pair that should be inserted.

keys method

Returns the keys of the dictionary as an array in sorted order.

value.keys(
) -> array

values method

Returns the values of the dictionary as an array in key-order.

value.values(
) -> array

pairs method

Returns the keys and values of the dictionary as an array of pairs. Each pair is represented as an array of length two.

value.pairs(
) -> array

remove method

Remove a pair from the dictionary by key and return the value.

value.remove() -> any

key
stringRequiredPositional
Question mark

The key of the pair that should be removed.