【发布时间】:2019-11-25 14:57:08
【问题描述】:
在 Typescript 中构建机器人时,Typescript 版本 3.7.2 出现错误 TS2345 错误。该错误使我无法动态创建属性,即使它们未定义,甚至在 stateProperty 访问器中引用它们。
在javascript example 中,这是通过以下代码完成的,没有错误。
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) {
但是,在 Typescript 中,我无法找到避免错误的方法。我可以直接使用 state 属性并以这种方式附加属性,但我不确定这是正确的,或者它何时确切地附加到对象或直接引用 DB。在这种情况下,内存中的数据库。实际上它是有效的,但我不确定我是否在正确的对象上,即引用我的状态属性访问器属性的对象,即const CONVERSATION_DATA_PROPERTY = 'conversationData';
话虽如此,当我直接访问键入为BotState 的 userState 时,它似乎确实会在转弯上下文中持续存在。
另一个问题,我应该对用户状态 stateProperty 访问器应用什么类型?
我已将其设置为 UserState,但我不确定这是否正确。用户状态类型应该设置为什么?
下面是代码示例:
this.dialogState = this.conversationState.createProperty<DialogState>('DialogState');
this.userProfileAccessor = this.userState.createProperty<UserState>('UserState');
****根据以下更改更新
我已根据以下答案中的信息添加了更改。
对于用户配置文件和对话数据,我现在在 index.ts 文件中添加 2 个属性访问器。
let userProfileAccessor: StatePropertyAccessor<UserProfile>;
let dialogDataAccessor: StatePropertyAccessor<DialogData>;
在我在属性设置器中添加的状态存储实现下方。
dialogDataAccessor = conversationState.createProperty<DialogData>(DIALOG_DATA_PROPERTY);
userProfileAccessor = userState.createProperty<UserProfile>(USER_PROFILE_PROPERTY);
我在对话框数据属性中添加了与对话框状态属性分开的对话框数据,因为我需要访问与包含对话状态的对话框堆栈的对话框状态分开的属性。
另外,它允许我有一个对话框状态不允许的类型化对话框StatePropertyAccessor。
【问题讨论】:
标签: javascript typescript botframework state