Skip to content

Commit 458e934

Browse files
fix: Add some exclusion to types to better handle remmaped DeviceEvent type to Redux Actions
1 parent 389562f commit 458e934

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

packages/suite/src/types/suite/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { messageSystemActions } from '@suite-common/message-system';
99
import type { Route } from '@suite-common/suite-types';
1010
import { notificationsActions } from '@suite-common/toast-notifications';
1111
import { deviceActions, discoveryActions, transactionsActions } from '@suite-common/wallet-core';
12-
import type { BlockchainEvent, TransportEvent, UiEvent } from '@trezor/connect';
12+
import { BlockchainEvent, DEVICE, DeviceEvent, TransportEvent, UiEvent } from '@trezor/connect';
13+
import { FilterOutFromUnionByTypeProperty } from '@trezor/type-utils';
1314

1415
import type { BackupAction } from 'src/actions/backup/backupActions';
1516
import type { OnboardingAction } from 'src/actions/onboarding/onboardingActions';
@@ -41,7 +42,14 @@ export type {
4142
TrezorDevice,
4243
} from '@suite-common/suite-types';
4344

44-
type TrezorConnectEvents = TransportEvent | UiEvent | BlockchainEvent;
45+
type FilteredDeviceEvents = FilterOutFromUnionByTypeProperty<
46+
DeviceEvent,
47+
// Those types are remapped onto different actions in the connectInitThunks.ts and not used directly
48+
// as the rest of the DeviceEvents.
49+
typeof DEVICE.CONNECT | typeof DEVICE.CONNECT_UNACQUIRED
50+
>;
51+
52+
type TrezorConnectEvents = TransportEvent | UiEvent | FilteredDeviceEvents | BlockchainEvent;
4553

4654
export type TransactionAction = ReturnType<
4755
(typeof transactionsActions)[keyof typeof transactionsActions]
@@ -62,7 +70,7 @@ type DeviceAuthenticityAction = ReturnType<
6270

6371
// all actions from all apps used to properly type Dispatch.
6472
export type Action =
65-
| TrezorConnectEvents // Todo: This should not be here, actions shall be defined independently from Connect Events (and they shall be mapped onto them)
73+
| TrezorConnectEvents
6674
| RouterAction
6775
| WindowAction
6876
| StorageAction
@@ -113,6 +121,7 @@ export type ForegroundAppProps = {
113121
export type ToastNotificationVariant = 'success' | 'info' | 'warning' | 'error' | 'transparent';
114122

115123
export { TorStatus } from '@trezor/suite-desktop-api/src/enums';
124+
116125
export interface TorBootstrap {
117126
current: number;
118127
total: number;

packages/type-utils/src/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,23 @@ export type DefinedUnionMember<T> = T extends string ? T : never;
108108
export type FilterPropertiesByType<T, ValueFilter> = {
109109
[Key in keyof T as T[Key] extends ValueFilter ? Key : never]: T[Key];
110110
};
111+
112+
/**
113+
* Removed the type from the union where `{ type: U }`.
114+
*
115+
* Example:
116+
* ```
117+
* type T1 =
118+
* | { type: 'A'; a: string }
119+
* | { type: 'B'; b: number }
120+
* | { type: 'C' | 'D' | 'E'; cde: boolean };
121+
*
122+
* // { type: 'A', a: string } | { type: 'B', b: number } | { type: 'D' | 'E', cde: boolean };
123+
* type NotC = FilterOutFromUnionByTypeProperty<T1, 'C'>;
124+
* ```
125+
*/
126+
export type FilterOutFromUnionByTypeProperty<T, U> = T extends { type: infer P }
127+
? P extends U
128+
? never
129+
: { type: Exclude<P, U> } & Omit<T, 'type'>
130+
: T;

suite-common/wallet-core/src/device/deviceActions.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createAction } from '@reduxjs/toolkit';
22

33
import { ButtonRequest, TrezorDevice } from '@suite-common/suite-types';
44
import { WalletType } from '@suite-common/wallet-types';
5-
import { DEVICE, Device, DeviceVersionChanged } from '@trezor/connect';
5+
import { DEVICE, Device } from '@trezor/connect';
66

77
export const DEVICE_MODULE_PREFIX = '@suite/device';
88

@@ -29,16 +29,6 @@ const deviceDisconnect = createAction(DEVICE.DISCONNECT, (payload: TrezorDevice)
2929
payload,
3030
}));
3131

32-
// this action is not used but is required because of the typings
33-
// changed here: https://github.com/trezor/trezor-suite/commit/c02412bccf80da7c827f624b7a7c85cdedf278c5#diff-2e9d057f0bfe2cc92fe50d4ce28838622d9e79fcca010ab8847a0fa288da13fd
34-
// in fact action is dispatched from connectInitThunk same as the rest of events
35-
const deviceFirmwareVersionChanged = createAction(
36-
DEVICE.FIRMWARE_VERSION_CHANGED,
37-
(payload: DeviceVersionChanged['payload']) => ({
38-
payload,
39-
}),
40-
);
41-
4232
const updatePassphraseMode = createAction(
4333
`${DEVICE_MODULE_PREFIX}/updatePassphraseMode`,
4434
(payload: { device: TrezorDevice; hidden: boolean; alwaysOnDevice?: boolean }) => ({ payload }),
@@ -118,5 +108,4 @@ export const deviceActions = {
118108
updateSelectedDevice,
119109
removeButtonRequests,
120110
setEntropyCheckFail,
121-
deviceFirmwareVersionChanged,
122111
};

0 commit comments

Comments
 (0)