参考

TypeScript 核心库的定义
https://github.com/Microsoft/TypeScript/tree/main/src/lib
image.png

JS内置对象

JavaScript 中有很多内置对象,它们可以直接在 TypeScript 中当做定义好了的类型。内置对象是指根据标准在全局作用域(Global)上存在的对象。这里的标准是指 ECMAScript 和其他环境(比如 DOM)的标准。

DOM 和 BOM 的内置对象

DOM 和 BOM 提供的内置对象有:
Document、HTMLElement、Event、NodeList 等。
TypeScript 中会经常用到这些类型:

  1. let body: HTMLElement = document.body;
  2. let allDiv: NodeList = document.querySelectorAll('div');
  3. document.addEventListener('click', function(e: MouseEvent) { // Do something });

它们的定义文件同样在 TypeScript 核心库的定义文件中。

标签类型定义

HTMLElement举例

https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts
image.png

React+TS中使用案例

image.png
image.png
image.png

各种标签对应的类型定义

并不是所有的标签都有对应单独的类型定义,应该是存在额外的属性的标签才会定义,比如input标签,普通的标签则共用HTMLElement类型(先这么理解,待修正),比如section、 b 、html等等。
具体参考:
https://github.com/microsoft/TypeScript/blob/v3.9.5/lib/lib.dom.d.ts#L19224-L19343

  1. interface HTMLElementTagNameMap {
  2. "a": HTMLAnchorElement;
  3. "abbr": HTMLElement;
  4. "address": HTMLElement;
  5. "applet": HTMLAppletElement;
  6. "area": HTMLAreaElement;
  7. "article": HTMLElement;
  8. "aside": HTMLElement;
  9. "audio": HTMLAudioElement;
  10. "b": HTMLElement;
  11. "base": HTMLBaseElement;
  12. "basefont": HTMLBaseFontElement;
  13. "bdi": HTMLElement;
  14. "bdo": HTMLElement;
  15. "blockquote": HTMLQuoteElement;
  16. "body": HTMLBodyElement;
  17. "br": HTMLBRElement;
  18. "button": HTMLButtonElement;
  19. "canvas": HTMLCanvasElement;
  20. "caption": HTMLTableCaptionElement;
  21. "cite": HTMLElement;
  22. "code": HTMLElement;
  23. "col": HTMLTableColElement;
  24. "colgroup": HTMLTableColElement;
  25. "data": HTMLDataElement;
  26. "datalist": HTMLDataListElement;
  27. "dd": HTMLElement;
  28. "del": HTMLModElement;
  29. "details": HTMLDetailsElement;
  30. "dfn": HTMLElement;
  31. "dialog": HTMLDialogElement;
  32. "dir": HTMLDirectoryElement;
  33. "div": HTMLDivElement;
  34. "dl": HTMLDListElement;
  35. "dt": HTMLElement;
  36. "em": HTMLElement;
  37. "embed": HTMLEmbedElement;
  38. "fieldset": HTMLFieldSetElement;
  39. "figcaption": HTMLElement;
  40. "figure": HTMLElement;
  41. "font": HTMLFontElement;
  42. "footer": HTMLElement;
  43. "form": HTMLFormElement;
  44. "frame": HTMLFrameElement;
  45. "frameset": HTMLFrameSetElement;
  46. "h1": HTMLHeadingElement;
  47. "h2": HTMLHeadingElement;
  48. "h3": HTMLHeadingElement;
  49. "h4": HTMLHeadingElement;
  50. "h5": HTMLHeadingElement;
  51. "h6": HTMLHeadingElement;
  52. "head": HTMLHeadElement;
  53. "header": HTMLElement;
  54. "hgroup": HTMLElement;
  55. "hr": HTMLHRElement;
  56. "html": HTMLHtmlElement;
  57. "i": HTMLElement;
  58. "iframe": HTMLIFrameElement;
  59. "img": HTMLImageElement;
  60. "input": HTMLInputElement;
  61. "ins": HTMLModElement;
  62. "kbd": HTMLElement;
  63. "label": HTMLLabelElement;
  64. "legend": HTMLLegendElement;
  65. "li": HTMLLIElement;
  66. "link": HTMLLinkElement;
  67. "main": HTMLElement;
  68. "map": HTMLMapElement;
  69. "mark": HTMLElement;
  70. "marquee": HTMLMarqueeElement;
  71. "menu": HTMLMenuElement;
  72. "meta": HTMLMetaElement;
  73. "meter": HTMLMeterElement;
  74. "nav": HTMLElement;
  75. "noscript": HTMLElement;
  76. "object": HTMLObjectElement;
  77. "ol": HTMLOListElement;
  78. "optgroup": HTMLOptGroupElement;
  79. "option": HTMLOptionElement;
  80. "output": HTMLOutputElement;
  81. "p": HTMLParagraphElement;
  82. "param": HTMLParamElement;
  83. "picture": HTMLPictureElement;
  84. "pre": HTMLPreElement;
  85. "progress": HTMLProgressElement;
  86. "q": HTMLQuoteElement;
  87. "rp": HTMLElement;
  88. "rt": HTMLElement;
  89. "ruby": HTMLElement;
  90. "s": HTMLElement;
  91. "samp": HTMLElement;
  92. "script": HTMLScriptElement;
  93. "section": HTMLElement;
  94. "select": HTMLSelectElement;
  95. "slot": HTMLSlotElement;
  96. "small": HTMLElement;
  97. "source": HTMLSourceElement;
  98. "span": HTMLSpanElement;
  99. "strong": HTMLElement;
  100. "style": HTMLStyleElement;
  101. "sub": HTMLElement;
  102. "summary": HTMLElement;
  103. "sup": HTMLElement;
  104. "table": HTMLTableElement;
  105. "tbody": HTMLTableSectionElement;
  106. "td": HTMLTableDataCellElement;
  107. "template": HTMLTemplateElement;
  108. "textarea": HTMLTextAreaElement;
  109. "tfoot": HTMLTableSectionElement;
  110. "th": HTMLTableHeaderCellElement;
  111. "thead": HTMLTableSectionElement;
  112. "time": HTMLTimeElement;
  113. "title": HTMLTitleElement;
  114. "tr": HTMLTableRowElement;
  115. "track": HTMLTrackElement;
  116. "u": HTMLElement;
  117. "ul": HTMLUListElement;
  118. "var": HTMLElement;
  119. "video": HTMLVideoElement;
  120. "wbr": HTMLElement;
  121. }

犯过的错

刚使用TS,照猫画虎使用,看到别的标签有单独的类型,使用section标签,以为也会有,又是百度又是群里问人,耗时耗力都没找到对应类型,最后找了个名字带section的类型提交了😂,被打回来了
image.png

事件类型定义

MouseEvent举例

https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts
image.png

Vue+TS中使用

https://github.com/Armour/vue-typescript-admin-template/blob/88b7440b3330324cf639d5cbeb5350994d020473/src/components/RightPanel/index.vue
内置对象,无需引入,直接使用
image.png

React+TS中使用

因为React 中标签触发的事件对象,是合成的,并不是原生dom的事件对象。所以需要单独从React中引入类型定义,并不是内置的MouseEvent类型

import React, { memo, MouseEvent, useState } from ‘react’

image.png

Nodejs使用TS

TypeScript 核心库的定义中不包含 Node.js 部分。
Node.js 不是内置对象的一部分,如果想用 TypeScript 写 Node.js,则需要引入第三方声明文件:

npm install @types/node —save-dev

image.png