Skip to content

Commit

Permalink
fix(#195): Enable instanceof checks by adding constructor to interfac…
Browse files Browse the repository at this point in the history
…e declaration
  • Loading branch information
JumpLink committed Nov 6, 2024
1 parent 84baa3a commit 6e9b416
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
42 changes: 23 additions & 19 deletions examples/gio-2-cat/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,37 @@
* the label should show a translation of 'Print help'
*/

import '@girs/gjs';
import '@girs/gjs/dom';
import GLib from '@girs/glib-2.0';
import Gio from '@girs/gio-2.0';
import '@girs/gjs'
import '@girs/gjs/dom'
import GLib from '@girs/glib-2.0'
import Gio from '@girs/gio-2.0'

const loop = GLib.MainLoop.new(null, false);
const loop = GLib.MainLoop.new(null, false)
const textDecoder = new TextDecoder()

function cat(filename: string) {
const file = Gio.file_new_for_path(filename);
const file = Gio.file_new_for_path(filename)

file.load_contents_async(null, (obj, res) => {
let contents: Uint8Array;
let contents: Uint8Array
try {
contents = obj!.load_contents_finish(res)![1];
contents = obj!.load_contents_finish(res)![1]
} catch (e) {
logError(e);
loop.quit();
return;
logError(e)
loop.quit()
return
}
print(textDecoder.decode(contents));
loop.quit();
});
print(textDecoder.decode(contents))
loop.quit()
})

// Tests instanceof, see https://github.com/gjsify/ts-for-gir/issues/195
if (!(file instanceof Gio.File)) {
throw new Error('file is not an instance of Gio.File')
}

loop.run();
loop.run()
}

if (ARGV.length !== 1)
printerr('Usage: gio-cat.js filename');
else
cat(ARGV[0]);
if (ARGV.length !== 1) printerr('Usage: gio-cat.js filename')
else cat(ARGV[0])
6 changes: 5 additions & 1 deletion packages/generator-typescript/src/module-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ export class ModuleGenerator extends FormatGenerator<string[]> {
]
}
generateInterfaceDeclaration(node: IntrospectedInterface): string[] {
return [`\n\nexport const ${node.name}: ${node.name}Namespace;\n`]
return [
`\n\nexport const ${node.name}: ${node.name}Namespace & {
new (): ${node.name} // This allows \`obj instanceof ${node.name}\`
}\n`,
]
}
generateError(node: IntrospectedError): string[] {
const { namespace } = this
Expand Down

0 comments on commit 6e9b416

Please sign in to comment.