![](/img/trans.png)
[英]TypeScript error TS2345 when rejecting a Promise with an error
[英]How can I avoid a Typescript Error, TS2345, in the Microsoft Bot Framework's StatePropertyAccessor when I assign a property
在 Typescript 中构建机器人时,Typescript 版本 3.7.2 出现错误 TS2345 错误。 该错误阻止我动态创建属性,即使它们未定义,甚至在stateProperty
访问器中引用它们。
在javascript 示例中,这是使用以下代码完成的,没有错误。
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 属性并以这种方式附加该属性,但我不确定这是正确的,或者它何时确切地附加到对象和/或直接引用数据库。 在这种情况下,内存中的数据库。 实际上它有效,但同样,我不确定我是否在正确的对象上,该对象将是指我的状态属性访问器属性的对象,即const CONVERSATION_DATA_PROPERTY = 'conversationData';
话虽如此,当我直接访问输入到BotState
它似乎确实在转弯上下文中持续存在。
另一个问题,我应该将什么类型应用于用户状态 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);
我在对话框数据属性中添加了与 Dialog State 属性分开的 DialogData 属性,因为我需要访问与包含对话状态的对话框堆栈的 dialogstate 分开的属性。
此外,它允许我有一个对话框状态不允许的类型化对话框StatePropertyAccessor
。
您是否在抛出错误的行上方的行中尝试了此代码?
//@ts-ignore
我绝对会避免使用类似this.userState.xyz = 'myNewData'
东西直接访问UserState
或DialgoState
。 我已经看到这导致了很多很多问题,这也是我们使用accessors
。
最后的代码示例适用于DialogState
但不适用于UserState
。 这是一个很长的答案为什么,但在这里......
在index.ts
,您创建ConversationState
和UserState
。 这些是 BotFramework SDK 中处理非常具体任务的类。
ConversationState
在存储中将不同的对话彼此隔离。UserState
在存储中将不同用户的数据相互隔离BotState
,它处理这些存储片段的实际存储和检索方式。所以......你的代码示例“错误”的原因是因为你有:
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`` string just tells
createProperty to create an object in
UserState 中to create an object in
with the key name of "UserProfile", so the object would look something like:
UserState: { ...UserProfile: {} }`
我相对确定这既可以解决您的所有问题,也可以为它们提供解决方案。 如果没有,请在您的问题中添加一些代码,我会提供其他帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.