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

Open
#406 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
typescript
Domain
tooling

Research direction

Start by reviewing the MetadataOptions property definitions and the transformation that produces JavaScript and .gen.d.ts files. Compare the proposed type-guessing and tsType approaches, then determine how specific function and object types should be represented in generated declarations while remaining simple at runtime. Done means the chosen approach is defined and supported consistently.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
213
Forks
35
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from UI5/typescript

All issues in UI5/typescript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.