pubnub / javascript

PubNub JavaScript SDK docs https://www.pubnub.com/docs/sdks/javascript
Other
553 stars 401 forks source link

Module subpathing required to import types #411

Closed yo1dog closed 4 days ago

yo1dog commented 1 month ago

To import types, one must import them from module subpaths. This ties projects to the module's internal file structure, which is against best practices, and could make any internal file renaming/restructuring a breaking change.

Instead, you could expose the types from the module root using a namespace as described in https://github.com/pubnub/javascript/issues/410.

For example, currently one must:

import type {FetchedMessage} from 'pubnub/lib/types/core/types/api/history';

Using the namespace you could instead do:

declare namespace PubNub {
  export * from '../core/types/api/history';
  ...
}
import {FetchedMessage} from 'pubnub';

Or, keeping the types namespaced:

declare namespace PubNub {
  export * as History from '../core/types/api/history';
  ...
}
import * as PubNub from 'pubnub';
let myMessages: PubNub.History.FetchedMessage[];
parfeon commented 3 weeks ago

Created a script which will merge all type definitions created by tsc into a single file. Common types (for whole library) part of the root PubNub namespace and specific API grouped under respective nested namespaces.

import PubNub, {PubNubConfiguration, Publish, History} from 'pubnub';

// PubNub client configuration.
let configuration: PubNubConfiguration = {
  publishKey: 'secret', 
  subscribeKey: 'secret', 
  userId: 'user-1'
}; 

// Fetch messages configuration.
let fetchParameter: History.FetchMessagesParameters = {
  channels: ['channel-1']
};
parfeon commented 4 days ago

This issue has been addressed in 8.2.10.