SAP / ui5-typescript

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

Writing MetadataOptions.properties.type function and object as ts types #406

Open Revest117 opened 1 year ago

Revest117 commented 1 year ago

Currently, when defining properties of type function or object, only these type unsafe definitions are accepted. The ability to define more specific types would greatly improve the quality and readability of the code.

I have 2 ideas in my mind how this could be achieved. The first would be a more type guess based aproach like the following:

static readonly metadata: MetadataOptions = {
    properties: {
        someFunc: {
            type: "(button: sap.m.Button) => void"
        }
    }
};

// the .gen.d.ts would look like this:
import { $ControlSettings } from "sap/ui/core/Control";
import Button from "sap/m/Button";

declare module "./Example" {
    interface $ExampleSettings extends $ControlSettings {
        callback?: (button: Button) => void
    }
    // ... //
}

The idea is to define them as type-safe TS types and convert them to the simple type with the transformation to js. In the case of the example, the metadata would be rewritten to type: "function".

Alternatively, if guessing the type would be too difficult, since some object types can be very complex, enhancing the property with an additional key could make it easier and enable the useage of interfaces and named types:

static readonly metadata: MetadataOptions = {
    properties: {
        someFunc: {
            tsType: "(button: sap.m.Button) => void",
            type: "function"
        }
    }
};

This 2nd approach would also .

Here the tsType would be used for the gen.d.ts file and removed in the js transformation as it's not needed anymore.

It's not often that i need to use these property types but when I do, it's follow by unnessecary assertion of types.