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

Closed
ComLock opened this issue May 14, 2024 · 0 comments · Fixed by #10557
Closed

Make the global types importable #10555

ComLock opened this issue May 14, 2024 · 0 comments · Fixed by #10557
Assignees
Milestone

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
@rymsha rymsha closed this as completed in 1a48ac1 Sep 17, 2024
@rymsha rymsha added this to the 7.15.0 milestone Sep 18, 2024
anatol-sialitski pushed a commit that referenced this issue Oct 31, 2024
* Fix #10555 Make global types importable

* Define and reuse definitions both as global and export

* XpBeans and XpLibraries belongs in globals

* Make global depend on core (not the other way around)

* Remove noImplicitUseStrict which is deprecated in TypeScript 5.5

* Remove NewBean and ScriptValue from global scope. Can be imported from core instead.

* Enable strict and fix most type errors

* Fix TypeScript errors in grid.ts

---------

Co-authored-by: Mikita Taukachou <edloidas@gmail.com>

(cherry picked from commit 1a48ac1)
anatol-sialitski pushed a commit that referenced this issue Oct 31, 2024
* Fix #10555 Make global types importable

* Define and reuse definitions both as global and export

* XpBeans and XpLibraries belongs in globals

* Make global depend on core (not the other way around)

* Remove noImplicitUseStrict which is deprecated in TypeScript 5.5

* Remove NewBean and ScriptValue from global scope. Can be imported from core instead.

* Enable strict and fix most type errors

* Fix TypeScript errors in grid.ts

---------

Co-authored-by: Mikita Taukachou <edloidas@gmail.com>

(cherry picked from commit 1a48ac1)
rymsha pushed a commit that referenced this issue Nov 1, 2024
* Fix #10555 Make global types importable

* Define and reuse definitions both as global and export

* XpBeans and XpLibraries belongs in globals

* Make global depend on core (not the other way around)

* Remove noImplicitUseStrict which is deprecated in TypeScript 5.5

* Remove NewBean and ScriptValue from global scope. Can be imported from core instead.

* Enable strict and fix most type errors

* Fix TypeScript errors in grid.ts

---------

Co-authored-by: Mikita Taukachou <edloidas@gmail.com>

(cherry picked from commit 1a48ac1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants