SAP / ui5-typescript

Tooling to enable TypeScript support in SAPUI5/OpenUI5 projects
https://sap.github.io/ui5-typescript
Apache License 2.0
200 stars 28 forks source link

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

Open iljapostnovs opened 2 years ago

iljapostnovs commented 2 years ago

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 commented 2 years ago

@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?

iljapostnovs commented 2 years ago

@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 commented 2 years ago

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.