Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to add code completion or strong typing for objects with Typescript? #597

Open
velara3 opened this issue May 1, 2024 · 1 comment

Comments

@velara3
Copy link

velara3 commented May 1, 2024

I'm using this project in a nodejs project in vscode using Typescript..

I've got it to work with a custom class / jar / library and now I'm wondering if there's a way to add strong typing or code completion in vscode.

For example, I have the following working code:

var java = require("java");
java.classpath.push("pngLibrary.jar");
const PngColorType = java.import("com.fileformats.png.PngColorType");
var colorType = new PngColorType();

But if I type the variable like so:

var java = require("java");
java.classpath.push("pngLibrary.jar");
const PngColorType = java.import("com.fileformats.png.PngColorType");
var colorType: PngColorType = new PngColorType(); // error here

I get the following error:

'PngColorType' refers to a value, but is being used as a type here. Did you mean 'typeof PngColorType'? ts(2749)
---
type PngColorType = /*unresolved*/ any

Is there any support for strong typing, code completion and so on?

@velara3 velara3 changed the title Is there a way to add code completion or strong typing with Typescript? Is there a way to add code completion or strong typing for objects with Typescript? May 1, 2024
@lm-sousa
Copy link

Not that I am aware of but there are a few tools out there that generate .d.ts files from a Java source. You could try to use one of those to generate typings for your projects and get type-checking support.

Your other option is to do it manually like I do. This also helps to keep track of where JS and Java mix as anything I do not add to this list will throw an error.

export namespace JavaClasses {
  export interface JavaClass {
    (...args: unknown[]): any;
    new (...args: unknown[]): any;
    [key: string]: any;
  }

  /* eslint-disable @typescript-eslint/no-empty-interface */
  export interface Foo extends JavaClass {}

  export interface File extends JavaClass {
    getParentFile(): JavaClasses.File;
    getAbsolutePath(): string;
  }
}

// Somewhere in your code
return java.import("java.io.File") as JavaClasses.File;

Definitely not a perfect solution. Did the trick for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants