Skip to main content

Interfaces

Interfaces in OneVision Scripts define the static shape of an object by specifying the properties it must contain and their types. They provide compile-time type safety by ensuring that objects follow the required structure. Interfaces do not exist at runtime — they are purely a type-system construct used to validate code before execution.

In other words, an interface is like a blueprint of an object structure.

tip

It's recommended to use interfaces when creating objects.

Define interfaces

To define an interface, you must write the keyword interface followed by the interface name and the interface structure between {}. The structure must have the object properties with their type, separated by semicolons ;.

interface User{
name: string;
age: number;
}

Create objects using interfaces

To create an object using an interface, you must write the object name, followed by the interface name and then fill the properties between {}, separated by ,. The properties and their types must match the interface properties and types.

The object created is a dynamic object, its properties can be changed.

var user: User = {
name: "Pau",
age: 34
};

Access and modify object properties

You can access and modify the object properties writing the object name followed by a dot . and the property name.

// Accessing object properties
var name = user.name; // name = "Pau"
var age = user.age; // age = 34

// Modifying object properties
user.name = "Jordi";
user.age = 35;

Lists of objects

With interfaces you can define lists of dynamic objects enforcing certain structure.

interface User{
name: string;
age: number;
}

var users: User[] = [];
users.push({
name: "Pau",
age: 34
});
users.push({
name: "Jordi",
age: 35
});

var user1 = users[0].name; // user1 = Pau
var user2 = users[1].name; // user2 = Jordi

Nested interfaces

You can create nested interfaces:

interface User{
name: string;
age: number;
}

interface Project{
id: string;
users: User[];
}

var project = Project {
id: "123456",
users: []
}
project.users.push({
name: "Pau",
age: 34
});

var user1 = project.users[0].name; // user1 = "Pau"