英文:
Cannot invoke value of map because possibly undefined
问题
以下是您要翻译的代码部分:
在我的应用程序中,我有一个`Map`,它以用户角色作为键返回一组设置。
enum Role {
ROLE_1 = "role"
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = ""
const map = new Map<Role, any>([
[Role.ROLE_1, settingsRole1]
]);
const settings = map.get(Role.ROLE_1) ?? defaultSettings;
现在我想引入第二个参数。由于我希望尽量少修改代码,我想将`Map<Role,Settings>`转换为`Map<Role,(secondParameter: string) => Settings>`,从映射中返回一个函数,稍后将调用它。
enum Role {
ROLE_1 = "role"
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = ""
const map = new Map<Role, (secondParameter: string) => any>([
[Role.ROLE_1, (secondParameter: string) => settingsRole1]
]);
const settings = map.get(Role.ROLE_1)(secondParameter) ?? defaultSettings;
尝试调用从映射返回的函数时,我收到以下错误:`TS2722:无法调用可能为'undefined'的对象`。
我尝试了一下保护它,但似乎不起作用...
enum Role {
ROLE_1 = "role"
}
const settings = {}
const defaultSettings = {}
const map = new Map<Role, (secondParameter: string) => any>([
[Role.ROLE_1, (secondParameter: string) => settings]
]);
const settings = map.has(Role.ROLE_1) ? map.get(Role.ROLE_1)(secondParameter) : defaultSettings;
我不明白如何使保护工作,无论如何,如果您想建议一种方法,我会考虑尝试其他方法。
请注意,我只提供了代码的翻译,不包括问题或其他内容。
英文:
In my application I have a Map
that returns a set of settings taking the role of the user as a key.
enum Role {
ROLE_1 = "role"
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = ""
const map = new Map<Role, any>([
[Role.ROLE_1, settingsRole1]
]);
const settings = map.get(Role.ROLE_1) ?? defaultSettings;
Now I want to introduce a second parameters. Since I want to modify the code as little as possible, I thought to transform the Map<Role, Settings>
into Map<Role, (secondParameter: string) => Settings>
, returning a function from the map, that will be invoked later.
enum Role {
ROLE_1 = "role"
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = ""
const map = new Map<Role, (secondParameter: string) => any>([
[Role.ROLE_1, (secondParameter: string) => settingsRole1]
]);
const settings = map.get(Role.ROLE_1)(secondParameter) ?? defaultSettings;
I receive this error while trying to calling the function returning from the map: TS2722: Cannot invoke an object which is possibly 'undefined'
.
I tried to guard it, but it doesn't seem to work...
enum Role {
ROLE_1 = "role"
}
const settings = {}
const defaultSettings = {}
const map = new Map<Role, (secondParameter: string) => any>([
[Role.ROLE_1, (secondParameter: string) => settings]
]);
const settings = map.has(Role.ROLE_1) ? map.get(Role.ROLE_1)(secondParameter) : defaultSettings;
I don't understand how to make the guard work, anyways I would consider trying other approaches if you want to suggest one
答案1
得分: 0
使用 !
在表达式后告诉编译器忽略其可为 null 的特性。在你的情况下,应该是(带有显式键检查)
const settings = map.has(Role.ROLE_1) ? map.get(Role.ROLE_1)!(secondParameter) : defaultSettings;
另一种方法是使用可选链 ?.
运算符
const settings = map.get(Role.ROLE_1)?.(secondParameter) ?? defaultSettings;
但从语义上稍有不同 - 它不区分丢失的键和具有显式 null
/ undefined
值的键。
英文:
Use !
after expression to tell compiler to ignore its nullability. In your case it should be (with explicit key check)
const settings = map.has(Role.ROLE_1) ? map.get(Role.ROLE_1)!(secondParameter) : defaultSettings;
Alternative way is to use optional chaining ?.
operator
const settings = map.get(Role.ROLE_1)?.(secondParameter) ?? defaultSettings;
but it's slightly different semantically - it doesn't distinguish between missing keys and keys with explicit null
/ undefined
values.
答案2
得分: 0
你可以使用可选链 ?.
来访问第一个参数。
const settings = map.get(Role.ROLE_1)?.(secondParameter) ?? defaultSettings;
英文:
You can use optional chaining ?.
for first parameter
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const settings = map.get(Role.ROLE_1)?.(secondParameter) ?? defaultSettings;
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论