Re: Subclassing a GObject in Typescript



If you decide to export a TypeScript API, I think it would be better to set it up as it was intended for regular _javascript_. The current API in GJS is considered transitional, until we get class and property decorators and data properties in _javascript_ proper.

The API usage will likely look like something like this, when that happens:

@GObject.registerClass
class Spinner extends Gtk.Widget {
    static [GObject.interfaces] = [Gtk.Orientable, Gtk.Buildable]
    static [GObject.signals] = {
        'foobars-incremented': {},
    }

    _init(props = {}) {
        ...
    }

    @GObject.property({
        description: 'The number of foobars',
        type: GObject.TYPE_INT,
    })
    get foobars() {
        return 42;
    }
}

All of this should actually work already with the equivalent JS function calls and "static get" properties, although I'm not sure anyone ever tests it. (The one thing that won't work is the GObject.property() decorator which isn't possible to emulate in current JS.)
But if I'm not mistaken all these things already have equivalents in TypeScript, so it would be nice to have the TypeScript API look like the intended API in JS.

Regards,
Philip

On Fri, Nov 16, 2018 at 1:07 PM <makepost firemail cc> wrote:
A generic seems appropriate, here's how you can start:

```ts
export function registerClass<T>(klass: T): T & Partial<{
   [interfaces]: any[]
   [properties]: any
   [requires]: any[]
   [signals]: any
}>`
```

Best not leave it with `any` but specify what types these properties
should have.

_init should receive `${name}_ConstructProps` for proper checking, not
just any object. Look for the push of "default constructor", and add the
_init definition nearby, that should be enough.

On 2018-11-16 15:56, Tony Houghton wrote:
> I want to make ts-for-gjs work with registerClass(), but as a
> Typescript beginner I would appreciate some advice. I'm proposing to
> make it add these globals to GObject.d.ts:
>
> export function registerClass(klass: Object): Object
> export declare const interfaces: unique symbol
> export declare const properties: unique symbol
> export declare const requires: unique symbol
> export declare const signals: unique symbol
>
> Does that look OK? I'm not 100% sure that Object is the most
> appropriate type to use for registerClass' parameter and return value.
>
> GObject.Object needs this pseudo-constructor:
>
>     _init(params?:object): void
>
> but is the base class the only one that needs that, or is it
> overridden in all subclasses?
>
> When registerClass() is called, it adds the equivalent of these static
> members to the class being registered:
>
>     static [interfaces]?: any[]
>     static [properties]?: object
>     static [requires]?: any[]
>     static [signals]?: object
>
> Seeing as they're added dynamically to 3rd party classes, but aren't
> present in the introspected classes, I don't think we can actually get
> ts-for-gjs to do anything about them. Any ideas on how these could be
> made convenient to use in Typescript?
>
> --
>
> TH
> _______________________________________________
> _javascript_-list mailing list
> _javascript_-list gnome org
> https://mail.gnome.org/mailman/listinfo/_javascript_-list
_______________________________________________
_javascript_-list mailing list
_javascript_-list gnome org
https://mail.gnome.org/mailman/listinfo/_javascript_-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]