GType

GType is the foundation of the GObject system. Although it is rarely necessary to interact with a GType directly in GJS, there are some situations where you may need to pass a GType as a function argument or in a class definition.

GType Object

Every GObject class has a static $gtype property that gives a GType object for the given type. This is the proper way to find the GType given an object or a class. For a class, GObject.type_from_name('GtkLabel') would work too if you know the GType name, but only if you had previously constructed a Gtk.Label object.

const GObject = imports.gi.GObject;

let objectInstance = new GObject.Object();

// Both of these calls return the same GType object
// expected output: [object GType for 'GObject']
log(GObject.Object.$gtype);
log(objectInstance.constructor.$gtype);
1
2
3
4
5
6
7
8

instanceofopen in new window can be used to compare an object instance to a constructor object.

log(typeof objectInstance);
// expected output: object

log(objectInstance instanceof GObject.Object);
// expected output: true
1
2
3
4
5

GTypeName

The name property of a GType object gives the GType name as a string ('GObject' in the example above). This is the proper way to find the type name given an object or a class. By default, the GType name of a subclass in GJS will be the class name prefixed with Gjs_:

const GObject = imports.gi.GObject;

const MySubclass = GObject.registerClass({
}, class MySubclass extends GObject.Object {
});

let obj = new GObject.Object();
let myObject = new Subclass();

// expected output: 'GObject'
log(GObject.Object.$gtype.name);
log(obj.constructor.$gtype.name);

// expected output: 'Gjs_MySubclass'
log(MySubclass.$gtype.name);
log(myObject.constructor.$gtype.name);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

In most cases you will not need to specify your own name, unless you are creating a GtkBuilder template class:

<interface>
  <template class="MyBox" parent="GtkBox">
    <!-- Template Definition -->
  </template>
</interface>
1
2
3
4
5

To set the GType name, pass it as the value for the GTypeName property to GObject.registerClass():

const MyBox = GObject.registerClass({
    GTypeName: 'MyBox',
}, class MyBox extends Gtk.Box {
});

let box = new MyBox();

// expected output: 'MyBox'
log(MyBox.$gtype.name);
log(box.constructor.$gtype.name);
1
2
3
4
5
6
7
8
9
10

Type Constants

For convenience, GJS has predefined constants for a number of built-in types. Usually these will only be used when defining properties and signals. These are equivalent to the $gtype property of a given object:

if (GObject.Object.$gtype === GObject.TYPE_OBJECT)
    log('equivalent');

// All number types are based on JavaScript's Number
if (GObject.TYPE_INT === Number.$gtype &&
    GObject.TYPE_DOUBLE === Number.$gtype)
    log('equivalent');
1
2
3
4
5
6
7
ConstantGLibJavaScript
GObject.TYPE_BOOLEANgbooleanBoolean
GObject.TYPE_STRINGgchararrayString
GObject.TYPE_INTgintNumber
GObject.TYPE_UINTguintNumber
GObject.TYPE_LONGglongNumber
GObject.TYPE_ULONGgulongNumber
GObject.TYPE_INT64gint64Number
GObject.TYPE_UINT64guint64Number
GObject.TYPE_FLOATgfloatNumber
GObject.TYPE_DOUBLEgdoubleNumber
GObject.TYPE_ENUMGEnumNumber
GObject.TYPE_FLAGSGFlagsNumber
GObject.TYPE_OBJECTGObjectGObject.Object
GObject.TYPE_INTERFACEGInterfaceGObject.Interface
GObject.TYPE_BOXEDGBoxed
GObject.TYPE_POINTERgpointernothing
GObject.TYPE_PARAMGParamGObject.ParamSpec
GObject.TYPE_VARIANTGVariantGLib.Variant
GObject.TYPE_GTYPEGTypeGObject.Type
GObject.TYPE_JSOBJECTGBoxedObject

JavaScript Types

GObject.TYPE_JSOBJECT is a special GType in GJS, created so that JavaScript types that inherit from Objectopen in new window can be used with the GObject framework. This allows you to use them as propertiesopen in new window and in signal parametersopen in new window in your GObject subclasses, and in some cases pass it to functions that take a GType.

In particular, this means that you may use Object (ie. {}) and Array (ie. []) types, but also more complex types such as Date and Function. Note that GObject.TYPE_JSOBJECT is a boxed type (GObject.TYPE_BOXED), so it may not be used where a GObject (GObject.TYPE_OBJECT) is expected such as with Gio.ListModelopen in new window.

// A GObject subclass with JavaScript Object properties and signals
const Example = GObject.registerClass({
    Properties: {
        'example-property': GObject.ParamSpec.jsobject(
            'example-property',
            'Example Property',
            'A property that holds a JavaScript Object',
            GObject.ParamFlags.READWRITE,
            ''
        ),
    },
    Signals: {
        'example-signal': {
            param_types: [GObject.TYPE_JSOBJECT],
        },
    },
}, class Example extends GObject.Object {
    get example_property() {
        if (this._example_property === undefined)
            this._example_property = {};
            
        return this._example_property;
    }
    
    set example_property(obj) {
        if (this.example_property === obj)
            return;
            
        this._example_property = obj;
        this.notify('example-object');
    }
    
    emitExampleSignal(obj) {
        this.emit('example-signal', obj);
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Last Updated: 7/4/2022, 7:07:16 PM
Contributors: Andy Holmes, Romain