I'm writing a React Native app and am trying to integrate Stream Chat. Using the Node SDK (server side) I want create a channel, give it a name, and a member to it.
async function createStreamChannel(channelId: string, name: string, userId: string) {
console.log(`Creating Stream channel: ${channelId} with name: ${name}`);
try {
const channel = streamClient.channel('messaging', channelId,
{
name: 'hello, world',
members: [userId],
created_by_id: userId
});
console.log(`Creating channel ${channelId} and adding user ${userId} to channel...`);
await channel.create();
console.log('Channel created successfully');
} catch (error) {
console.error(`Error creating Stream channel ${channelId}:`, error);
throw error;
}
}
The problem with the above snippet is that name is not a valid attribute of ChannelData. Versions from package.json listen below
"stream-chat": "^9.17.0",
"stream-chat-react-native": "^8.3.3"
It appears that the name property is actually valid there (as shown in the documentation) but the authors of the SDK forgot to add it to the ChannelData type, so you're seeing an excess property warning when you try to provide it. This isn't the first time the authors have forgotten to include a valid property in ChannelData. Consider filing an issue to let them know about it, or take the initiative to fix it yourself and send a pull request.
In the meantime you can provide a name as long as you don't use an object literal directly in the streamClient.channel call (the fact that you're using an inline/contextually-typed object literal there is why you see the excess property warning). For example:
async function createStreamChannel(channelId: string, name: string, userId: string) {
console.log(`Creating Stream channel: ${channelId} with name: ${name}`);
try {
const channelData = {
name: 'hello, world',
members: [userId],
created_by_id: userId
};
const channel = streamClient.channel('messaging', channelId, channelData);
console.log(`Creating channel ${channelId} and adding user ${userId} to channel...`);
await channel.create();
console.log('Channel created successfully');
} catch (error) {
console.error(`Error creating Stream channel ${channelId}:`, error);
throw error;
}
}
Note that you won't get any type safety for name with this workaround. You're allowed to write name: [] or other such nonsense in the object without seeing any static analysis errors, and at runtime things will break or at least not behave the way you want. You could define your own subtype of ChannelData which adds a name: string property and use that if you want a chance to catch such errors:
type ChannelDataWithName = ChannelData & {
name: string
}
async function createStreamChannel(channelId: string, name: string, userId: string) {
// …
const channelData: ChannelDataWithName = {
name: 'hello, world',
members: [userId],
created_by_id: userId
};
// …
}
You could even use this type to create a wrapper around streamClient.channel with a more-correct type signature. But I'd try to get the upstream SDK types fixed before spending much effort on such things.