gpt4 book ai didi

javascript - 分配属性时,如何避免 Microsoft Bot Framework State PropertyAccessor 中出现 Typescript 错误 TS2345

转载 作者:行者123 更新时间:2023-12-01 00:25:40 29 4
gpt4 key购买 nike

在 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 中我无法找到避免该错误的方法。我可以直接使用状态属性并以这种方式附加属性,但我不确定这是正确的,或者它何时附加到对象和/或直接引用数据库。在本例中,数据库位于内存中。实际上它有效,但同样,我不确定我是否位于正确的对象上,该对象是引用我的状态属性访问器属性的对象,即 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>;

在状态存储实现下面,我添加了属性 setter 。

dialogDataAccessor = conversationState.createProperty<DialogData>(DIALOG_DATA_PROPERTY);
userProfileAccessor = userState.createProperty<UserProfile>(USER_PROFILE_PROPERTY);

我在对话框数据属性|DialogData中添加了与对话框状态属性分开的属性,因为我需要访问与包含对话状态的对话框堆栈的dialogstate分开的属性。

此外,它还允许我输入一个对话框 StatePropertyAccessor对话框状态不允许这样做。

最佳答案

我绝对会避免使用 this.userState.x.y.z = 'myNewData' 等直接访问 UserStateDialgoState。我已经看到这会导致很多很多问题,这就是我们使用访问器的原因。

最后的代码示例适用于 DialogState,但不适用于 UserState。这是一个很长的答案,但这里是......

index.ts 中,您create ConversationState and UserState 。这些是 BotFramework SDK 中处理非常具体的任务的类。

  • ConversationState在存储中将不同的对话相互隔离。
  • UserState不同用户的数据在存储中相互隔离
  • 它们都扩展 BotState ,它处理这些存储片段的实际存储和检索方式。
  • 您永远不应该直接操作这些对象上的任何内容,因为 Bot 框架依赖于其中每个对象的属性。

所以...您的代码示例“错误”的原因是因为您:

this.userState.createProperty<UserState>('UserState');

...它告诉机器人在现有的 UserState 对象中创建一个额外的 UserState 对象。这“有效”,但自从 UserState实际上没有任何属性,TypeScript 不知道您要在其中存储什么键和值。

在 TypeScript 中,您在使用如下内容时可能会看到错误:

const userProfile = await this.userProfileAccessor.get(turnContext, {});
const conversationData = await this.conversationDataAccessor.get(
turnContext, { promptedForUserName: false });

因为您还没有定义这些对象应该是什么类型。你需要类似的东西

export interface UserProfile {
name: string;
data: {
age: number;
location: string;
}
}

[...]
this.userState.createProperty<UserProfile>('UserProfile');
const userProfile: UserProfile = await this.userProfileAccessor.get(turnContext, {});

...或类似的东西。

注意:您还应该确保名称保持一致。例如,如果您使用 createProperty('UserProfile'),则在检索变量时应将其命名为 userProfile。另请注意,'UserProfile`` 字符串只是告诉createPropertyUserState中创建一个键名为“UserProfile”的对象,因此该对象看起来会是这样例如:UserState: { ...UserProfile: {} }`

<小时/>

我相对确定这既能解决您的所有问题,又能提供解决方案。如果没有,请在您的问题中添加一些代码,我将提供额外的帮助。

关于javascript - 分配属性时,如何避免 Microsoft Bot Framework State PropertyAccessor 中出现 Typescript 错误 TS2345,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59034571/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com