【问题标题】:Are some DOM types now "wrong" with TypeScript 2.0 and strictNullChecks?TypeScript 2.0 和 strictNullChecks 现在是否有一些 DOM 类型“错误”?
【发布时间】:2017-01-04 10:07:48
【问题描述】:

(这是使用 TypeScript 2.0.0 (npm install -g typescript@beta) 和 --strictNullChecks。)

根据lib.dom.d.ts,document.querySelector的类型签名如下:

querySelector(selectors: string): Element;

但是让这个函数返回 null 是微不足道的:

document.querySelector('asdf'); // returns null (in typical cases)

在 2.0 的严格空值检查之前,这似乎是合理的,因为您必须假设所有值都可能为空值。但是启用严格的空值检查后,您会陷入一种虚假的安全感。

如果querySelector 的类型是

querySelector(selectors: string): Element | null;

那么事情会更安全。这些类型是否可能会更新,或者我是否遗漏了一些关于其工作原理的内容?

【问题讨论】:

  • 是的,这是错误的。他们要么忘记了它,要么将其作为某种我想不到的妥协而保持不变。
  • 我认为这不是问这个问题的最佳地点。 typescript 的 github 页面上的问题会好得多。 :)
  • @toskv 认为您可能是对的,我去提交了一个 github 问题。但是他们明确要求人们来这里提问,您可以在此处的模板中看到github.com/Microsoft/TypeScript/issues/new

标签: typescript typescript2.0


【解决方案1】:

这些类型可能会更新

是的。请报告您发现的此类差异(示例问题https://github.com/Microsoft/TypeScript/issues/10315)?

【讨论】:

    猜你喜欢
    • 2020-03-11
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多