-
Sorry for the trivial question but I am confused on how to get the simple example setup and working. Even without typescript the syntax provided does not seem to work. I have tried both gjs and node and neither work. I am not particularly familiar with gjs so I must be doing something wrong. I have tried running the examples by running the specified yarn commands in the example directory and I have errors since it depends on some sort of yarn workspace configuration. Is there a quickstart repo that would be a good reference somewhere? Sorry if this is missing obvious baseline things |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
@C-Loftus you need to run the example with the Use the types in your JavaScript projectHowever, you can still use the types in the way you are trying to without needing a bundler or the TypeScript compiler. To do this, you need to create a jsconfig.json (note that this is not a tsconfig.json but a jsconfig.json) and add the {
"compilerOptions": {
"checkJs": true,
"types": ["@girs/gjs", "@girs/gjs/dom", "@girs/gio-2.0", "@girs/gobject-2.0", "@girs/gtk-4.0"],
}
} These types also contain ambient module definitions, which means there are also types for the GJS typical import statements like Therefore you can import the modules as described in the GJS documentation, like this: import Gtk from 'gi://Gtk?version=4.0';
const button = new Gtk.Button(); You should now have autocompletion and type checking in your JavaScript project, provided your IDE supports this. Here is an external blog post about this topic: https://www.dandoescode.com/blog/using-typescript-without-typescript |
Beta Was this translation helpful? Give feedback.
-
Play with the examples@C-Loftus To also answer your other comment about # Install yarn, see https://yarnpkg.com/getting-started/install
npm install -g yarn
# Check out the repo including submodules and go into this project
git clone --recurse-submodules https://github.com/gjsify/ts-for-gir.git
cd ./ts-for-gir
# Install dependencies and initialise the yarn workspace
yarn install
# Now switch to the example you want to run, e.g.
cd examples/gjs/adw-1-hello
yarn start The mistake that is probably made quickly is to check out the prepository without submodules, but you can also do this later: git submodule update --init --recursive |
Beta Was this translation helpful? Give feedback.
-
Start a new projectI am planning to add the feature to the CLI to generate project templates, but this is not yet possible. But you can start a new project by copy one of the examples (with a bundler of your choice, the examples use different bundlers like esbuild, vite or webpack) and then you have to adapt the package.json by removing the Before: {
"name": "@ts-for-gir-example/adw-1-hello-example",
"version": "4.0.0-beta.2",
"description": "Simple GJS Typescript hello-world example using Libadwaita",
"main": "index.js",
"type": "module",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:app": "vite build",
"build": "yarn build:app",
"start:app": "gjs -m dist/main.js",
"debug:app": "GTK_DEBUG=interactive gjs -m dist/main.js",
"start": "yarn build && yarn start:app",
"validate": "yarn validate:types && yarn validate:app",
"validate:types": "tsc --project tsconfig.types.json",
"validate:app": "tsc --noEmit",
"clear": "rm -rf dist"
},
"author": "Pascal Garber <[email protected]>",
"licence": "MIT",
"devDependencies": {
"@babel/core": "^7.24.0",
"@rollup/plugin-babel": "^6.0.4",
"rollup": "^4.13.0",
"typescript": "^5.4.2",
"vite": "^5.1.6"
},
"dependencies": {
"@girs/adw-1": "workspace:^",
"@girs/gio-2.0": "workspace:^",
"@girs/gjs": "workspace:^",
"@girs/glib-2.0": "workspace:^",
"@girs/gtk-4.0": "workspace:^"
}
}
After: {
"name": "my-project",
"version": "0.0.1",
"description": "My new project",
"main": "index.js",
"type": "module",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:app": "vite build",
"build": "npm run build:app",
"start:app": "gjs -m dist/main.js",
"debug:app": "GTK_DEBUG=interactive gjs -m dist/main.js",
"start": "npm run build && npm run start:app",
"validate": "npm run validate:types && npm run validate:app",
"validate:types": "tsc --project tsconfig.types.json",
"validate:app": "tsc --noEmit",
"clear": "rm -rf dist"
},
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.24.0",
"@rollup/plugin-babel": "^6.0.4",
"rollup": "^4.13.0",
"typescript": "^5.4.2",
"vite": "^5.1.6"
},
"dependencies": {
"@girs/adw-1": "^1.5.0-4.0.0-beta.2",
"@girs/gio-2.0": "^2.80.0-4.0.0-beta.2",
"@girs/gjs": "^4.0.0-beta.2",
"@girs/glib-2.0": "^2.80.0-4.0.0-beta.2",
"@girs/gtk-4.0": "^4.14.1-4.0.0-beta.2"
}
}
Now you are no longer dependent on yarn and you can use npm (or any other package manager of your choice) to install the dependencies and run the project: npm install
npm run start I haven't tested it, but it should work in theory, let me know if it works. I hope this helps :) |
Beta Was this translation helpful? Give feedback.
-
Hi Pascal, thank you so much these examples and explanation are super
helpful! I really appreciate you taking the time to walk me through this.
…On Thu, Apr 11, 2024 at 4:32 AM Pascal Garber ***@***.***> wrote:
If you want to start a new project, you can copy one of the examples (with
a bundler of your choice, the examples use different bundlers like esbuild,
vite <https://vitejs.dev/> or webpack <https://webpack.js.org/>) and then
you have to adapt the package.json by removing the yarn workspace
references and replacing yarn commands with npm run, in the case of
adw-1-hello it would look like this:
Before:
{
"name": ***@***.***/adw-1-hello-example",
"version": "4.0.0-beta.2",
"description": "Simple GJS Typescript hello-world example using Libadwaita",
"main": "index.js",
"type": "module",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:app": "vite build",
"build": "yarn build:app",
"start:app": "gjs -m dist/main.js",
"debug:app": "GTK_DEBUG=interactive gjs -m dist/main.js",
"start": "yarn build && yarn start:app",
"validate": "yarn validate:types && yarn validate:app",
"validate:types": "tsc --project tsconfig.types.json",
"validate:app": "tsc --noEmit",
"clear": "rm -rf dist"
},
"author": "Pascal Garber ***@***.***>",
"licence": "MIT",
"devDependencies": {
***@***.***/core": "^7.24.0",
***@***.***/plugin-babel": "^6.0.4",
"rollup": "^4.13.0",
"typescript": "^5.4.2",
"vite": "^5.1.6"
},
"dependencies": {
***@***.***/adw-1": "workspace:^",
***@***.***/gio-2.0": "workspace:^",
***@***.***/gjs": "workspace:^",
***@***.***/glib-2.0": "workspace:^",
***@***.***/gtk-4.0": "workspace:^"
}
}
After:
{
"name": ***@***.***/adw-1-hello-example",
"version": "4.0.0-beta.2",
"description": "Simple GJS Typescript hello-world example using Libadwaita",
"main": "index.js",
"type": "module",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:app": "vite build",
"build": "npm run build:app",
"start:app": "gjs -m dist/main.js",
"debug:app": "GTK_DEBUG=interactive gjs -m dist/main.js",
"start": "npm run build && npm run start:app",
"validate": "npm run validate:types && npm run validate:app",
"validate:types": "tsc --project tsconfig.types.json",
"validate:app": "tsc --noEmit",
"clear": "rm -rf dist"
},
"author": "Pascal Garber ***@***.***>",
"license": "MIT",
"devDependencies": {
***@***.***/core": "^7.24.0",
***@***.***/plugin-babel": "^6.0.4",
"rollup": "^4.13.0",
"typescript": "^5.4.2",
"vite": "^5.1.6"
},
"dependencies": {
***@***.***/adw-1": "^1.5.0-4.0.0-beta.2",
***@***.***/gio-2.0": "^2.80.0-4.0.0-beta.2",
***@***.***/gjs": "^4.0.0-beta.2",
***@***.***/glib-2.0": "^2.80.0-4.0.0-beta.2",
***@***.***/gtk-4.0": "^4.14.1-4.0.0-beta.2"
}
}
Now you are no longer dependent on yarn:
npm install
npm run start
I haven't tested it, but it should work in theory, let me know if it works
—
Reply to this email directly, view it on GitHub
<#160 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ2T6Z522DQCR7BPHADFQQLY4ZC7ZAVCNFSM6AAAAABGBSJRHKVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOBQG43TG>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@JumpLink Quick question sorry to bother, but are the following two imports different? I am trying to port a simple atspi example to ts, but I get different behavior in the typescript vs js versions ( I am not changing any code when porting to TS, just adding type annotations and the important). For context, the difference is a permission error in atspi if it makes a difference. I have permission to dump to accessibility tree in JS, but not in the compiled TS for some reason. I wasn't sure if I am misunderstanding anything still. JS:const Atspi = imports.gi.Atspi; TSimport Atspi from "@girs/atspi-2.0"; when compiled it outputs the following which I am not really sure if they are the same. They have the same API, but I feel something must be different. import _ from "gi://Atspi?version=2.0"; |
Beta Was this translation helpful? Give feedback.
@C-Loftus you need to run the example with the
gjs -m
flag to enable ESM Module support. Also, the@girs/*
imports can only be used with a bundler like esbuild, GJS itself has no support for NPM modules, so you will need a bundler to resolve this for you.Use the types in your JavaScript project
However, you can still use the types in the way you are trying to without needing a bundler or the TypeScript compiler. To do this, you need to create a jsconfig.json (note that this is not a tsconfig.json but a jsconfig.json) and add the
@girs/*
types there, for example like this: