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

`@sapui5/ts-types-esm` - `sap/ui/core/Core` is incorrectly exported as an instance instead of a class #443

Closed DetachHead closed 7 months ago

DetachHead commented 7 months ago

Describe the bug sap/ui/core/Core seems to be exported as an instance of the Core interface, instead of a class:

declare module "sap/ui/core/Core" {
    interface Core {
        // ...
    }
    const Core: Core
    export default Core
}

this means that it cannot be instantiated when imported:

import Core from 'sap/ui/core/Core'

// typescript error:
// This expression is not constructable.
//   Type 'Core' has no construct signatures.
const core = new Core()

Expected behavior it should probably be something like this:

declare module "sap/ui/core/Core" {
    export default class Core {
        // ...
    }
}

Additional context at runtime, Core is a class that needs to be instantiated:

image

codeworrior commented 7 months ago

Eh, no.

At runtime, the Core module returns an instance of sap.ui.core.Core. Each call to the constructor (if you get access to it via globals) would even return the same singletons instance.

Check the source;

    /*
     * Create a new (the only) instance of the Core and return it's interface as module value.
     *
     * Do not export the module value under the global name!
     *
     * Note that the Core = EventProvider.extend() call above already exposes sap.ui.core.Core.
     * This is needed for backward compatibility reason, in case some other code tries to enhance
     * the core prototype. Once global names are switched off, such extension scenarios are
     * no longer supported.
     */
    oCore = new Core().getInterface();
    return oCore;

You must not create new instances but rather use the export of the module.

But I understand that the documentation falls short to explain this. In earlier versions, this was slightly better, but the example went lost with a recent refactoring (most likely as the example was based on an API that uses globals):

image

I'll take care of the documentation aspect, but that's a topic for openui5, not ui5-typescript. The types are correct and reflect the intended usage of the Core.

I therefore close this issue.