Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
const obj = { a: 5 } as const obj.b ----> error const key = "b" obj[key] ----> no error
为什么打字稿不阻止访问会导致运行时错误的不存在的属性?在上面的示例key中,打字稿将其推断为文字“b”
key
TypeScript 是否将此报告为错误,取决于您配置的编译器设置。如果您打开该strict选项(或至少打开noImplicitAny),它将报告为错误。
strict
noImplicitAny
游乐场与noImplicitAny == false
noImplicitAny == false
游乐场与noImplicitAny == true
noImplicitAny == true