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

@ui5/ts-interface-generator: add generics to generated interface #376

Open
iljapostnovs opened this issue Aug 11, 2022 · 3 comments
Open
Labels
enhancement New feature or request ts-interface-generator Related to the ts-interface-generator sub-package

Comments

@iljapostnovs
Copy link

Hi.

There is an issue when generating interfaces for generic classes. Example:

import ManagedObject from "sap/ui/base/ManagedObject";

/**
 * @namespace com.ts.test.tstest.util
 */
export default abstract class MyGenericClass<T> extends ManagedObject {
	static readonly metadata: object = {
		properties: {
			test: "string"
		}
	}
}

Generated interface:

import { PropertyBindingInfo } from "sap/ui/base/ManagedObject";
import { $ManagedObjectSettings } from "sap/ui/base/ManagedObject";

declare module "./MyGenericClass" {

    /**
     * Interface defining the settings object used in constructor calls
     */
    interface $MyGenericClassSettings extends $ManagedObjectSettings {
        test?: string | PropertyBindingInfo;
    }

    export default interface MyGenericClass {

        // property: test
        getTest(): string;
        setTest(test: string): this;
    }
}

As a result, error message is shown: All declarations of 'MyGenericClass' must have identical type parameters
Which, basically, means, that generated interface should be:

export default interface MyGenericClass<T> {

        // property: test
        getTest(): string;
        setTest(test: string): this;
    }

Would it be possible to add generics to the interface?
Thanks!

@akudev
Copy link
Contributor

akudev commented Aug 11, 2022

@iljapostnovs If I don't miss anything, there is even already a pull request for this: #357

However, that one is stuck a bit, as such generic classes cannot be used as type of control (or ManagedObject) properties/aggregations etc. - the UI5 way of specifying them just doesn't allow it. As result, after merging that PR, one would be able to generate interfaces for classes with generics, but one could NOT use these classes in the metadata of another class (see #357 (comment)). And I don't see a proper way to overcome this at the time being.

What do you think, is this restriction one you (and most) could live with and is the overall feature still worth it? Or would it be a deal-breaker and too confusing when those classes cannot be used everywhere?

@akudev akudev added enhancement New feature or request ts-interface-generator Related to the ts-interface-generator sub-package labels Aug 11, 2022
@iljapostnovs
Copy link
Author

iljapostnovs commented Aug 12, 2022

@iljapostnovs If I don't miss anything, there is even already a pull request for this: #357

However, that one is stuck a bit, as such generic classes cannot be used as type of control (or ManagedObject) properties/aggregations etc. - the UI5 way of specifying them just doesn't allow it. As result, after merging that PR, one would be able to generate interfaces for classes with generics, but one could NOT use these classes in the metadata of another class (see #357 (comment)). And I don't see a proper way to overcome this at the time being.

What do you think, is this restriction one you (and most) could live with and is the overall feature still worth it? Or would it be a deal-breaker and too confusing when those classes cannot be used everywhere?

@akudev, I understood the problem.
The benefits of generics are far greater in my case. However, I do understand that it might confuse somebody. (Problem with inability to use generic classes at all might confuse somebody as well ;) ) The choice as for me looks like either "Please don't use generic classes" or "Please don't use generic classes in other class metadata". The second one looks much less restrictive if you ask me.

@iljapostnovs
Copy link
Author

iljapostnovs commented Aug 30, 2022

Hi @akudev,
I've found another issue with generics.
If class A is extending e.g. ManagedObject, class A is generic class, interface for class A is generated successfully, however, if class B is extending class A, nothing is generated for class B.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ts-interface-generator Related to the ts-interface-generator sub-package
Projects
None yet
Development

No branches or pull requests

2 participants