简体   繁体   English

类型上不存在属性:为什么 TypeScript 抱怨而 JavaScript 不抱怨?

[英]Property does not exist on type: Why does TypeScript complain while JavaScript doesn't?

In the following code在下面的代码中

import { useMsal } from "@azure/msal-react";

function ProfileContent() {
    const { instance, accounts } = useMsal();

    const name = accounts[0] && accounts[0].name;

JavaScript has no problem with this, and the code works as expected. JavaScript对此没有任何问题,代码按预期工作。 But when I make this a TypeScript file, I get errors that但是当我把它变成一个TypeScript文件时,我得到的错误是

Property 'name' does not exist on type 'Partial<Pick<AccountInfo, "homeAccountId" |类型 'Partial<Pick<AccountInfo, "homeAccountId" 上不存在属性 'name' | "localAccountId" | "localAccountId" | "username">> “用户名”>>

What has so drastically changed to the point that TypeScript believes the property within the library doesn't even exist?是什么发生了如此巨大的变化,以至于 TypeScript 认为图书馆内的财产甚至不存在?

Browsing through the source code, it looks like they might have used the wrong type in IMsalContext .浏览源代码,看起来他们可能在IMsalContext中使用了错误的类型。 There's a commit on the dev branch with the message "Fix accounts type" .dev分支上有一个提交,消息是"Fix accounts type"

Here is the current released version , the dev branch version , the pull request , and the associated issue .这是当前发布的版本开发分支版本拉取请求相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM