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

Missing working solution for event.getParameter("value") #409

Closed EricVanEldik closed 1 year ago

EricVanEldik commented 1 year ago

According to the release notes of 1.115.0 in https://sap.github.io/ui5-typescript/releasenotes.html evt.getParameter("eventParamName"); now returns a typescript error: Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

However the solution provided: public handleChange(evt: UI5Event<$InputBaseChangeEventParameters>) : void { ... } doesn't seem to work, because UI5Event is not defined as a type in "@sapui5/types": "^1.116.0"

I tried the following solution which does exists:

async onInputEmployeeChange(event: Event<Input$ChangeEventParameters>) {
        const input = event.getSource() as Input;
        const path = input.getBinding("value").getPath();
        const id = (event.getParameter("value") as string).trim();

but still gives the type error: Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

akudev commented 1 year ago

Right, the solution involving UI5Event may be unintuitive, as it is not clear where UI5Event is supposed to come from - it should despite its name be imported from sap/ui/base/Event. Sometimes we choose Names like UI5Element and UI5Event to prevent confusion with DOM Elements/Events, but of course they only work with the correct import.

Regarding the existing solution you tried, please check from which module you have imported Input$ChangeEventParameters - I'm pretty sure it's from the web components library ("sap.ui.webc..."), while the Input you are using is a "sap.m.Input". The WebComponents Input has a "change" event with no parameters, hence the "never".

Try with "InputBase$ChangeEventParameters" and import { InputBase$ChangeEventParameters } from "sap/m/InputBase"; instead. The event is defined on this base class. Or when using 1.115.1 or 1.116.0, you can directly use InputBase$ChangeEvent instead of Event<InputBase$ChangeEventParameters>.

akudev commented 1 year ago

I see the Input$ChangeEvent has also been mentioned in the release notes. https://github.com/SAP/ui5-typescript/commit/7d37027ac90a93cf393b6b00f00604368a1e77f9 fixes this and changes usages of UI5Event to Event, including the import now.

EricVanEldik commented 1 year ago

thank you, this solves it!