# 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
Last Updated: 4/30/2021, 5:04:43 AM