During the process of constructing a bot in Typescript, I encountered TS2345 error with Typescript version 3.7.2. This error is causing issues when attempting to create properties dynamically, even if they are undefined, or referencing them in the stateProperty
accessor.
In the Javascript example, this functionality is achieved without any errors using the provided code snippet.
this.conversationDataAccessor = conversationState.createProperty(CONVERSATION_DATA_PROPERTY);
this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);
// The state management objects for the conversation and user state.
this.conversationState = conversationState;
this.userState = userState;
this.onMessage(async (turnContext, next) => {
// Get the state properties from the turn context.
const userProfile = await this.userProfileAccessor.get(turnContext, {});
const conversationData = await this.conversationDataAccessor.get(
turnContext, { promptedForUserName: false });
if (!userProfile.name) {
However, in Typescript, I am struggling to solve this error. While I can use the state property directly to attach the property, I am unsure about its accuracy and whether it is referring directly to the database, which in this case is an in-memory DB. Although it functions properly, I am uncertain if it's pointing to the correct object that should be accessing my state property accessor i.e.
const CONVERSATION_DATA_PROPERTY = 'conversationData';
Moreover, when I directly access the userState, which is Typed to BotState
, it seems to persist through the turn context.
One more question arises - What type should I assign to the user state stateProperty accessor?
I currently have it set to UserState
, but I am uncertain if it's correct. What should the userstate type be set as?
Here is the updated code example:
this.dialogState = this.conversationState.createProperty<DialogState>('DialogState');
this.userProfileAccessor = this.userState.createProperty<UserState>('UserState');
****Update per changes below
To address the information from the answer below, I have made the following changes.
In the index.ts file, I added 2 property accessors for the user profile and conversation data.
let userProfileAccessor: StatePropertyAccessor<UserProfile>;
let dialogDataAccessor: StatePropertyAccessor<DialogData>;
Following the state storage implementations, I included the property setters.
dialogDataAccessor = conversationState.createProperty<DialogData>(DIALOG_DATA_PROPERTY);
userProfileAccessor = userState.createProperty<UserProfile>(USER_PROFILE_PROPERTY);
I introduced the dialog data property|DialogData separately from the Dialog State property to allow individual access to properties separate from the dialogstate, which holds the dialogstack of the conversation state.
This approach also enables me to utilize a typed dialog StatePropertyAccessor
, which is not possible with dialogstate alone.