<分区>
it’s best not to add annotations when the type system would end up inferring the same type anyway.
https://www.typescriptlang.org/docs/handbook/2/basic-types.html
请举例说明何时添加类型注释,何时不添加。
我怎么知道 Typescript 何时能够自行确定类型?
<分区>
it’s best not to add annotations when the type system would end up inferring the same type anyway.
https://www.typescriptlang.org/docs/handbook/2/basic-types.html
请举例说明何时添加类型注释,何时不添加。
我怎么知道 Typescript 何时能够自行确定类型?
最佳答案
当 TS 无法推断类型时,它会将其设置为 any
。有一个选项 noImplicitAny
,它告诉编译器就此类情况向您发出警告,以便您可以查看未推断出哪些类型并对其进行注释。
关于javascript - 什么时候应该在 Typescript 中显式添加类型注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67209564/
相关文章:
javascript - 简单的 JavaScript 不起作用
javascript - 防止随机操作在 Javascript 中返回相同的值两次
javascript - jQuery 识别 URL 并追加参数
javascript - 单击另一个输入后如何防止我的日期选择器重新聚焦
javascript - 开 Jest mock promise 方法调用错误次数
javascript - 使用 css3 比例会模糊 Metro 应用程序中的 View
javascript - TensorflowJS:计算多个张量之间的距离或相似度的最佳方法?
node.js - 属性 proxyConfig 是不允许的。 Angular
typescript - 当 noUncheckedIndexedAccess 为 true 时,按长度缩小数组类型的类型安全方法