-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed generic selector types (#1199)
Updated redux selectors to properly derive return types from generic input types. fixes #1197
- Loading branch information
Showing
1 changed file
with
40 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,113 @@ | ||
import type { RootState, Storage, WorkspaceSettings } from './store'; | ||
import type { RootState } from './store'; | ||
|
||
const EMPTY_OBJECT = Object.freeze({}); | ||
|
||
const EMPTY_MAP: ReadonlyMap<unknown, unknown> = new Map(); | ||
|
||
export type Selector<State extends RootState, R> = (store: State) => R; | ||
|
||
type Settings<State extends RootState> = State['workspace']['data']['settings']; | ||
|
||
// User | ||
export const getUser = <State extends RootState = RootState>( | ||
store: State | ||
): State['user'] => store.user; | ||
export const getUser = <State extends RootState>(store: State): State['user'] => | ||
store.user; | ||
|
||
export const getUserName = <State extends RootState = RootState>( | ||
export const getUserName = <State extends RootState>( | ||
store: State | ||
): State['user']['name'] => getUser(store).name; | ||
|
||
export const getUserGroups = <State extends RootState = RootState>( | ||
export const getUserGroups = <State extends RootState>( | ||
store: State | ||
): State['user']['groups'] => getUser(store).groups; | ||
|
||
// Storage | ||
export const getStorage = <State extends RootState = RootState>( | ||
export const getStorage = <State extends RootState>( | ||
store: State | ||
): State['storage'] => store.storage; | ||
|
||
export const getCommandHistoryStorage = <State extends RootState = RootState>( | ||
export const getCommandHistoryStorage = <State extends RootState>( | ||
store: State | ||
): State['storage']['commandHistoryStorage'] => | ||
getStorage(store).commandHistoryStorage; | ||
|
||
export const getFileStorage = <State extends RootState = RootState>( | ||
export const getFileStorage = <State extends RootState>( | ||
store: State | ||
): State['storage']['fileStorage'] => getStorage(store).fileStorage; | ||
|
||
export const getWorkspaceStorage = <State extends RootState = RootState>( | ||
export const getWorkspaceStorage = <State extends RootState>( | ||
store: State | ||
): Storage['workspaceStorage'] => getStorage(store).workspaceStorage; | ||
): State['storage']['workspaceStorage'] => getStorage(store).workspaceStorage; | ||
|
||
// Workspace | ||
export const getWorkspace = <State extends RootState = RootState>( | ||
export const getWorkspace = <State extends RootState>( | ||
store: State | ||
): RootState['workspace'] => store.workspace; | ||
): State['workspace'] => store.workspace; | ||
|
||
// Settings | ||
export const getSettings = <State extends RootState = RootState>( | ||
export const getSettings = <State extends RootState>( | ||
store: State | ||
): RootState['workspace']['data']['settings'] => | ||
getWorkspace(store).data.settings; | ||
): Settings<State> => getWorkspace(store).data.settings; | ||
|
||
export const getDefaultDateTimeFormat = <State extends RootState = RootState>( | ||
export const getDefaultDateTimeFormat = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['defaultDateTimeFormat'] => | ||
): Settings<State>['defaultDateTimeFormat'] => | ||
getSettings(store).defaultDateTimeFormat; | ||
|
||
export const getDefaultDecimalFormatOptions = < | ||
State extends RootState = RootState | ||
>( | ||
store: State | ||
): WorkspaceSettings['defaultDecimalFormatOptions'] => | ||
): Settings<State>['defaultDecimalFormatOptions'] => | ||
getSettings(store).defaultDecimalFormatOptions ?? EMPTY_OBJECT; | ||
|
||
export const getDefaultIntegerFormatOptions = < | ||
State extends RootState = RootState | ||
>( | ||
store: State | ||
): WorkspaceSettings['defaultIntegerFormatOptions'] => | ||
): Settings<State>['defaultIntegerFormatOptions'] => | ||
getSettings(store).defaultIntegerFormatOptions ?? EMPTY_OBJECT; | ||
|
||
export const getFormatter = <State extends RootState = RootState>( | ||
export const getFormatter = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['formatter'] => getSettings(store).formatter; | ||
): Settings<State>['formatter'] => getSettings(store).formatter; | ||
|
||
export const getTimeZone = <State extends RootState = RootState>( | ||
export const getTimeZone = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['timeZone'] => getSettings(store).timeZone; | ||
): Settings<State>['timeZone'] => getSettings(store).timeZone; | ||
|
||
export const getShowTimeZone = <State extends RootState = RootState>( | ||
export const getShowTimeZone = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['showTimeZone'] => getSettings(store).showTimeZone; | ||
): Settings<State>['showTimeZone'] => getSettings(store).showTimeZone; | ||
|
||
export const getShowTSeparator = <State extends RootState = RootState>( | ||
export const getShowTSeparator = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['showTSeparator'] => getSettings(store).showTSeparator; | ||
): Settings<State>['showTSeparator'] => getSettings(store).showTSeparator; | ||
|
||
export const getTruncateNumbersWithPound = < | ||
State extends RootState = RootState | ||
>( | ||
store: State | ||
): WorkspaceSettings['truncateNumbersWithPound'] => | ||
): Settings<State>['truncateNumbersWithPound'] => | ||
getSettings(store).truncateNumbersWithPound; | ||
|
||
export const getDisableMoveConfirmation = <State extends RootState = RootState>( | ||
export const getDisableMoveConfirmation = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['disableMoveConfirmation'] => | ||
): Settings<State>['disableMoveConfirmation'] => | ||
getSettings(store).disableMoveConfirmation || false; | ||
|
||
export const getShortcutOverrides = <State extends RootState = RootState>( | ||
export const getShortcutOverrides = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['shortcutOverrides'] => | ||
getSettings(store).shortcutOverrides; | ||
): Settings<State>['shortcutOverrides'] => getSettings(store).shortcutOverrides; | ||
|
||
export const getDefaultNotebookSettings = <State extends RootState = RootState>( | ||
export const getDefaultNotebookSettings = <State extends RootState>( | ||
store: State | ||
): WorkspaceSettings['defaultNotebookSettings'] => | ||
): Settings<State>['defaultNotebookSettings'] => | ||
getSettings(store).defaultNotebookSettings ?? EMPTY_OBJECT; | ||
|
||
export const getActiveTool = <State extends RootState = RootState>( | ||
export const getActiveTool = <State extends RootState>( | ||
store: State | ||
): RootState['activeTool'] => store.activeTool; | ||
): State['activeTool'] => store.activeTool; | ||
|
||
export const getPlugins = <State extends RootState = RootState>( | ||
export const getPlugins = <State extends RootState>( | ||
store: State | ||
): RootState['plugins'] => store.plugins ?? EMPTY_MAP; | ||
): State['plugins'] => store.plugins ?? EMPTY_MAP; |