ui5-typescript icon indicating copy to clipboard operation
ui5-typescript copied to clipboard

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

Open iljapostnovs opened this issue 3 years ago • 3 comments

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!

iljapostnovs avatar Aug 11 '22 13:08 iljapostnovs

@iljapostnovs If I don't miss anything, there is even already a pull request for this: https://github.com/SAP/ui5-typescript/pull/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 https://github.com/SAP/ui5-typescript/pull/357#issuecomment-1146137512). 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 avatar Aug 11 '22 14:08 akudev

@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 avatar Aug 12 '22 07:08 iljapostnovs

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.

iljapostnovs avatar Aug 30 '22 07:08 iljapostnovs