Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the global types importable #10555

Open
ComLock opened this issue May 14, 2024 · 0 comments · May be fixed by #10557
Open

Make the global types importable #10555

ComLock opened this issue May 14, 2024 · 0 comments · May be fixed by #10557
Assignees

Comments

@ComLock
Copy link
Member

ComLock commented May 14, 2024

LongTitle: Make the types in @enonic-types/global importable from @enonic-types/core

--

The types in @enonic-types/global are working as intended for Enonic XP server-side code files in src/main/resources/*.ts

However when writing tests the global functions and object must be replaced by mocks.
Since the globals are defined as "const" in @enonic-types/global, it's a type error if one tries to replace them.

Changing the type to var (let) is not a good idea since that's actually wrong when it comes to the actual "runtime" types.

My suggestion is to

  1. export the global types in @enonic-types/core
  2. make @enonic-types/global dependant on @enonic-types/core
  3. import the global types from @enonic-types/core into @enonic-types/global and use them to define the const's

Then if I need any of those global types anywhere, I can manually import only the specific ones I need from @enonic-types/core.

One example is when implementing the classes in Mock-XP, I would like to use the type for the global log object as an interface:

import type {Log as LogInterface} from '@enonic-types/core';

export class Log implements LogInterface {
  public debug(...args: unknown[]): void {
    //...
  }
}

The same goes then modifying the type of globalThis when mocking in test files:

import type {Log} from '@enonic-types/core';

declare module globalThis {
   let log: Log
}

log.debug = () => {
  // no-op silence :)
}

I can probably make it even prettier with a local Global Type Library, or a local Global Modifying Module.

But I need the actual types. I don't want to duplicate those types locally.
Define once, use everywhere.

@ComLock ComLock self-assigned this May 14, 2024
@ComLock ComLock changed the title Make the types in @enonic-types/global importable from @enonic-types/core Make the global types importable May 14, 2024
ComLock added a commit that referenced this issue May 14, 2024
@ComLock ComLock linked a pull request May 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant