Skip to content

Commit d19c0ed

Browse files
Merge branch 'khushal87-crns-501' of github.com:GetStream/stream-chat-react-native into khushal87-crns-501
2 parents ff488fb + 22a3ba2 commit d19c0ed

File tree

4 files changed

+103
-47
lines changed

4 files changed

+103
-47
lines changed
7.64 KB
Loading
17.3 KB
Loading

docusaurus/docs/reactnative/basics/upgrade_helper.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,52 @@ user won't be able to see a cropped image on UI. In this version, we render imag
286286
</TabItem>
287287
</Tabs>
288288

289+
### Refactor Generics to a Single Generic
290+
291+
The Typescript generics can be simplified using a single Generic which can be passed to the component.
292+
293+
```tsx
294+
const client = StreamChat.getInstance<StreamChatType>('YOUR_API_KEY');
295+
```
296+
297+
where `StreamChatType` can be defined as;
298+
299+
```tsx
300+
type StreamChatType = {
301+
attachmentType: LocalAttachmentType;
302+
channelType: LocalChannelType;
303+
commandType: LocalCommandType;
304+
eventType: LocalEventType;
305+
messageType: LocalMessageType;
306+
reactionType: LocalReactionType;
307+
userType: LocalUserType;
308+
};
309+
```
310+
311+
:::note
312+
`LocalAttachmentType`, `LocalChannelType` etc. are type deifinitions for their respective key as per your application types necessities.
313+
314+
Eg:
315+
316+
```tsx
317+
type LocalAttachmentType = {
318+
file_size?: number;
319+
mime_type?: string;
320+
};
321+
type LocalChannelType = Record<string, unknown>;
322+
type LocalCommandType = string;
323+
type LocalEventType = Record<string, unknown>;
324+
type LocalMessageType = Record<string, unknown>;
325+
type LocalReactionType = Record<string, unknown>;
326+
type LocalUserType = {
327+
image?: string;
328+
};
329+
```
330+
331+
:::
332+
333+
A seperate guide has been added to documentation for details of [Generics](../customization/typescript.mdx#generics)
334+
289335
### Slow Mode Feature
290336

291337
Slow mode helps reduce noise on a channel by limiting users to a maximum of 1 message per cooldown interval.

docusaurus/docs/reactnative/customization/typescript.mdx

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,48 @@ In many cases TypeScript can use [inference](https://www.typescriptlang.org/docs
2121

2222
It is important that the proper generics be applied to the `stream-chat-js` client when it is instantiated.
2323
The [documentation on `stream-chat-js` TypeScript](https://github.com/GetStream/stream-chat-js#typescript-v2xx) has examples of how this can be done in detail.
24-
The client takes seven optional generics that correspond to the seven customizable fields that currently exist in `stream-chat-js`.
24+
The client takes seven generics that correspond to the seven customizable fields that currently exist in `stream-chat-js`.
2525

2626
```tsx
27-
const client = StreamChat.getInstance<
28-
AttachmentType,
29-
ChannelType,
30-
CommandType,
31-
EventType,
32-
MessageType,
33-
ReactionType,
34-
UserType
35-
>('YOUR_API_KEY');
27+
const client = StreamChat.getInstance<StreamChatType>('YOUR_API_KEY');
3628
```
3729

30+
where `StreamChatType` can be defined as a type with the seven generics that correspond to the seven customizable fields as follows:
31+
32+
```tsx
33+
type StreamChatType = {
34+
attachmentType: LocalAttachmentType;
35+
channelType: LocalChannelType;
36+
commandType: LocalCommandType;
37+
eventType: LocalEventType;
38+
messageType: LocalMessageType;
39+
reactionType: LocalReactionType;
40+
userType: LocalUserType;
41+
};
42+
```
43+
44+
:::note
45+
`LocalAttachmentType`, `LocalChannelType` etc. are type deifinitions for their respective key as per your application types necessities.
46+
47+
Eg:
48+
49+
```tsx
50+
type LocalAttachmentType = {
51+
file_size?: number;
52+
mime_type?: string;
53+
};
54+
type LocalChannelType = Record<string, unknown>;
55+
type LocalCommandType = string;
56+
type LocalEventType = Record<string, unknown>;
57+
type LocalMessageType = Record<string, unknown>;
58+
type LocalReactionType = Record<string, unknown>;
59+
type LocalUserType = {
60+
image?: string;
61+
};
62+
```
63+
64+
:::
65+
3866
The seven customizable fields these generics extend are provided via `stream-chat-js`.
3967

4068
1. [`Attachment`](https://github.com/GetStream/stream-chat-js/blob/0e29bea1e773f15599564b5174d4fabbd3bcb495/src/types.ts#L1471)
@@ -45,13 +73,10 @@ The seven customizable fields these generics extend are provided via `stream-cha
4573
6. [`Reaction`](https://github.com/GetStream/stream-chat-js/blob/0e29bea1e773f15599564b5174d4fabbd3bcb495/src/types.ts#L1854)
4674
7. [`User`](https://github.com/GetStream/stream-chat-js/blob/0e29bea1e773f15599564b5174d4fabbd3bcb495/src/types.ts#L2004)
4775

48-
All seven generics contain defaults in the `stream-chat-react-native` repo that you can extend for custom data.
76+
All seven generics contain defaults in the `stream-chat-react-native` repo that you can extend for custom data as per your types for the Channel, Message, etc.
4977

5078
```tsx
51-
type DefaultAttachmentType = Record<string, unknown> & {
52-
file_size?: number | string;
53-
mime_type?: string;
54-
};
79+
type DefaultAttachmentType = Record<string, unknown>;
5580
type DefaultChannelType = Record<string, unknown> & {
5681
image?: string;
5782
};
@@ -64,19 +89,23 @@ type DefaultUserType = Record<string, unknown> & {
6489
};
6590
```
6691

67-
Additional fields on the defaults i.e. `file_size`, `mime_type`, and `image` are custom fields used by `stream-chat-react-native` already within the SDK.
92+
Additional fields on the defaults i.e. `image` are custom fields used by `stream-chat-react-native` already within the SDK.
6893
When wanting to set a subset of generics the preceding and interceding generics must also be set in order for the TypeScript compiler to correctly understand intent.
6994

70-
To set `ChannelType` and `MessageType` for `AttachmentType`, `CommandType`, and `EventType` must also be set.
95+
```tsx
96+
type StreamChatType = {
97+
attachmentType: DefaultAttachmentType;
98+
channelType: DefaultChannelType;
99+
commandType: DefaultCommandType;
100+
eventType: DefaultEventType;
101+
messageType: DefaultMessageType;
102+
reactionType: DefaultReactionType;
103+
userType: DefaultUserType;
104+
};
105+
```
71106

72107
```tsx
73-
const client = StreamChat.getInstance<
74-
DefaultAttachmentType,
75-
{ image?: string; nickName?: string },
76-
DefaultCommandType,
77-
DefaultEventType,
78-
{ isAdminMessage?: boolean }
79-
>('YOUR_API_KEY');
108+
const client = StreamChat.getInstance<StreamChatType>('YOUR_API_KEY');
80109
```
81110

82111
:::note
@@ -92,7 +121,7 @@ The [TypeScript Example App](https://github.com/GetStream/stream-chat-react-nati
92121
Core to understanding this usage is [how generics can be applied to JSX elements](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#generic-type-arguments-in-jsx-elements).
93122

94123
In many cases the use of a single prop such as `client` or `channel` allows TypeScript to infer the generics on an element.
95-
In this case `LocalAttachmentType` is inferred from `channel` and passed to the props type for a custom Attachment component.
124+
In this case `StreamChatType` is inferred from `channel` and passed to the props type for a custom Attachment component.
96125

97126
<img src={inference} alt='TypeScript Inference' />
98127

@@ -101,13 +130,7 @@ In these cases the generics must be applied to the component directly.
101130
`MessageList` for instance could have the previous generics used on `client` applied to it in a similar manner.
102131

103132
```tsx
104-
<MessageList<
105-
DefaultAttachmentType,
106-
{ image?: string; nickName?: string },
107-
DefaultCommandType,
108-
DefaultEventType,
109-
{ isAdminMessage?: boolean }
110-
>
133+
<MessageList<StreamChatType>
111134
onThreadSelect={thread => {
112135
/** Set thread and navigate to thread screen */
113136
}}
@@ -124,13 +147,7 @@ Hooks, including those to access contexts, also require generics to be applied t
124147
`useChannelContext` for instance would have the previous generics applied to it to get a correctly typed return for `channel`.
125148

126149
```tsx
127-
const { channel } = useChannelContext<
128-
DefaultAttachmentType,
129-
{ image?: string; nickName?: string },
130-
DefaultCommandType,
131-
DefaultEventType,
132-
{ isAdminMessage?: boolean }
133-
>();
150+
const { channel } = useChannelContext<StreamChatType>();
134151
```
135152

136153
## Partial inference
@@ -144,12 +161,5 @@ The lack of partial inference is particularly relevant if Higher Order Component
144161
The `withChannelContext` HOC accepts the generics similarly to the `useChannelContext` hook, but because partial inference is not supported the props for the wrapped component must also be explicitly provided.
145162

146163
```tsx {2}
147-
withChannelContext<
148-
MyComponentProps,
149-
DefaultAttachmentType,
150-
{ image?: string; nickName?: string },
151-
DefaultCommandType,
152-
DefaultEventType,
153-
{ isAdminMessage?: boolean }
154-
>(MyComponent);
164+
withChannelContext<MyComponentProps, StreamChatType>(MyComponent);
155165
```

0 commit comments

Comments
 (0)