不能调用map的值,因为可能未定义。

huangapple go评论71阅读模式
英文:

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 = &quot;role&quot;
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = &quot;&quot;
const map = new Map&lt;Role, any&gt;([
[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&lt;Role, Settings&gt; into Map&lt;Role, (secondParameter: string) =&gt; Settings&gt;, returning a function from the map, that will be invoked later.

enum Role {
ROLE_1 = &quot;role&quot;
}
const settingsRole1 = {}
const defaultSettings = {}
const secondParameter = &quot;&quot;
const map = new Map&lt;Role, (secondParameter: string) =&gt; any&gt;([
[Role.ROLE_1, (secondParameter: string) =&gt; 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 &#39;undefined&#39;.
I tried to guard it, but it doesn't seem to work...

enum Role {
ROLE_1 = &quot;role&quot;
}
const settings = {}
const defaultSettings = {}
const map = new Map&lt;Role, (secondParameter: string) =&gt; any&gt;([
[Role.ROLE_1, (secondParameter: string) =&gt; 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 -->

huangapple
  • 本文由 发表于 2023年7月31日 21:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定