tsc-ts 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (596) hide show
  1. package/README.md +46 -0
  2. package/dist/api/compiler/ast-utilities.d.ts +82 -0
  3. package/dist/api/compiler/ast-utilities.js +462 -0
  4. package/dist/api/compiler/ast.d.ts +2350 -0
  5. package/dist/api/compiler/ast.js +11206 -0
  6. package/dist/api/compiler/astnav.d.ts +37 -0
  7. package/dist/api/compiler/astnav.js +767 -0
  8. package/dist/api/compiler/binder-utilities.d.ts +182 -0
  9. package/dist/api/compiler/binder-utilities.js +1240 -0
  10. package/dist/api/compiler/binder.d.ts +33 -0
  11. package/dist/api/compiler/binder.js +2358 -0
  12. package/dist/api/compiler/character-codes.d.ts +161 -0
  13. package/dist/api/compiler/character-codes.js +160 -0
  14. package/dist/api/compiler/checker-calls.d.ts +46 -0
  15. package/dist/api/compiler/checker-calls.js +1640 -0
  16. package/dist/api/compiler/checker-classes.d.ts +30 -0
  17. package/dist/api/compiler/checker-classes.js +582 -0
  18. package/dist/api/compiler/checker-contextual.d.ts +77 -0
  19. package/dist/api/compiler/checker-contextual.js +1136 -0
  20. package/dist/api/compiler/checker-declaration-order.d.ts +12 -0
  21. package/dist/api/compiler/checker-declaration-order.js +356 -0
  22. package/dist/api/compiler/checker-declarations.d.ts +33 -0
  23. package/dist/api/compiler/checker-declarations.js +888 -0
  24. package/dist/api/compiler/checker-declared-types.d.ts +48 -0
  25. package/dist/api/compiler/checker-declared-types.js +787 -0
  26. package/dist/api/compiler/checker-decorators.d.ts +26 -0
  27. package/dist/api/compiler/checker-decorators.js +562 -0
  28. package/dist/api/compiler/checker-deprecation.d.ts +33 -0
  29. package/dist/api/compiler/checker-deprecation.js +315 -0
  30. package/dist/api/compiler/checker-destructuring.d.ts +34 -0
  31. package/dist/api/compiler/checker-destructuring.js +584 -0
  32. package/dist/api/compiler/checker-elaboration.d.ts +15 -0
  33. package/dist/api/compiler/checker-elaboration.js +290 -0
  34. package/dist/api/compiler/checker-emit-helpers.d.ts +58 -0
  35. package/dist/api/compiler/checker-emit-helpers.js +154 -0
  36. package/dist/api/compiler/checker-emit-resolver.d.ts +75 -0
  37. package/dist/api/compiler/checker-emit-resolver.js +579 -0
  38. package/dist/api/compiler/checker-evaluator.d.ts +27 -0
  39. package/dist/api/compiler/checker-evaluator.js +188 -0
  40. package/dist/api/compiler/checker-expressions.d.ts +67 -0
  41. package/dist/api/compiler/checker-expressions.js +1351 -0
  42. package/dist/api/compiler/checker-features.d.ts +6 -0
  43. package/dist/api/compiler/checker-features.js +354 -0
  44. package/dist/api/compiler/checker-flow.d.ts +86 -0
  45. package/dist/api/compiler/checker-flow.js +169 -0
  46. package/dist/api/compiler/checker-functions.d.ts +43 -0
  47. package/dist/api/compiler/checker-functions.js +1002 -0
  48. package/dist/api/compiler/checker-generated-code.d.ts +25 -0
  49. package/dist/api/compiler/checker-generated-code.js +168 -0
  50. package/dist/api/compiler/checker-globals.d.ts +14 -0
  51. package/dist/api/compiler/checker-globals.js +37 -0
  52. package/dist/api/compiler/checker-grammar.d.ts +35 -0
  53. package/dist/api/compiler/checker-grammar.js +2573 -0
  54. package/dist/api/compiler/checker-heritage.d.ts +28 -0
  55. package/dist/api/compiler/checker-heritage.js +900 -0
  56. package/dist/api/compiler/checker-inference.d.ts +179 -0
  57. package/dist/api/compiler/checker-inference.js +2105 -0
  58. package/dist/api/compiler/checker-iteration.d.ts +115 -0
  59. package/dist/api/compiler/checker-iteration.js +857 -0
  60. package/dist/api/compiler/checker-javascript.d.ts +42 -0
  61. package/dist/api/compiler/checker-javascript.js +441 -0
  62. package/dist/api/compiler/checker-jsdoc.d.ts +30 -0
  63. package/dist/api/compiler/checker-jsdoc.js +320 -0
  64. package/dist/api/compiler/checker-jsx.d.ts +94 -0
  65. package/dist/api/compiler/checker-jsx.js +1433 -0
  66. package/dist/api/compiler/checker-literals.d.ts +6 -0
  67. package/dist/api/compiler/checker-literals.js +43 -0
  68. package/dist/api/compiler/checker-members.d.ts +198 -0
  69. package/dist/api/compiler/checker-members.js +2126 -0
  70. package/dist/api/compiler/checker-modules.d.ts +85 -0
  71. package/dist/api/compiler/checker-modules.js +2141 -0
  72. package/dist/api/compiler/checker-narrowing.d.ts +120 -0
  73. package/dist/api/compiler/checker-narrowing.js +2797 -0
  74. package/dist/api/compiler/checker-object-literals.d.ts +14 -0
  75. package/dist/api/compiler/checker-object-literals.js +479 -0
  76. package/dist/api/compiler/checker-operators.d.ts +33 -0
  77. package/dist/api/compiler/checker-operators.js +1040 -0
  78. package/dist/api/compiler/checker-overloads.d.ts +7 -0
  79. package/dist/api/compiler/checker-overloads.js +270 -0
  80. package/dist/api/compiler/checker-property-access.d.ts +34 -0
  81. package/dist/api/compiler/checker-property-access.js +672 -0
  82. package/dist/api/compiler/checker-services.d.ts +49 -0
  83. package/dist/api/compiler/checker-services.js +995 -0
  84. package/dist/api/compiler/checker-signatures.d.ts +55 -0
  85. package/dist/api/compiler/checker-signatures.js +788 -0
  86. package/dist/api/compiler/checker-source-file.d.ts +27 -0
  87. package/dist/api/compiler/checker-source-file.js +619 -0
  88. package/dist/api/compiler/checker-statements.d.ts +18 -0
  89. package/dist/api/compiler/checker-statements.js +226 -0
  90. package/dist/api/compiler/checker-super.d.ts +13 -0
  91. package/dist/api/compiler/checker-super.js +241 -0
  92. package/dist/api/compiler/checker-symbol-types.d.ts +67 -0
  93. package/dist/api/compiler/checker-symbol-types.js +1885 -0
  94. package/dist/api/compiler/checker-symbols.d.ts +181 -0
  95. package/dist/api/compiler/checker-symbols.js +1782 -0
  96. package/dist/api/compiler/checker-type-factory.d.ts +142 -0
  97. package/dist/api/compiler/checker-type-factory.js +2501 -0
  98. package/dist/api/compiler/checker-type-nodes.d.ts +26 -0
  99. package/dist/api/compiler/checker-type-nodes.js +828 -0
  100. package/dist/api/compiler/checker-type-operators.d.ts +109 -0
  101. package/dist/api/compiler/checker-type-operators.js +1842 -0
  102. package/dist/api/compiler/checker-types.d.ts +1027 -0
  103. package/dist/api/compiler/checker-types.js +1697 -0
  104. package/dist/api/compiler/checker-unused.d.ts +19 -0
  105. package/dist/api/compiler/checker-unused.js +524 -0
  106. package/dist/api/compiler/checker-utilities.d.ts +33 -0
  107. package/dist/api/compiler/checker-utilities.js +141 -0
  108. package/dist/api/compiler/checker.d.ts +491 -0
  109. package/dist/api/compiler/checker.js +912 -0
  110. package/dist/api/compiler/command-line.d.ts +32 -0
  111. package/dist/api/compiler/command-line.js +383 -0
  112. package/dist/api/compiler/compile-program.d.ts +118 -0
  113. package/dist/api/compiler/compile-program.js +384 -0
  114. package/dist/api/compiler/diagnostic-locales.d.ts +22 -0
  115. package/dist/api/compiler/diagnostic-locales.js +64 -0
  116. package/dist/api/compiler/diagnostic-messages.d.ts +2166 -0
  117. package/dist/api/compiler/diagnostic-messages.js +184 -0
  118. package/dist/api/compiler/diagnostics.d.ts +167 -0
  119. package/dist/api/compiler/diagnostics.js +820 -0
  120. package/dist/api/compiler/emitter.d.ts +71 -0
  121. package/dist/api/compiler/emitter.js +327 -0
  122. package/dist/api/compiler/file-include.d.ts +117 -0
  123. package/dist/api/compiler/file-include.js +536 -0
  124. package/dist/api/compiler/file-loader.d.ts +261 -0
  125. package/dist/api/compiler/file-loader.js +1257 -0
  126. package/dist/api/compiler/file-system.d.ts +51 -0
  127. package/dist/api/compiler/file-system.js +132 -0
  128. package/dist/api/compiler/flow.d.ts +60 -0
  129. package/dist/api/compiler/flow.js +45 -0
  130. package/dist/api/compiler/instantiation.d.ts +74 -0
  131. package/dist/api/compiler/instantiation.js +799 -0
  132. package/dist/api/compiler/jsdoc.d.ts +75 -0
  133. package/dist/api/compiler/jsdoc.js +1229 -0
  134. package/dist/api/compiler/link-tables.d.ts +36 -0
  135. package/dist/api/compiler/link-tables.js +83 -0
  136. package/dist/api/compiler/locale-data.d.ts +24 -0
  137. package/dist/api/compiler/locale-data.js +183 -0
  138. package/dist/api/compiler/locale.d.ts +4 -0
  139. package/dist/api/compiler/locale.js +317 -0
  140. package/dist/api/compiler/locales/cs-CZ.d.ts +2 -0
  141. package/dist/api/compiler/locales/cs-CZ.js +3 -0
  142. package/dist/api/compiler/locales/de-DE.d.ts +2 -0
  143. package/dist/api/compiler/locales/de-DE.js +3 -0
  144. package/dist/api/compiler/locales/es-ES.d.ts +2 -0
  145. package/dist/api/compiler/locales/es-ES.js +3 -0
  146. package/dist/api/compiler/locales/fr-FR.d.ts +2 -0
  147. package/dist/api/compiler/locales/fr-FR.js +3 -0
  148. package/dist/api/compiler/locales/it-IT.d.ts +2 -0
  149. package/dist/api/compiler/locales/it-IT.js +3 -0
  150. package/dist/api/compiler/locales/ja-JP.d.ts +2 -0
  151. package/dist/api/compiler/locales/ja-JP.js +3 -0
  152. package/dist/api/compiler/locales/ko-KR.d.ts +2 -0
  153. package/dist/api/compiler/locales/ko-KR.js +3 -0
  154. package/dist/api/compiler/locales/pl-PL.d.ts +2 -0
  155. package/dist/api/compiler/locales/pl-PL.js +3 -0
  156. package/dist/api/compiler/locales/pt-BR.d.ts +2 -0
  157. package/dist/api/compiler/locales/pt-BR.js +3 -0
  158. package/dist/api/compiler/locales/ru-RU.d.ts +2 -0
  159. package/dist/api/compiler/locales/ru-RU.js +3 -0
  160. package/dist/api/compiler/locales/tr-TR.d.ts +2 -0
  161. package/dist/api/compiler/locales/tr-TR.js +3 -0
  162. package/dist/api/compiler/locales/zh-CN.d.ts +2 -0
  163. package/dist/api/compiler/locales/zh-CN.js +3 -0
  164. package/dist/api/compiler/locales/zh-TW.d.ts +2 -0
  165. package/dist/api/compiler/locales/zh-TW.js +3 -0
  166. package/dist/api/compiler/module-resolution-types.d.ts +83 -0
  167. package/dist/api/compiler/module-resolution-types.js +176 -0
  168. package/dist/api/compiler/module-resolution.d.ts +71 -0
  169. package/dist/api/compiler/module-resolution.js +1598 -0
  170. package/dist/api/compiler/module-specifiers.d.ts +64 -0
  171. package/dist/api/compiler/module-specifiers.js +1101 -0
  172. package/dist/api/compiler/name-resolver.d.ts +43 -0
  173. package/dist/api/compiler/name-resolver.js +509 -0
  174. package/dist/api/compiler/node-builder-names.d.ts +32 -0
  175. package/dist/api/compiler/node-builder-names.js +854 -0
  176. package/dist/api/compiler/node-builder-pseudo.d.ts +36 -0
  177. package/dist/api/compiler/node-builder-pseudo.js +744 -0
  178. package/dist/api/compiler/node-builder-reuse.d.ts +4 -0
  179. package/dist/api/compiler/node-builder-reuse.js +647 -0
  180. package/dist/api/compiler/node-builder.d.ts +270 -0
  181. package/dist/api/compiler/node-builder.js +2645 -0
  182. package/dist/api/compiler/option-declarations.d.ts +90 -0
  183. package/dist/api/compiler/option-declarations.js +1460 -0
  184. package/dist/api/compiler/options.d.ts +289 -0
  185. package/dist/api/compiler/options.js +1197 -0
  186. package/dist/api/compiler/output-paths.d.ts +42 -0
  187. package/dist/api/compiler/output-paths.js +195 -0
  188. package/dist/api/compiler/package-json.d.ts +175 -0
  189. package/dist/api/compiler/package-json.js +1015 -0
  190. package/dist/api/compiler/parallel-parse.d.ts +219 -0
  191. package/dist/api/compiler/parallel-parse.js +537 -0
  192. package/dist/api/compiler/parser.d.ts +587 -0
  193. package/dist/api/compiler/parser.js +6407 -0
  194. package/dist/api/compiler/path.d.ts +91 -0
  195. package/dist/api/compiler/path.js +705 -0
  196. package/dist/api/compiler/printer/async-helpers.d.ts +22 -0
  197. package/dist/api/compiler/printer/async-helpers.js +91 -0
  198. package/dist/api/compiler/printer/auto-generate.d.ts +13 -0
  199. package/dist/api/compiler/printer/auto-generate.js +20 -0
  200. package/dist/api/compiler/printer/decorator-helpers.d.ts +24 -0
  201. package/dist/api/compiler/printer/decorator-helpers.js +118 -0
  202. package/dist/api/compiler/printer/emit-context.d.ts +154 -0
  203. package/dist/api/compiler/printer/emit-context.js +576 -0
  204. package/dist/api/compiler/printer/emit-flags.d.ts +60 -0
  205. package/dist/api/compiler/printer/emit-flags.js +2 -0
  206. package/dist/api/compiler/printer/factory-base.d.ts +171 -0
  207. package/dist/api/compiler/printer/factory-base.js +1505 -0
  208. package/dist/api/compiler/printer/factory.d.ts +147 -0
  209. package/dist/api/compiler/printer/factory.js +542 -0
  210. package/dist/api/compiler/printer/helpers.d.ts +27 -0
  211. package/dist/api/compiler/printer/helpers.js +164 -0
  212. package/dist/api/compiler/printer/name-generator.d.ts +41 -0
  213. package/dist/api/compiler/printer/name-generator.js +283 -0
  214. package/dist/api/compiler/printer/printer.d.ts +411 -0
  215. package/dist/api/compiler/printer/printer.js +4517 -0
  216. package/dist/api/compiler/printer/text-writer.d.ts +46 -0
  217. package/dist/api/compiler/printer/text-writer.js +172 -0
  218. package/dist/api/compiler/printer/utilities.d.ts +82 -0
  219. package/dist/api/compiler/printer/utilities.js +817 -0
  220. package/dist/api/compiler/program-checker.d.ts +156 -0
  221. package/dist/api/compiler/program-checker.js +526 -0
  222. package/dist/api/compiler/program-utilities.d.ts +52 -0
  223. package/dist/api/compiler/program-utilities.js +365 -0
  224. package/dist/api/compiler/program.d.ts +180 -0
  225. package/dist/api/compiler/program.js +1011 -0
  226. package/dist/api/compiler/pseudo-bigint.d.ts +5 -0
  227. package/dist/api/compiler/pseudo-bigint.js +61 -0
  228. package/dist/api/compiler/regexp-checker.d.ts +38 -0
  229. package/dist/api/compiler/regexp-checker.js +942 -0
  230. package/dist/api/compiler/relations.d.ts +263 -0
  231. package/dist/api/compiler/relations.js +4002 -0
  232. package/dist/api/compiler/reparser.d.ts +48 -0
  233. package/dist/api/compiler/reparser.js +1323 -0
  234. package/dist/api/compiler/scanner.d.ts +256 -0
  235. package/dist/api/compiler/scanner.js +2853 -0
  236. package/dist/api/compiler/side-state.d.ts +16 -0
  237. package/dist/api/compiler/side-state.js +89 -0
  238. package/dist/api/compiler/sourcemap.d.ts +55 -0
  239. package/dist/api/compiler/sourcemap.js +340 -0
  240. package/dist/api/compiler/spelling.d.ts +14 -0
  241. package/dist/api/compiler/spelling.js +128 -0
  242. package/dist/api/compiler/symbol-accessibility.d.ts +57 -0
  243. package/dist/api/compiler/symbol-accessibility.js +894 -0
  244. package/dist/api/compiler/symbol.d.ts +240 -0
  245. package/dist/api/compiler/symbol.js +228 -0
  246. package/dist/api/compiler/symlinks.d.ts +32 -0
  247. package/dist/api/compiler/symlinks.js +114 -0
  248. package/dist/api/compiler/syntax-kind.d.ts +389 -0
  249. package/dist/api/compiler/syntax-kind.js +2 -0
  250. package/dist/api/compiler/timeline.d.ts +9 -0
  251. package/dist/api/compiler/timeline.js +54 -0
  252. package/dist/api/compiler/transformers/alias-references.d.ts +39 -0
  253. package/dist/api/compiler/transformers/alias-references.js +676 -0
  254. package/dist/api/compiler/transformers/async-utilities.d.ts +51 -0
  255. package/dist/api/compiler/transformers/async-utilities.js +260 -0
  256. package/dist/api/compiler/transformers/async.d.ts +53 -0
  257. package/dist/api/compiler/transformers/async.js +645 -0
  258. package/dist/api/compiler/transformers/class-fields.d.ts +206 -0
  259. package/dist/api/compiler/transformers/class-fields.js +2418 -0
  260. package/dist/api/compiler/transformers/class-utilities.d.ts +46 -0
  261. package/dist/api/compiler/transformers/class-utilities.js +261 -0
  262. package/dist/api/compiler/transformers/commonjs-module.d.ts +103 -0
  263. package/dist/api/compiler/transformers/commonjs-module.js +1180 -0
  264. package/dist/api/compiler/transformers/const-enum-inliner.d.ts +11 -0
  265. package/dist/api/compiler/transformers/const-enum-inliner.js +36 -0
  266. package/dist/api/compiler/transformers/declarations-diagnostics.d.ts +52 -0
  267. package/dist/api/compiler/transformers/declarations-diagnostics.js +538 -0
  268. package/dist/api/compiler/transformers/declarations-pseudo.d.ts +160 -0
  269. package/dist/api/compiler/transformers/declarations-pseudo.js +797 -0
  270. package/dist/api/compiler/transformers/declarations-resolver.d.ts +152 -0
  271. package/dist/api/compiler/transformers/declarations-resolver.js +35 -0
  272. package/dist/api/compiler/transformers/declarations-syntactic.d.ts +119 -0
  273. package/dist/api/compiler/transformers/declarations-syntactic.js +1582 -0
  274. package/dist/api/compiler/transformers/declarations-tracker.d.ts +46 -0
  275. package/dist/api/compiler/transformers/declarations-tracker.js +197 -0
  276. package/dist/api/compiler/transformers/declarations.d.ts +175 -0
  277. package/dist/api/compiler/transformers/declarations.js +2380 -0
  278. package/dist/api/compiler/transformers/decorator-metadata.d.ts +54 -0
  279. package/dist/api/compiler/transformers/decorator-metadata.js +554 -0
  280. package/dist/api/compiler/transformers/decorator-utilities.d.ts +38 -0
  281. package/dist/api/compiler/transformers/decorator-utilities.js +238 -0
  282. package/dist/api/compiler/transformers/destructuring.d.ts +25 -0
  283. package/dist/api/compiler/transformers/destructuring.js +626 -0
  284. package/dist/api/compiler/transformers/downlevel.d.ts +6 -0
  285. package/dist/api/compiler/transformers/downlevel.js +45 -0
  286. package/dist/api/compiler/transformers/emit-resolver.d.ts +61 -0
  287. package/dist/api/compiler/transformers/emit-resolver.js +197 -0
  288. package/dist/api/compiler/transformers/es-decorators.d.ts +118 -0
  289. package/dist/api/compiler/transformers/es-decorators.js +1901 -0
  290. package/dist/api/compiler/transformers/exponentiation.d.ts +11 -0
  291. package/dist/api/compiler/transformers/exponentiation.js +76 -0
  292. package/dist/api/compiler/transformers/external-module-info.d.ts +35 -0
  293. package/dist/api/compiler/transformers/external-module-info.js +351 -0
  294. package/dist/api/compiler/transformers/for-await.d.ts +43 -0
  295. package/dist/api/compiler/transformers/for-await.js +469 -0
  296. package/dist/api/compiler/transformers/import-elision.d.ts +16 -0
  297. package/dist/api/compiler/transformers/import-elision.js +124 -0
  298. package/dist/api/compiler/transformers/jsx-entities.d.ts +1 -0
  299. package/dist/api/compiler/transformers/jsx-entities.js +256 -0
  300. package/dist/api/compiler/transformers/jsx.d.ts +71 -0
  301. package/dist/api/compiler/transformers/jsx.js +700 -0
  302. package/dist/api/compiler/transformers/legacy-decorators.d.ts +56 -0
  303. package/dist/api/compiler/transformers/legacy-decorators.js +564 -0
  304. package/dist/api/compiler/transformers/logical-assignment.d.ts +14 -0
  305. package/dist/api/compiler/transformers/logical-assignment.js +62 -0
  306. package/dist/api/compiler/transformers/module-utilities.d.ts +9 -0
  307. package/dist/api/compiler/transformers/module-utilities.js +36 -0
  308. package/dist/api/compiler/transformers/module.d.ts +49 -0
  309. package/dist/api/compiler/transformers/module.js +296 -0
  310. package/dist/api/compiler/transformers/named-evaluation.d.ts +26 -0
  311. package/dist/api/compiler/transformers/named-evaluation.js +266 -0
  312. package/dist/api/compiler/transformers/nullish-coalescing.d.ts +9 -0
  313. package/dist/api/compiler/transformers/nullish-coalescing.js +26 -0
  314. package/dist/api/compiler/transformers/object-rest-spread.d.ts +34 -0
  315. package/dist/api/compiler/transformers/object-rest-spread.js +411 -0
  316. package/dist/api/compiler/transformers/optional-catch.d.ts +9 -0
  317. package/dist/api/compiler/transformers/optional-catch.js +20 -0
  318. package/dist/api/compiler/transformers/optional-chain.d.ts +14 -0
  319. package/dist/api/compiler/transformers/optional-chain.js +188 -0
  320. package/dist/api/compiler/transformers/reference-resolver.d.ts +128 -0
  321. package/dist/api/compiler/transformers/reference-resolver.js +1155 -0
  322. package/dist/api/compiler/transformers/runtime-syntax.d.ts +71 -0
  323. package/dist/api/compiler/transformers/runtime-syntax.js +753 -0
  324. package/dist/api/compiler/transformers/subtree-facts.d.ts +9 -0
  325. package/dist/api/compiler/transformers/subtree-facts.js +203 -0
  326. package/dist/api/compiler/transformers/syntax-facts.d.ts +51 -0
  327. package/dist/api/compiler/transformers/syntax-facts.js +254 -0
  328. package/dist/api/compiler/transformers/tagged-template.d.ts +17 -0
  329. package/dist/api/compiler/transformers/tagged-template.js +148 -0
  330. package/dist/api/compiler/transformers/transformer.d.ts +17 -0
  331. package/dist/api/compiler/transformers/transformer.js +54 -0
  332. package/dist/api/compiler/transformers/type-eraser.d.ts +21 -0
  333. package/dist/api/compiler/transformers/type-eraser.js +346 -0
  334. package/dist/api/compiler/transformers/type-serialization.d.ts +20 -0
  335. package/dist/api/compiler/transformers/type-serialization.js +347 -0
  336. package/dist/api/compiler/transformers/using.d.ts +56 -0
  337. package/dist/api/compiler/transformers/using.js +453 -0
  338. package/dist/api/compiler/transformers/utilities.d.ts +50 -0
  339. package/dist/api/compiler/transformers/utilities.js +347 -0
  340. package/dist/api/compiler/transformers/visitor-base.d.ts +6 -0
  341. package/dist/api/compiler/transformers/visitor-base.js +817 -0
  342. package/dist/api/compiler/transformers/visitor-hooks.d.ts +29 -0
  343. package/dist/api/compiler/transformers/visitor-hooks.js +189 -0
  344. package/dist/api/compiler/tsconfig.d.ts +177 -0
  345. package/dist/api/compiler/tsconfig.js +1311 -0
  346. package/dist/api/compiler/type-printer.d.ts +15 -0
  347. package/dist/api/compiler/type-printer.js +74 -0
  348. package/dist/api/compiler/types.d.ts +87 -0
  349. package/dist/api/compiler/types.js +23 -0
  350. package/dist/api/compiler/unicode-identifiers.d.ts +2 -0
  351. package/dist/api/compiler/unicode-identifiers.js +209 -0
  352. package/dist/api/compiler/unicode-properties.d.ts +8 -0
  353. package/dist/api/compiler/unicode-properties.js +531 -0
  354. package/dist/api/compiler/vfs-match.d.ts +24 -0
  355. package/dist/api/compiler/vfs-match.js +587 -0
  356. package/dist/api/library/ast.d.ts +74 -0
  357. package/dist/api/library/ast.generated.d.ts +1145 -0
  358. package/dist/api/library/ast.generated.js +3 -0
  359. package/dist/api/library/ast.js +5 -0
  360. package/dist/api/library/checker.d.ts +51 -0
  361. package/dist/api/library/checker.js +318 -0
  362. package/dist/api/library/compiler-options.generated.d.ts +108 -0
  363. package/dist/api/library/compiler-options.generated.js +3 -0
  364. package/dist/api/library/diagnostics.d.ts +13 -0
  365. package/dist/api/library/diagnostics.js +50 -0
  366. package/dist/api/library/disk.d.ts +13 -0
  367. package/dist/api/library/disk.js +66 -0
  368. package/dist/api/library/enums.generated.d.ts +891 -0
  369. package/dist/api/library/enums.generated.js +916 -0
  370. package/dist/api/library/errors.d.ts +13 -0
  371. package/dist/api/library/errors.js +24 -0
  372. package/dist/api/library/host.d.ts +24 -0
  373. package/dist/api/library/host.js +52 -0
  374. package/dist/api/library/index.d.ts +14 -0
  375. package/dist/api/library/index.js +15 -0
  376. package/dist/api/library/nodes.d.ts +51 -0
  377. package/dist/api/library/nodes.js +196 -0
  378. package/dist/api/library/options.d.ts +144 -0
  379. package/dist/api/library/options.js +308 -0
  380. package/dist/api/library/parse.d.ts +19 -0
  381. package/dist/api/library/parse.js +41 -0
  382. package/dist/api/library/program.d.ts +21 -0
  383. package/dist/api/library/program.js +234 -0
  384. package/dist/api/library/symbols.d.ts +8 -0
  385. package/dist/api/library/symbols.js +11 -0
  386. package/dist/api/library/types.d.ts +313 -0
  387. package/dist/api/library/types.js +6 -0
  388. package/package.json +22 -8
  389. package/src/compiler/ast-utilities.ts +548 -0
  390. package/src/compiler/ast.ts +12862 -0
  391. package/src/compiler/astnav.ts +799 -0
  392. package/src/compiler/binder-utilities.ts +1392 -0
  393. package/src/compiler/binder.ts +2703 -0
  394. package/src/compiler/build-info.ts +1197 -0
  395. package/src/compiler/character-codes.ts +323 -0
  396. package/src/compiler/checker-calls.ts +2155 -0
  397. package/src/compiler/checker-classes.ts +706 -0
  398. package/src/compiler/checker-contextual.ts +1457 -0
  399. package/src/compiler/checker-declaration-order.ts +438 -0
  400. package/src/compiler/checker-declarations.ts +1194 -0
  401. package/src/compiler/checker-declared-types.ts +919 -0
  402. package/src/compiler/checker-decorators.ts +694 -0
  403. package/src/compiler/checker-deprecation.ts +359 -0
  404. package/src/compiler/checker-destructuring.ts +805 -0
  405. package/src/compiler/checker-elaboration.ts +477 -0
  406. package/src/compiler/checker-emit-helpers.ts +224 -0
  407. package/src/compiler/checker-emit-resolver.ts +769 -0
  408. package/src/compiler/checker-evaluator.ts +243 -0
  409. package/src/compiler/checker-expressions.ts +1831 -0
  410. package/src/compiler/checker-features.ts +361 -0
  411. package/src/compiler/checker-flow.ts +394 -0
  412. package/src/compiler/checker-functions.ts +1296 -0
  413. package/src/compiler/checker-generated-code.ts +227 -0
  414. package/src/compiler/checker-globals.ts +52 -0
  415. package/src/compiler/checker-grammar.ts +3584 -0
  416. package/src/compiler/checker-heritage.ts +1233 -0
  417. package/src/compiler/checker-inference.ts +2576 -0
  418. package/src/compiler/checker-iteration.ts +1206 -0
  419. package/src/compiler/checker-javascript.ts +502 -0
  420. package/src/compiler/checker-jsdoc.ts +360 -0
  421. package/src/compiler/checker-jsx.ts +1924 -0
  422. package/src/compiler/checker-literals.ts +61 -0
  423. package/src/compiler/checker-members.ts +2436 -0
  424. package/src/compiler/checker-modules.ts +2886 -0
  425. package/src/compiler/checker-narrowing.ts +3230 -0
  426. package/src/compiler/checker-object-literals.ts +575 -0
  427. package/src/compiler/checker-operators.ts +1422 -0
  428. package/src/compiler/checker-overloads.ts +328 -0
  429. package/src/compiler/checker-property-access.ts +906 -0
  430. package/src/compiler/checker-services.ts +1149 -0
  431. package/src/compiler/checker-signatures.ts +1032 -0
  432. package/src/compiler/checker-source-file.ts +732 -0
  433. package/src/compiler/checker-statements.ts +292 -0
  434. package/src/compiler/checker-super.ts +261 -0
  435. package/src/compiler/checker-symbol-types.ts +2107 -0
  436. package/src/compiler/checker-symbols.ts +2150 -0
  437. package/src/compiler/checker-type-factory.ts +2579 -0
  438. package/src/compiler/checker-type-nodes.ts +1033 -0
  439. package/src/compiler/checker-type-operators.ts +2502 -0
  440. package/src/compiler/checker-types.ts +2340 -0
  441. package/src/compiler/checker-unused.ts +642 -0
  442. package/src/compiler/checker-utilities.ts +161 -0
  443. package/src/compiler/checker.ts +1116 -0
  444. package/src/compiler/command-line.ts +436 -0
  445. package/src/compiler/compile-program.ts +439 -0
  446. package/src/compiler/decoder.ts +1209 -0
  447. package/src/compiler/diagnostic-locales.ts +86 -0
  448. package/src/compiler/diagnostic-messages.ts +2349 -0
  449. package/src/compiler/diagnostics.ts +897 -0
  450. package/src/compiler/emitter.ts +434 -0
  451. package/src/compiler/encoder.ts +1945 -0
  452. package/src/compiler/file-include.ts +673 -0
  453. package/src/compiler/file-loader.ts +1528 -0
  454. package/src/compiler/file-system.ts +143 -0
  455. package/src/compiler/flow.ts +89 -0
  456. package/src/compiler/help.ts +473 -0
  457. package/src/compiler/incremental.ts +2784 -0
  458. package/src/compiler/init.ts +141 -0
  459. package/src/compiler/instantiation.ts +894 -0
  460. package/src/compiler/jsdoc.ts +1386 -0
  461. package/src/compiler/link-tables.ts +98 -0
  462. package/src/compiler/locale-data.ts +203 -0
  463. package/src/compiler/locale.ts +307 -0
  464. package/src/compiler/locales/cs-CZ.ts +5 -0
  465. package/src/compiler/locales/de-DE.ts +5 -0
  466. package/src/compiler/locales/es-ES.ts +5 -0
  467. package/src/compiler/locales/fr-FR.ts +5 -0
  468. package/src/compiler/locales/it-IT.ts +5 -0
  469. package/src/compiler/locales/ja-JP.ts +5 -0
  470. package/src/compiler/locales/ko-KR.ts +5 -0
  471. package/src/compiler/locales/pl-PL.ts +5 -0
  472. package/src/compiler/locales/pt-BR.ts +5 -0
  473. package/src/compiler/locales/ru-RU.ts +5 -0
  474. package/src/compiler/locales/tr-TR.ts +5 -0
  475. package/src/compiler/locales/zh-CN.ts +5 -0
  476. package/src/compiler/locales/zh-TW.ts +5 -0
  477. package/src/compiler/module-resolution-types.ts +243 -0
  478. package/src/compiler/module-resolution.ts +2072 -0
  479. package/src/compiler/module-specifiers.ts +1491 -0
  480. package/src/compiler/name-resolver.ts +646 -0
  481. package/src/compiler/node-builder-names.ts +1069 -0
  482. package/src/compiler/node-builder-pseudo.ts +892 -0
  483. package/src/compiler/node-builder-reuse.ts +764 -0
  484. package/src/compiler/node-builder.ts +3425 -0
  485. package/src/compiler/option-declarations.ts +1560 -0
  486. package/src/compiler/options.ts +1576 -0
  487. package/src/compiler/output-paths.ts +334 -0
  488. package/src/compiler/package-json.ts +1083 -0
  489. package/src/compiler/parallel-check.ts +1951 -0
  490. package/src/compiler/parallel-parse.ts +590 -0
  491. package/src/compiler/parse-thread.ts +140 -0
  492. package/src/compiler/parser.ts +7716 -0
  493. package/src/compiler/path.ts +717 -0
  494. package/src/compiler/printer/async-helpers.ts +183 -0
  495. package/src/compiler/printer/auto-generate.ts +27 -0
  496. package/src/compiler/printer/decorator-helpers.ts +218 -0
  497. package/src/compiler/printer/emit-context.ts +679 -0
  498. package/src/compiler/printer/emit-flags.ts +64 -0
  499. package/src/compiler/printer/factory-base.ts +2577 -0
  500. package/src/compiler/printer/factory.ts +764 -0
  501. package/src/compiler/printer/helpers.ts +291 -0
  502. package/src/compiler/printer/name-generator.ts +372 -0
  503. package/src/compiler/printer/printer.ts +5072 -0
  504. package/src/compiler/printer/text-writer.ts +183 -0
  505. package/src/compiler/printer/utilities.ts +884 -0
  506. package/src/compiler/program-checker.ts +592 -0
  507. package/src/compiler/program-utilities.ts +435 -0
  508. package/src/compiler/program.ts +1419 -0
  509. package/src/compiler/pseudo-bigint.ts +62 -0
  510. package/src/compiler/regexp-checker.ts +1142 -0
  511. package/src/compiler/relations.ts +5149 -0
  512. package/src/compiler/reparser.ts +1566 -0
  513. package/src/compiler/scanner.ts +2951 -0
  514. package/src/compiler/show-config.ts +239 -0
  515. package/src/compiler/side-state.ts +125 -0
  516. package/src/compiler/sourcemap.ts +366 -0
  517. package/src/compiler/spelling.ts +122 -0
  518. package/src/compiler/symbol-accessibility.ts +1071 -0
  519. package/src/compiler/symbol.ts +439 -0
  520. package/src/compiler/symlinks.ts +137 -0
  521. package/src/compiler/syntax-kind.ts +391 -0
  522. package/src/compiler/thread-entry.ts +191 -0
  523. package/src/compiler/timeline.ts +53 -0
  524. package/src/compiler/transformers/alias-references.ts +757 -0
  525. package/src/compiler/transformers/async-utilities.ts +335 -0
  526. package/src/compiler/transformers/async.ts +838 -0
  527. package/src/compiler/transformers/class-fields.ts +2967 -0
  528. package/src/compiler/transformers/class-utilities.ts +324 -0
  529. package/src/compiler/transformers/commonjs-module.ts +1621 -0
  530. package/src/compiler/transformers/const-enum-inliner.ts +50 -0
  531. package/src/compiler/transformers/declarations-diagnostics.ts +832 -0
  532. package/src/compiler/transformers/declarations-pseudo.ts +966 -0
  533. package/src/compiler/transformers/declarations-resolver.ts +249 -0
  534. package/src/compiler/transformers/declarations-syntactic.ts +1859 -0
  535. package/src/compiler/transformers/declarations-tracker.ts +269 -0
  536. package/src/compiler/transformers/declarations.ts +3120 -0
  537. package/src/compiler/transformers/decorator-metadata.ts +697 -0
  538. package/src/compiler/transformers/decorator-utilities.ts +283 -0
  539. package/src/compiler/transformers/destructuring.ts +708 -0
  540. package/src/compiler/transformers/downlevel.ts +50 -0
  541. package/src/compiler/transformers/emit-resolver.ts +258 -0
  542. package/src/compiler/transformers/es-decorators.ts +2682 -0
  543. package/src/compiler/transformers/exponentiation.ts +80 -0
  544. package/src/compiler/transformers/external-module-info.ts +412 -0
  545. package/src/compiler/transformers/for-await.ts +687 -0
  546. package/src/compiler/transformers/import-elision.ts +152 -0
  547. package/src/compiler/transformers/jsx-entities.ts +256 -0
  548. package/src/compiler/transformers/jsx.ts +824 -0
  549. package/src/compiler/transformers/legacy-decorators.ts +726 -0
  550. package/src/compiler/transformers/logical-assignment.ts +75 -0
  551. package/src/compiler/transformers/module-utilities.ts +42 -0
  552. package/src/compiler/transformers/module.ts +474 -0
  553. package/src/compiler/transformers/named-evaluation.ts +389 -0
  554. package/src/compiler/transformers/nullish-coalescing.ts +35 -0
  555. package/src/compiler/transformers/object-rest-spread.ts +577 -0
  556. package/src/compiler/transformers/optional-catch.ts +29 -0
  557. package/src/compiler/transformers/optional-chain.ts +246 -0
  558. package/src/compiler/transformers/reference-resolver.ts +1201 -0
  559. package/src/compiler/transformers/runtime-syntax.ts +942 -0
  560. package/src/compiler/transformers/subtree-facts.ts +245 -0
  561. package/src/compiler/transformers/syntax-facts.ts +353 -0
  562. package/src/compiler/transformers/tagged-template.ts +167 -0
  563. package/src/compiler/transformers/transformer.ts +56 -0
  564. package/src/compiler/transformers/type-eraser.ts +520 -0
  565. package/src/compiler/transformers/type-serialization.ts +374 -0
  566. package/src/compiler/transformers/using.ts +613 -0
  567. package/src/compiler/transformers/utilities.ts +419 -0
  568. package/src/compiler/transformers/visitor-base.ts +1601 -0
  569. package/src/compiler/transformers/visitor-hooks.ts +252 -0
  570. package/src/compiler/tsc-build.ts +1418 -0
  571. package/src/compiler/tsc-cli.ts +666 -0
  572. package/src/compiler/tsc-compile.ts +437 -0
  573. package/src/compiler/tsc-watch.ts +854 -0
  574. package/src/compiler/tsconfig.ts +1574 -0
  575. package/src/compiler/type-printer.ts +110 -0
  576. package/src/compiler/types.ts +115 -0
  577. package/src/compiler/unicode-identifiers.ts +212 -0
  578. package/src/compiler/unicode-properties.ts +537 -0
  579. package/src/compiler/vfs-match.ts +597 -0
  580. package/src/compiler/xxh3.ts +650 -0
  581. package/src/library/ast.generated.ts +1685 -0
  582. package/src/library/ast.ts +104 -0
  583. package/src/library/checker.ts +384 -0
  584. package/src/library/compiler-options.generated.ts +114 -0
  585. package/src/library/diagnostics.ts +72 -0
  586. package/src/library/disk.ts +73 -0
  587. package/src/library/enums.generated.ts +1024 -0
  588. package/src/library/errors.ts +26 -0
  589. package/src/library/host.ts +63 -0
  590. package/src/library/index.ts +71 -0
  591. package/src/library/nodes.ts +232 -0
  592. package/src/library/options.ts +449 -0
  593. package/src/library/parse.ts +63 -0
  594. package/src/library/program.ts +293 -0
  595. package/src/library/symbols.ts +15 -0
  596. package/src/library/types.ts +359 -0
@@ -0,0 +1,2107 @@
1
+ // Types of value symbols: variables, parameters, properties, accessors, functions, classes,
2
+ // enums, modules and aliases; optionality and widening.
3
+ import { checkJsxAttribute } from './checker-jsx.js';
4
+ import { getTargetOfAliasDeclaration, resolveExternalModuleSymbol } from './checker-modules.js';
5
+ import { CachedTypeKind, cachedTypeKey, CheckMode } from './checker-types.js';
6
+ import {
7
+ type ArrayLiteralExpression,
8
+ type BindingElement,
9
+ type BindingPattern,
10
+ type BinaryExpression,
11
+ type Block,
12
+ type ConditionalExpression,
13
+ type ForInOrOfStatement,
14
+ ModifierFlags,
15
+ NodeVisitor,
16
+ type ObjectLiteralExpression,
17
+ type PrefixUnaryExpression,
18
+ type Node,
19
+ NodeFlags,
20
+ getArguments,
21
+ getInitializer,
22
+ getName,
23
+ getParameters,
24
+ getType,
25
+ getBody,
26
+ getSymbol,
27
+ getMembers,
28
+ getDotDotDotToken,
29
+ getElements,
30
+ getExpression,
31
+ getLocals,
32
+ getPostfixToken,
33
+ getQuestionToken,
34
+ getText,
35
+ type PropertyAssignment,
36
+ type ShorthandPropertyAssignment,
37
+ type SourceFile,
38
+ type VariableDeclarationList,
39
+ } from './ast.js';
40
+ import {
41
+ declarationNameToString,
42
+ findConstructorDeclaration,
43
+ getCombinedModifierFlags,
44
+ getCombinedNodeFlags,
45
+ getNameOfDeclaration,
46
+ getRootDeclaration,
47
+ getSourceFileOfNode,
48
+ hasStaticModifier,
49
+ hasSyntacticModifier,
50
+ isCatchClauseVariableDeclarationOrBindingElement,
51
+ isExternalModule,
52
+ isJsonSourceFile,
53
+ isTypeNodeKind,
54
+ skipParentheses,
55
+ } from './binder-utilities.js';
56
+ import { identifierToKeywordKind } from './scanner.js';
57
+ import { tsgoCheckerSlots } from './symbol-accessibility.js';
58
+ import { type DiagnosticMessage, Diagnostics } from './diagnostics.js';
59
+ import { CheckFlags, type Symbol, SymbolFlags, getSymbolId } from './symbol.js';
60
+ import { SyntaxKind } from './syntax-kind.js';
61
+ import { type Checker, createDiagnosticForNode } from './checker.js';
62
+ import {
63
+ type InterfaceType,
64
+ ObjectFlags,
65
+ type Type,
66
+ TypeFlags,
67
+ type UnionType,
68
+ IndexInfo,
69
+ TypeReference,
70
+ } from './checker-types.js';
71
+ import {
72
+ UnionReduction,
73
+ createAnonymousType,
74
+ createTypeReference,
75
+ getIntersectionType,
76
+ getUnionType,
77
+ getUnionTypeEx,
78
+ getWidenedLiteralType,
79
+ isArrayOrTupleType,
80
+ isEmptyObjectType,
81
+ newObjectType,
82
+ removeType,
83
+ filterType,
84
+ typesOf,
85
+ getTypeArguments,
86
+ } from './checker-type-factory.js';
87
+ import {
88
+ getDeclarationOfAliasSymbol,
89
+ getParentOfSymbol,
90
+ getSymbolFlags,
91
+ getSymbolOfDeclaration,
92
+ popTypeResolution,
93
+ pushTypeResolution,
94
+ reportCircularityError,
95
+ resolveAlias,
96
+ getMergedSymbol,
97
+ TypeSystemPropertyName,
98
+ getGlobalTypeAliasSymbol,
99
+ resolveName,
100
+ getGlobalSymbol,
101
+ } from './checker-symbols.js';
102
+ import {
103
+ getBaseConstructorTypeOfClass,
104
+ getDeclaredTypeOfClassOrInterface,
105
+ getDeclaredTypeOfEnumMember,
106
+ getDeclaredTypeOfSymbol,
107
+ } from './checker-declared-types.js';
108
+ import { getESSymbolLikeTypeForNode, getTypeFromTypeNode, getTypeAliasInstantiation } from './checker-type-nodes.js';
109
+ import {
110
+ getFlowTypeInConstructor,
111
+ getFlowTypeInStaticBlocks,
112
+ getTypeOfPropertyInBaseClass,
113
+ } from './checker-classes.js';
114
+ import { instantiateType } from './instantiation.js';
115
+ import {
116
+ getIndexInfosOfType,
117
+ getPropertiesOfObjectType,
118
+ getPropertiesOfType,
119
+ getPropertyOfObjectType,
120
+ isObjectLiteralType,
121
+ } from './checker-members.js';
122
+ import { typeToString, symbolToString } from './type-printer.js';
123
+ import { getTypeOfMappedSymbol, getIndexType } from './checker-type-operators.js';
124
+ import {
125
+ getReturnTypeOfSignature,
126
+ getSignatureFromDeclaration,
127
+ getTypeOfParameter as getTypeOfParameterOfSignature,
128
+ } from './checker-signatures.js';
129
+ import {
130
+ getReturnTypeFromBody,
131
+ getTypeOfObjectLiteralMethod,
132
+ ReturnCollector,
133
+ shouldReportErrorsFromWideningWithContextualSignature,
134
+ } from './checker-functions.js';
135
+ import { getTypeOfReverseMappedSymbol } from './checker-inference.js';
136
+ import {
137
+ checkDeclarationInitializer,
138
+ getContextuallyTypedParameterType,
139
+ getContextualThisParameterType,
140
+ checkExpressionCached,
141
+ checkExpressionEx,
142
+ isConstTypeReference,
143
+ } from './checker-expressions.js';
144
+ import { checkPropertyAssignment, checkShorthandPropertyAssignment } from './checker-object-literals.js';
145
+ import { getTypeForBindingElement, getTypeFromBindingPattern } from './checker-destructuring.js';
146
+ import { checkRightHandSideOfForOf } from './checker-statements.js';
147
+ import { getTypeWithFacts, TypeFacts, hasTypeFacts, getNonNullableType } from './checker-flow.js';
148
+ import { getTypeOfModuleExportsSymbol, getWidenedTypeForAssignmentDeclaration } from './checker-javascript.js';
149
+
150
+ export function getTypeOfSymbol(c: Checker, symbol: Symbol): Type {
151
+ const checkFlags = symbol.checkFlags;
152
+ if ((checkFlags & CheckFlags.DeferredType) !== 0) return getTypeOfSymbolWithDeferredType(c, symbol);
153
+ if ((checkFlags & CheckFlags.Instantiated) !== 0) return getTypeOfInstantiatedSymbol(c, symbol);
154
+ if ((checkFlags & CheckFlags.Mapped) !== 0) return getTypeOfMappedSymbol(c, symbol);
155
+ if ((checkFlags & CheckFlags.ReverseMapped) !== 0) return getTypeOfReverseMappedSymbol(c, symbol);
156
+ const flags = symbol.flags;
157
+ if ((flags & SymbolFlags.Accessor) !== 0) return getTypeOfAccessors(c, symbol);
158
+ if ((flags & (SymbolFlags.Variable | SymbolFlags.Property)) !== 0)
159
+ return getTypeOfVariableOrParameterOrProperty(c, symbol);
160
+ if (
161
+ (flags &
162
+ (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) !==
163
+ 0
164
+ )
165
+ return getTypeOfFuncClassEnumModule(c, symbol);
166
+ if ((flags & SymbolFlags.EnumMember) !== 0) return getTypeOfEnumMember(c, symbol);
167
+ if ((flags & SymbolFlags.Alias) !== 0) return getTypeOfAlias(c, symbol);
168
+ return c.errorType;
169
+ }
170
+
171
+ export function getTypeOfParameter(c: Checker, symbol: Symbol): Type {
172
+ return getTypeOfParameterOfSignature(c, symbol);
173
+ }
174
+
175
+ export function getNonMissingTypeOfSymbol(c: Checker, symbol: Symbol): Type {
176
+ return removeMissingType(c, getTypeOfSymbol(c, symbol), (symbol.flags & SymbolFlags.Optional) !== 0);
177
+ }
178
+
179
+ function getTypeOfSymbolWithDeferredType(c: Checker, symbol: Symbol): Type {
180
+ const links = c.symbolLinks(symbol);
181
+ const id = symbol.id;
182
+ if (c.symbolTypes.get(id) === undefined) {
183
+ const constituents = links.deferralConstituents!;
184
+ c.symbolTypes.set(
185
+ id,
186
+ (links.deferralParent!.flags & TypeFlags.Union) !== 0
187
+ ? getUnionType(c, constituents)
188
+ : getIntersectionType(c, constituents),
189
+ );
190
+ }
191
+ return c.symbolTypes.get(id)!;
192
+ }
193
+
194
+ function getWriteTypeOfSymbolWithDeferredType(c: Checker, symbol: Symbol): Type {
195
+ const links = c.symbolLinks(symbol);
196
+ if (links.writeType === undefined) {
197
+ const constituents = links.deferralWriteConstituents;
198
+ if (constituents === undefined) links.writeType = getTypeOfSymbolWithDeferredType(c, symbol);
199
+ else
200
+ links.writeType =
201
+ (links.deferralParent!.flags & TypeFlags.Union) !== 0
202
+ ? getUnionType(c, constituents)
203
+ : getIntersectionType(c, constituents);
204
+ }
205
+ return links.writeType;
206
+ }
207
+
208
+ function getTypeOfInstantiatedSymbol(c: Checker, symbol: Symbol): Type {
209
+ const id = getSymbolId(symbol);
210
+ let type = c.symbolTypes.get(id);
211
+ if (type === undefined) {
212
+ type = instantiateType(c, getTypeOfSymbol(c, c.symbolTargets.get(id)!), c.symbolMappers.get(id));
213
+ c.symbolTypes.set(id, type);
214
+ }
215
+ return type;
216
+ }
217
+
218
+ export function getWriteTypeOfSymbol(c: Checker, symbol: Symbol): Type {
219
+ if ((symbol.checkFlags & CheckFlags.SyntheticProperty) !== 0) {
220
+ if ((symbol.checkFlags & CheckFlags.DeferredType) !== 0) return getWriteTypeOfSymbolWithDeferredType(c, symbol);
221
+ const id = getSymbolId(symbol);
222
+ return c.symbolLinkTable.get(id)?.writeType ?? c.symbolTypes.get(id) ?? getTypeOfSymbol(c, symbol);
223
+ }
224
+ if ((symbol.flags & SymbolFlags.Property) !== 0)
225
+ return removeMissingType(c, getTypeOfSymbol(c, symbol), (symbol.flags & SymbolFlags.Optional) !== 0);
226
+ if ((symbol.flags & SymbolFlags.Accessor) !== 0) {
227
+ if ((symbol.checkFlags & CheckFlags.Instantiated) !== 0) {
228
+ const links = c.symbolLinks(symbol);
229
+ const symbolId = symbol.id;
230
+ if (links.writeType === undefined)
231
+ links.writeType = instantiateType(
232
+ c,
233
+ getWriteTypeOfSymbol(c, c.symbolTargets.get(symbolId)!),
234
+ c.symbolMappers.get(symbolId),
235
+ );
236
+ return links.writeType;
237
+ }
238
+ return getWriteTypeOfAccessors(c, symbol);
239
+ }
240
+ return getTypeOfSymbol(c, symbol);
241
+ }
242
+
243
+ // ---- Variables, parameters and properties
244
+
245
+ function getTypeOfVariableOrParameterOrProperty(c: Checker, symbol: Symbol): Type {
246
+ const id = getSymbolId(symbol);
247
+ const cached = c.symbolTypes.get(id);
248
+ if (cached !== undefined) return cached;
249
+ const t = getTypeOfVariableOrParameterOrPropertyWorker(c, symbol);
250
+ // Contextually typed parameters may get their type later; don't cache theirs.
251
+ if (c.symbolTypes.get(id) === undefined && !isParameterOfContextSensitiveSignature(c, symbol))
252
+ c.symbolTypes.set(id, t);
253
+ return t;
254
+ }
255
+
256
+ function isParameterOfContextSensitiveSignature(c: Checker, symbol: Symbol): boolean {
257
+ let decl = symbol.valueDeclaration;
258
+ if (decl === undefined) return false;
259
+ if (decl.kind === SyntaxKind.BindingElement) decl = getRootDeclaration(decl);
260
+ return decl.kind === SyntaxKind.Parameter && isContextSensitiveFunctionLikeLazy(c, decl.parent!);
261
+ }
262
+
263
+ import { isContextSensitiveFunctionOrObjectLiteralMethod as isContextSensitiveFunctionLikeLazy } from './checker-expressions.js';
264
+
265
+ function getTypeOfVariableOrParameterOrPropertyWorker(c: Checker, symbol: Symbol): Type {
266
+ if ((symbol.flags & SymbolFlags.Prototype) !== 0) return getTypeOfPrototypeProperty(c, symbol);
267
+ if (symbol === c.requireSymbol) return c.anyType;
268
+ const declaration = symbol.valueDeclaration;
269
+ if (declaration === undefined) return c.errorType;
270
+ // The `export=` symbol of a JSON file has the type of its value.
271
+ if (declaration.kind === SyntaxKind.SourceFile && isJsonSourceFile(declaration as SourceFile)) {
272
+ const statements = (declaration as SourceFile).statements.nodes;
273
+ if (statements.length === 0) return c.emptyObjectType;
274
+ return getWidenedType(c, getWidenedLiteralType(c, checkExpression(c, getExpression(statements[0]!)!)));
275
+ }
276
+ if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.Type)) return reportCircularityError(c, symbol);
277
+ // Like tsgo, the type resolution stays pushed: until popped by an enclosing resolution, its cached
278
+ // type ends cycle detection there, so aliases like `module.exports.b = module.exports.a` resolve.
279
+ if ((symbol.flags & SymbolFlags.ModuleExports) !== 0) return getTypeOfModuleExportsSymbol(c, symbol);
280
+ let result: Type;
281
+ switch (declaration.kind) {
282
+ case SyntaxKind.Parameter:
283
+ case SyntaxKind.PropertyDeclaration:
284
+ case SyntaxKind.PropertySignature:
285
+ case SyntaxKind.VariableDeclaration:
286
+ case SyntaxKind.BindingElement:
287
+ result = getWidenedTypeForVariableLikeDeclaration(c, declaration, true);
288
+ break;
289
+ case SyntaxKind.PropertyAssignment:
290
+ case SyntaxKind.ShorthandPropertyAssignment:
291
+ case SyntaxKind.MethodDeclaration:
292
+ // The declared symbol of an object literal member, as opposed to the checked literal's property.
293
+ if (declaration.kind === SyntaxKind.PropertyAssignment)
294
+ result = checkPropertyAssignment(c, declaration as PropertyAssignment, CheckMode.Normal);
295
+ else if (declaration.kind === SyntaxKind.ShorthandPropertyAssignment)
296
+ result = checkShorthandPropertyAssignment(
297
+ c,
298
+ declaration as ShorthandPropertyAssignment,
299
+ true,
300
+ CheckMode.Normal,
301
+ );
302
+ else result = getTypeOfObjectLiteralMethod(c, declaration, CheckMode.Normal);
303
+ break;
304
+ case SyntaxKind.JsxAttribute:
305
+ result = checkJsxAttribute(c, declaration, CheckMode.Normal);
306
+ break;
307
+ case SyntaxKind.ExportAssignment: {
308
+ const typeNode = getType(declaration);
309
+ result =
310
+ typeNode !== undefined
311
+ ? getTypeFromTypeNode(c, typeNode)
312
+ : widenTypeForVariableLikeDeclaration(
313
+ c,
314
+ checkExpressionCached(c, getExpression(declaration)!),
315
+ declaration,
316
+ false,
317
+ );
318
+ break;
319
+ }
320
+ case SyntaxKind.EnumMember:
321
+ result = getTypeOfEnumMember(c, symbol);
322
+ break;
323
+ case SyntaxKind.BinaryExpression:
324
+ case SyntaxKind.CallExpression:
325
+ result = getWidenedTypeForAssignmentDeclaration(c, symbol);
326
+ break;
327
+ default:
328
+ result = c.anyType;
329
+ }
330
+ if (!popTypeResolution(c)) return reportCircularityError(c, symbol);
331
+ return result;
332
+ }
333
+
334
+ export function getWidenedTypeForVariableLikeDeclaration(c: Checker, declaration: Node, reportErrors: boolean): Type {
335
+ return widenTypeForVariableLikeDeclaration(
336
+ c,
337
+ getTypeForVariableLikeDeclaration(c, declaration, true),
338
+ declaration,
339
+ reportErrors,
340
+ );
341
+ }
342
+
343
+ /** Whether a parameter, property or method is declared with `?`. */
344
+ export function isOptionalDeclaration(node: Node): boolean {
345
+ if (getQuestionToken(node) !== undefined) return true;
346
+ // Properties and methods mark optionality with a `?` postfix token.
347
+ const postfix = getPostfixToken(node);
348
+ return postfix !== undefined && postfix.kind === SyntaxKind.QuestionToken;
349
+ }
350
+
351
+ function isEmptyArrayLiteral(node: Node): boolean {
352
+ return node.kind === SyntaxKind.ArrayLiteralExpression && getElements(node)!.nodes.length === 0;
353
+ }
354
+
355
+ function isNullOrUndefined(node: Node): boolean {
356
+ let n = node;
357
+ while (n.kind === SyntaxKind.ParenthesizedExpression) n = getExpression(n)!;
358
+ return n.kind === SyntaxKind.NullKeyword || (n.kind === SyntaxKind.Identifier && getText(n) === 'undefined');
359
+ }
360
+
361
+ function isBindingPattern(node: Node | undefined): boolean {
362
+ return (
363
+ node !== undefined &&
364
+ (node.kind === SyntaxKind.ObjectBindingPattern || node.kind === SyntaxKind.ArrayBindingPattern)
365
+ );
366
+ }
367
+
368
+ /** The declared or inferred type of a variable-like declaration; undefined when nothing can be inferred. */
369
+ export function getTypeForVariableLikeDeclaration(
370
+ c: Checker,
371
+ declaration: Node,
372
+ includeOptionality: boolean,
373
+ checkMode: CheckMode = CheckMode.Normal,
374
+ ): Type | undefined {
375
+ if (declaration.kind === SyntaxKind.VariableDeclaration) {
376
+ const grandParent = declaration.parent!.parent!;
377
+ if (grandParent.kind === SyntaxKind.ForInStatement) {
378
+ // `for (k in x)` declares k as string, or keyof T for generic x.
379
+ let objectType = checkExpressionEx(c, getExpression(grandParent)!, checkMode);
380
+ if (hasTypeFacts(c, objectType, TypeFacts.IsUndefinedOrNull)) objectType = getNonNullableType(c, objectType);
381
+ const indexType = getIndexType(c, objectType);
382
+ return (indexType.flags & (TypeFlags.TypeParameter | TypeFlags.Index)) !== 0
383
+ ? getExtractStringType(c, indexType)
384
+ : c.stringType;
385
+ }
386
+ if (grandParent.kind === SyntaxKind.ForOfStatement) return checkRightHandSideOfForOf(c, grandParent);
387
+ } else if (declaration.kind === SyntaxKind.BindingElement) return getTypeForBindingElement(c, declaration);
388
+ const isProperty =
389
+ (declaration.kind === SyntaxKind.PropertyDeclaration &&
390
+ (getCombinedModifierFlags(declaration) & ModifierFlags.Accessor) === 0) ||
391
+ declaration.kind === SyntaxKind.PropertySignature;
392
+ const isOptional = includeOptionality && isOptionalDeclaration(declaration);
393
+ const typeNode = getType(declaration);
394
+ const declaredType = typeNode !== undefined ? getTypeFromTypeNode(c, typeNode) : undefined;
395
+ if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
396
+ if (declaredType !== undefined)
397
+ return (declaredType.flags & TypeFlags.AnyOrUnknown) !== 0 ? declaredType : c.errorType;
398
+ return c.useUnknownInCatchVariables ? c.unknownType : c.anyType;
399
+ }
400
+ if (declaredType !== undefined) return addOptionality(c, declaredType, isProperty, isOptional);
401
+ if (
402
+ c.noImplicitAny &&
403
+ declaration.kind === SyntaxKind.VariableDeclaration &&
404
+ !isBindingPattern(getName(declaration)) &&
405
+ (getCombinedModifierFlags(declaration) & ModifierFlags.Export) === 0 &&
406
+ (declaration.flags & NodeFlags.Ambient) === 0
407
+ ) {
408
+ // Control-flow typed `any` for uninitialized or null/undefined-initialized let and var.
409
+ const initializer = getInitializer(declaration);
410
+ if (
411
+ (getCombinedNodeFlags(declaration) & NodeFlags.Constant) === 0 &&
412
+ (initializer === undefined || isNullOrUndefined(initializer))
413
+ )
414
+ return c.autoType;
415
+ if (initializer !== undefined && isEmptyArrayLiteral(initializer)) return c.autoArrayType;
416
+ }
417
+ if (declaration.kind === SyntaxKind.Parameter) {
418
+ if (getSymbol(declaration) === undefined) return undefined;
419
+ const fn = declaration.parent!;
420
+ // A set accessor parameter takes the getter's type.
421
+ if (fn.kind === SyntaxKind.SetAccessor) {
422
+ const fnSymbol = getSymbolOfDeclaration(c, fn);
423
+ const getter = fnSymbol?.declarations.find((d) => d.kind === SyntaxKind.GetAccessor);
424
+ if (getter !== undefined) {
425
+ const getterSignature = getSignatureFromDeclaration(c, getter);
426
+ // An unannotated `this` of a setter takes the getter's `this` type.
427
+ const params = getParameters(fn)!.nodes;
428
+ if (params.length === 2 && params[0] === declaration && getSymbol(declaration)!.name === 'this')
429
+ return getterSignature.thisParameter !== undefined
430
+ ? getTypeOfSymbol(c, getterSignature.thisParameter)
431
+ : c.anyType;
432
+ return getReturnTypeOfSignature(c, getterSignature);
433
+ }
434
+ }
435
+ const fullSignatureType = getParameterTypeOfFullSignature(c, fn, declaration);
436
+ if (fullSignatureType !== undefined) return fullSignatureType;
437
+ const t =
438
+ getSymbol(declaration)!.name === 'this'
439
+ ? getContextualThisParameterType(c, fn)
440
+ : getContextuallyTypedParameterType(c, declaration);
441
+ if (t !== undefined) return addOptionality(c, t, false, isOptional);
442
+ }
443
+ const initializer = getInitializer(declaration);
444
+ if (initializer !== undefined) {
445
+ const t = widenTypeInferredFromInitializer(c, declaration, checkDeclarationInitializer(c, declaration, checkMode));
446
+ return addOptionality(c, t, isProperty, isOptional);
447
+ }
448
+ if (c.noImplicitAny && declaration.kind === SyntaxKind.PropertyDeclaration) {
449
+ // Without an annotation or initializer, the type comes from `this` assignments in the
450
+ // constructor or static blocks.
451
+ const symbol = getSymbol(declaration)!;
452
+ const isAmbient = hasSyntacticModifier(declaration, ModifierFlags.Ambient);
453
+ let t: Type | undefined;
454
+ if (!hasStaticModifier(declaration)) {
455
+ const constructor = findConstructorDeclaration(declaration.parent!);
456
+ if (constructor !== undefined) t = getFlowTypeInConstructor(c, symbol, constructor);
457
+ else if (isAmbient) t = getTypeOfPropertyInBaseClass(c, symbol);
458
+ } else {
459
+ const staticBlocks: Node[] = [];
460
+ for (const member of getMembers(declaration.parent!)!.nodes)
461
+ if (member.kind === SyntaxKind.ClassStaticBlockDeclaration) staticBlocks.push(member);
462
+ if (staticBlocks.length !== 0) t = getFlowTypeInStaticBlocks(c, symbol, staticBlocks);
463
+ else if (isAmbient) t = getTypeOfPropertyInBaseClass(c, symbol);
464
+ }
465
+ return t !== undefined ? addOptionality(c, t, true, isOptional) : undefined;
466
+ }
467
+ if (declaration.kind === SyntaxKind.JsxAttribute) return c.trueType;
468
+ if (isBindingPattern(getName(declaration))) return getTypeFromBindingPattern(c, getName(declaration)!, false, true);
469
+ return undefined;
470
+ }
471
+
472
+ export function widenTypeInferredFromInitializer(c: Checker, declaration: Node, t: Type): Type {
473
+ const widened = getWidenedLiteralTypeForInitializer(c, declaration, t);
474
+ // `null`, `undefined` and `[]` initializers in JavaScript are implicitly `any` and `any[]`.
475
+ if (isInJSFile(declaration)) {
476
+ if (widened === (c.strictNullChecks ? c.implicitNeverType : c.undefinedWideningType)) {
477
+ reportImplicitAny(c, declaration, c.anyType);
478
+ return c.anyType;
479
+ }
480
+ if (isEmptyArrayLiteralType(c, widened)) {
481
+ reportImplicitAny(c, declaration, c.anyArrayType);
482
+ return c.anyArrayType;
483
+ }
484
+ }
485
+ return widened;
486
+ }
487
+
488
+ export function getWidenedLiteralTypeForInitializer(c: Checker, declaration: Node, t: Type): Type {
489
+ if ((getCombinedNodeFlags(declaration) & NodeFlags.Constant) !== 0 || isDeclarationReadonly(declaration)) return t;
490
+ return getWidenedLiteralType(c, t);
491
+ }
492
+
493
+ function isDeclarationReadonly(declaration: Node): boolean {
494
+ return (
495
+ (getCombinedModifierFlags(declaration) & ModifierFlags.Readonly) !== 0 && declaration.kind !== SyntaxKind.Parameter
496
+ );
497
+ }
498
+
499
+ function isGlobalSymbolConstructor(c: Checker, node: Node): boolean {
500
+ const symbol = getSymbolOfDeclaration(c, node);
501
+ return symbol !== undefined && symbol === getGlobalSymbol(c, 'SymbolConstructor', SymbolFlags.Type, undefined);
502
+ }
503
+
504
+ export function widenTypeForVariableLikeDeclaration(
505
+ c: Checker,
506
+ t: Type | undefined,
507
+ declaration: Node,
508
+ reportErrors: boolean,
509
+ ): Type {
510
+ if (t !== undefined) {
511
+ // `symbol` properties merged into the global SymbolConstructor are unique symbols, as libraries
512
+ // that predate `unique symbol` declare them.
513
+ if ((t.flags & TypeFlags.ESSymbol) !== 0 && isGlobalSymbolConstructor(c, declaration.parent!))
514
+ t = getESSymbolLikeTypeForNode(c, declaration);
515
+ if (reportErrors) reportErrorsFromWidening(c, declaration, t, WideningKind.Normal);
516
+ // A unique symbol type from another declaration widens.
517
+ if (
518
+ (t.flags & TypeFlags.UniqueESSymbol) !== 0 &&
519
+ (declaration.kind === SyntaxKind.BindingElement || getType(declaration) === undefined) &&
520
+ t.symbol !== getSymbolOfDeclaration(c, declaration)
521
+ )
522
+ t = c.esSymbolType;
523
+ return getWidenedType(c, t);
524
+ }
525
+ // Rest parameters default to any[], others to any.
526
+ const isRest = declaration.kind === SyntaxKind.Parameter && getDotDotDotToken(declaration) !== undefined;
527
+ const result = isRest ? c.anyArrayType : c.anyType;
528
+ if (reportErrors && !declarationBelongsToPrivateAmbientMember(declaration)) reportImplicitAny(c, declaration, result);
529
+ return result;
530
+ }
531
+
532
+ export function declarationBelongsToPrivateAmbientMember(declaration: Node): boolean {
533
+ const root = getRootDeclaration(declaration);
534
+ const member = root.kind === SyntaxKind.Parameter ? root.parent! : root;
535
+ return isPrivateWithinAmbient(member);
536
+ }
537
+
538
+ export function isPrivateWithinAmbient(node: Node): boolean {
539
+ return (
540
+ ((getCombinedModifierFlags(node) & ModifierFlags.Private) !== 0 || isPrivateIdentifierClassElement(node)) &&
541
+ (node.flags & NodeFlags.Ambient) !== 0
542
+ );
543
+ }
544
+
545
+ function isPrivateIdentifierClassElement(node: Node): boolean {
546
+ const name = getName(node);
547
+ return name !== undefined && name.kind === SyntaxKind.PrivateIdentifier;
548
+ }
549
+
550
+ /** What a widened type is the type of, for the implicit any messages. */
551
+ export const enum WideningKind {
552
+ Normal,
553
+ FunctionReturn,
554
+ GeneratorNext,
555
+ GeneratorYield,
556
+ }
557
+
558
+ /**
559
+ * Implicit-any widening of null/undefined, which strictNullChecks avoids. A function's inferred
560
+ * types report it only without a contextual signature, or when the contextual type is generic.
561
+ */
562
+ export function reportErrorsFromWidening(c: Checker, declaration: Node, t: Type, wideningKind: WideningKind): void {
563
+ if (!c.noImplicitAny || (t.objectFlags & ObjectFlags.ContainsWideningType) === 0) return;
564
+ if (
565
+ wideningKind !== WideningKind.Normal &&
566
+ !(
567
+ isFunctionLikeDeclarationKind(declaration.kind) &&
568
+ shouldReportErrorsFromWideningWithContextualSignature(c, declaration, wideningKind)
569
+ )
570
+ )
571
+ return;
572
+ // Implicit any errors are reported within the type where possible, else on the declaration.
573
+ if (!reportWideningErrorsInType(c, t)) reportImplicitAny(c, declaration, t, wideningKind);
574
+ }
575
+
576
+ function reportWideningErrorsInType(c: Checker, t: Type): boolean {
577
+ let errorReported = false;
578
+ if ((t.objectFlags & ObjectFlags.ContainsWideningType) === 0) return false;
579
+ if ((t.flags & TypeFlags.Union) !== 0) {
580
+ if (someEmptyObjectType(c, typesOf(t))) errorReported = true;
581
+ else for (const s of typesOf(t)) errorReported = errorReported || reportWideningErrorsInType(c, s);
582
+ } else if (isArrayOrTupleType(c, t)) {
583
+ for (const s of getTypeArguments(c, t)) errorReported = errorReported || reportWideningErrorsInType(c, s);
584
+ } else if (isObjectLiteralType(t)) {
585
+ for (const p of getPropertiesOfObjectType(c, t)) {
586
+ const s = getTypeOfSymbol(c, p);
587
+ if ((s.objectFlags & ObjectFlags.ContainsWideningType) === 0) continue;
588
+ errorReported = reportWideningErrorsInType(c, s);
589
+ if (errorReported) continue;
590
+ // Property types can come from object literal normalization in unions.
591
+ let valueDeclaration: Node | undefined;
592
+ for (const d of p.declarations) {
593
+ const v = getSymbol(d)?.valueDeclaration;
594
+ if (v !== undefined && v.parent === t.symbol?.valueDeclaration) {
595
+ valueDeclaration = d;
596
+ break;
597
+ }
598
+ }
599
+ if (valueDeclaration !== undefined) {
600
+ c.error(
601
+ valueDeclaration,
602
+ Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type,
603
+ symbolToString(c, p),
604
+ typeToString(c, getWidenedType(c, s)),
605
+ );
606
+ errorReported = true;
607
+ }
608
+ }
609
+ }
610
+ return errorReported;
611
+ }
612
+
613
+ export function reportImplicitAny(
614
+ c: Checker,
615
+ declaration: Node,
616
+ t: Type,
617
+ wideningKind: WideningKind = WideningKind.Normal,
618
+ ): void {
619
+ const file = getSourceFileOfNode(declaration);
620
+ // Only TypeScript files and checked JavaScript files report implicit `any`.
621
+ if (isInJSFile(declaration) && !isCheckJsEnabledForFile(file, c.compilerOptions)) return;
622
+ const typeAsString = typeToString(c, getWidenedType(c, t));
623
+ let message: DiagnosticMessage;
624
+ switch (declaration.kind) {
625
+ case SyntaxKind.BinaryExpression:
626
+ case SyntaxKind.PropertyDeclaration:
627
+ case SyntaxKind.PropertySignature:
628
+ message = c.noImplicitAny
629
+ ? Diagnostics.Member_0_implicitly_has_an_1_type
630
+ : Diagnostics.Member_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
631
+ break;
632
+ case SyntaxKind.Parameter: {
633
+ const isRest = getDotDotDotToken(declaration) !== undefined;
634
+ // `(string) => void` most likely meant a parameter of type `string`.
635
+ const name = getName(declaration);
636
+ const parent = declaration.parent!;
637
+ if (
638
+ name !== undefined &&
639
+ name.kind === SyntaxKind.Identifier &&
640
+ (parent.kind === SyntaxKind.CallSignature ||
641
+ parent.kind === SyntaxKind.MethodSignature ||
642
+ parent.kind === SyntaxKind.FunctionType)
643
+ ) {
644
+ const index = getParameters(parent)!.nodes.indexOf(declaration);
645
+ const text = getText(name);
646
+ if (
647
+ index >= 0 &&
648
+ (isTypeNodeKind(identifierToKeywordKind(text)) ||
649
+ resolveName(c, declaration, text, SymbolFlags.Type, undefined, true, false) !== undefined)
650
+ ) {
651
+ c.addErrorOrSuggestion(
652
+ c.noImplicitAny,
653
+ createDiagnosticForNode(declaration, Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, [
654
+ 'arg' + index,
655
+ declarationNameToString(file, name) + (isRest ? '[]' : ''),
656
+ ]),
657
+ );
658
+ return;
659
+ }
660
+ }
661
+ if (isRest)
662
+ message = c.noImplicitAny
663
+ ? Diagnostics.Rest_parameter_0_implicitly_has_an_any_type
664
+ : Diagnostics.Rest_parameter_0_implicitly_has_an_any_type_but_a_better_type_may_be_inferred_from_usage;
665
+ else
666
+ message = c.noImplicitAny
667
+ ? Diagnostics.Parameter_0_implicitly_has_an_1_type
668
+ : Diagnostics.Parameter_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
669
+ break;
670
+ }
671
+ case SyntaxKind.BindingElement:
672
+ if (!c.noImplicitAny) return;
673
+ message = Diagnostics.Binding_element_0_implicitly_has_an_1_type;
674
+ break;
675
+ case SyntaxKind.FunctionDeclaration:
676
+ case SyntaxKind.MethodDeclaration:
677
+ case SyntaxKind.MethodSignature:
678
+ case SyntaxKind.GetAccessor:
679
+ case SyntaxKind.SetAccessor:
680
+ case SyntaxKind.FunctionExpression:
681
+ case SyntaxKind.ArrowFunction:
682
+ if (c.noImplicitAny && getName(declaration) === undefined) {
683
+ c.error(
684
+ declaration,
685
+ wideningKind === WideningKind.GeneratorYield
686
+ ? Diagnostics.Generator_implicitly_has_yield_type_0_Consider_supplying_a_return_type_annotation
687
+ : Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type,
688
+ typeAsString,
689
+ );
690
+ return;
691
+ }
692
+ if (c.noImplicitAny && (declaration.flags & NodeFlags.Reparsed) !== 0) {
693
+ c.error(
694
+ declaration,
695
+ Diagnostics.This_overload_implicitly_returns_the_type_0_because_it_lacks_a_return_type_annotation,
696
+ typeAsString,
697
+ );
698
+ return;
699
+ }
700
+ if (!c.noImplicitAny)
701
+ message = Diagnostics.X_0_implicitly_has_an_1_return_type_but_a_better_type_may_be_inferred_from_usage;
702
+ else if (wideningKind === WideningKind.GeneratorYield)
703
+ message = Diagnostics.X_0_which_lacks_return_type_annotation_implicitly_has_an_1_yield_type;
704
+ else message = Diagnostics.X_0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
705
+ break;
706
+ case SyntaxKind.MappedType:
707
+ if (c.noImplicitAny) c.error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
708
+ return;
709
+ default:
710
+ message = c.noImplicitAny
711
+ ? Diagnostics.Variable_0_implicitly_has_an_1_type
712
+ : Diagnostics.Variable_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
713
+ }
714
+ const name = getNameOfDeclaration(declaration);
715
+ c.addErrorOrSuggestion(
716
+ c.noImplicitAny,
717
+ createDiagnosticForNode(declaration, message, [declarationNameToString(file, name), typeAsString]),
718
+ );
719
+ }
720
+
721
+ // ---- Widening
722
+
723
+ export function getWidenedType(c: Checker, t: Type): Type {
724
+ return getWidenedTypeWithContext(c, t, undefined);
725
+ }
726
+
727
+ /** Widening state for the constituents of a union: object literals get each other's missing properties. */
728
+ class WideningContext {
729
+ readonly parent: WideningContext | undefined;
730
+ readonly propertyName: string;
731
+ siblings: Type[] | undefined;
732
+ resolvedProperties: Symbol[] | undefined = undefined;
733
+ /** The contexts of the properties, by name, shared by the object literals widened in this one. */
734
+ childContexts: Map<string, WideningContext> | undefined = undefined;
735
+ /** The widened types of the object literals widened in this context, for child contexts. */
736
+ widenedTypes: Map<Type, Type> | undefined = undefined;
737
+
738
+ constructor(parent: WideningContext | undefined, propertyName: string, siblings: Type[] | undefined) {
739
+ this.parent = parent;
740
+ this.propertyName = propertyName;
741
+ this.siblings = siblings;
742
+ }
743
+
744
+ getChildContext(propertyName: string): WideningContext {
745
+ this.childContexts ??= new Map<string, WideningContext>();
746
+ let result = this.childContexts.get(propertyName);
747
+ if (result === undefined) {
748
+ result = new WideningContext(this, propertyName, undefined);
749
+ this.childContexts.set(propertyName, result);
750
+ }
751
+ return result;
752
+ }
753
+ }
754
+
755
+ function getWidenedTypeWithContext(c: Checker, t: Type, context: WideningContext | undefined): Type {
756
+ if ((t.objectFlags & ObjectFlags.RequiresWidening) === 0) return t;
757
+ const key = cachedTypeKey(CachedTypeKind.Widened, t.id);
758
+ if (context === undefined) {
759
+ const cached = c.cachedTypesById.get(key);
760
+ if (cached !== undefined) return cached;
761
+ }
762
+ let result: Type | undefined;
763
+ if ((t.flags & (TypeFlags.Any | TypeFlags.Nullable)) !== 0) result = c.anyType;
764
+ else if ((t.objectFlags & ObjectFlags.ObjectLiteral) !== 0 && (t.flags & TypeFlags.Object) !== 0)
765
+ result = getWidenedTypeOfObjectLiteral(c, t, context);
766
+ else if ((t.flags & TypeFlags.Union) !== 0) {
767
+ const unionContext = context ?? new WideningContext(undefined, '', typesOf(t));
768
+ const widened: Type[] = [];
769
+ for (const u of typesOf(t))
770
+ widened.push((u.flags & TypeFlags.Nullable) !== 0 ? u : getWidenedTypeWithContext(c, u, unionContext));
771
+ // Widened empty object literals reduce subtypes, e.g. {} | string to {}.
772
+ result = getUnionTypeEx(
773
+ c,
774
+ widened,
775
+ someEmptyObjectType(c, widened) ? UnionReduction.Subtype : UnionReduction.Literal,
776
+ undefined,
777
+ undefined,
778
+ );
779
+ } else if ((t.flags & TypeFlags.Intersection) !== 0) result = getIntersectionType(c, getWidenedTypes(c, typesOf(t)));
780
+ else if (isArrayOrTupleType(c, t))
781
+ result = createTypeReference(c, (t as TypeReference).target!, getWidenedTypes(c, getTypeArguments(c, t)));
782
+ if (result !== undefined && context === undefined) c.cachedTypesById.set(key, result);
783
+ return result ?? t;
784
+ }
785
+
786
+ function getWidenedTypes(c: Checker, types: Type[]): Type[] {
787
+ const result: Type[] = [];
788
+ for (const t of types) result.push(getWidenedType(c, t));
789
+ return result;
790
+ }
791
+
792
+ function someEmptyObjectType(c: Checker, types: Type[]): boolean {
793
+ for (const t of types) if (isEmptyObjectType(c, t)) return true;
794
+ return false;
795
+ }
796
+
797
+ function getWidenedTypeOfObjectLiteral(c: Checker, t: Type, context: WideningContext | undefined): Type {
798
+ const cached = context?.widenedTypes?.get(t);
799
+ if (cached !== undefined) return cached;
800
+ const members = new Map<string, Symbol>();
801
+ for (const prop of getPropertiesOfObjectType(c, t)) members.set(prop.name, getWidenedProperty(c, prop, context));
802
+ if (context !== undefined)
803
+ for (const prop of getPropertiesOfContext(c, context))
804
+ if (!members.has(prop.name)) members.set(prop.name, getUndefinedProperty(c, prop));
805
+ const infos = getIndexInfosOfType(c, t).map(
806
+ (info) =>
807
+ new IndexInfo(
808
+ info.keyType,
809
+ getWidenedType(c, info.valueType),
810
+ info.isReadonly,
811
+ info.declaration,
812
+ info.components,
813
+ ),
814
+ );
815
+ const result = createAnonymousType(c, t.symbol, members, [], [], infos);
816
+ result.objectFlags |= t.objectFlags & (ObjectFlags.JSLiteral | ObjectFlags.NonInferrableType);
817
+ // The root context widens each object literal once; a property's context may widen one literal
818
+ // for several siblings.
819
+ if (context !== undefined && context.parent !== undefined) {
820
+ context.widenedTypes ??= new Map<Type, Type>();
821
+ context.widenedTypes.set(t, result);
822
+ }
823
+ return result;
824
+ }
825
+
826
+ function getWidenedProperty(c: Checker, prop: Symbol, context: WideningContext | undefined): Symbol {
827
+ if ((prop.flags & SymbolFlags.Property) === 0) return prop;
828
+ const original = getTypeOfSymbol(c, prop);
829
+ const propContext = context?.getChildContext(prop.name);
830
+ const widened = getWidenedTypeWithContext(c, original, propContext);
831
+ if (widened === original) return prop;
832
+ return createSymbolWithWidenedType(c, prop, widened);
833
+ }
834
+
835
+ function createSymbolWithWidenedType(c: Checker, prop: Symbol, t: Type): Symbol {
836
+ const result = c.newSymbolWithCheckFlags(prop.flags, prop.name, prop.checkFlags & CheckFlags.Readonly);
837
+ result.declarations = prop.declarations;
838
+ result.parent = prop.parent;
839
+ result.valueDeclaration = prop.valueDeclaration;
840
+ const id = getSymbolId(result);
841
+ c.symbolTypes.set(id, t);
842
+ c.symbolTargets.set(id, prop);
843
+ c.symbolNameTypes.set(id, c.symbolNameTypes.get(getSymbolId(prop)));
844
+ return result;
845
+ }
846
+
847
+ /**
848
+ * An optional `undefined` property standing in for `prop`, the last property of that name among the
849
+ * sibling object literals (getPropertiesOfContext). It carries the declarations of the property it is
850
+ * created from, which order it among the members when they are printed.
851
+ *
852
+ * tsgo caches one such symbol per name and checker, so the declarations come from the first literal
853
+ * its checker widened with that name missing in a sibling. Its checkers check the program's files
854
+ * round robin, each file from the top. tsc-ts's checkers take files in another order, and a checker
855
+ * can infer a file's functions for an importer before it checks the file, so the first union is found
856
+ * in the sources of the files tsgo checks with the checker of the property's file, up to that file
857
+ * (`firstSiblingProperty`), which doesn't depend on the order of checking. Without one, the property
858
+ * is cached per sibling property. The order differs from tsgo's where tsgo's checker first infers a
859
+ * function of a file of another checker, or a later function of the file.
860
+ */
861
+ function getUndefinedProperty(c: Checker, prop: Symbol): Symbol {
862
+ const member = prop.declarations.length > 0 ? firstSiblingProperty(c, prop.declarations[0]!, prop.name) : undefined;
863
+ const first = member !== undefined ? getSymbol(member) : undefined;
864
+ const source = first ?? prop;
865
+ const key = getSymbolId(source);
866
+ const cached = c.undefinedProperties.get(key);
867
+ if (cached !== undefined) return cached;
868
+ const result = createSymbolWithWidenedType(c, source, c.undefinedOrMissingType);
869
+ result.flags |= SymbolFlags.Optional;
870
+ // The properties of a literal in an `as const` assertion are readonly.
871
+ if (first !== undefined && isInConstAssertion(member!.parent!)) result.checkFlags |= CheckFlags.Readonly;
872
+ c.undefinedProperties.set(key, result);
873
+ return result;
874
+ }
875
+
876
+ /**
877
+ * The property of `name` in the first union of object literals that leaves `name` missing in one of
878
+ * them, among the files tsgo checks with the checker of the declaration's file, up to that file: the
879
+ * last literal's property of that name. tsgo's checker `k` checks files `k`, `k + n`, `k + 2n`, ...
880
+ * in order (`tsgoCheckerSlots`). Unions are those of the returned expressions of a function without
881
+ * a return type, and of the alternatives of an initializer without a type (`?:`, `||`, `??`, array
882
+ * elements, an element access of an array literal, and the values of a property across the literals),
883
+ * of the variables of a `for...of`, of the object of a method call and of the expression of a type
884
+ * assertion. In a file, they are taken in the order a checker checking the file from the top widens
885
+ * them (`SiblingPropertyScan`).
886
+ */
887
+ function firstSiblingProperty(c: Checker, declaration: Node, name: string): Node | undefined {
888
+ const file = getSourceFileOfNode(declaration);
889
+ const index = c.fileIndex.get(file);
890
+ if (index === undefined) return siblingPropertiesOfFile(c, file).get(name);
891
+ const slots = tsgoCheckerSlots(c);
892
+ let slot = c.slotSiblingProperties.get(index % slots);
893
+ if (slot === undefined) {
894
+ slot = new SlotSiblingProperties(index % slots);
895
+ c.slotSiblingProperties.set(index % slots, slot);
896
+ }
897
+ for (; slot.next <= index; slot.next += slots)
898
+ for (const [n, member] of siblingPropertiesOfFile(c, c.files[slot.next]!))
899
+ if (!slot.members.has(n)) {
900
+ slot.members.set(n, member);
901
+ slot.fileIndexes.set(n, slot.next);
902
+ }
903
+ const found = slot.fileIndexes.get(name);
904
+ return found !== undefined && found <= index ? slot.members.get(name) : undefined;
905
+ }
906
+
907
+ /**
908
+ * By name, the first property of the unions of `file` that leave the name missing: what a parse thread
909
+ * found (`SourceFile.siblingProperties`), or else the checker's own scan.
910
+ */
911
+ function siblingPropertiesOfFile(c: Checker, file: SourceFile): Map<string, Node> {
912
+ // A parse thread's scan takes array literals to have the global Array type.
913
+ if (file.siblingProperties !== undefined && c.globalArrayType !== c.emptyGenericType) return file.siblingProperties;
914
+ let first = c.firstSiblingProperties.get(file);
915
+ if (first === undefined) {
916
+ first = scanSiblingProperties(file, c)!;
917
+ c.firstSiblingProperties.set(file, first);
918
+ }
919
+ return first;
920
+ }
921
+
922
+ /**
923
+ * By name, the first property of the unions of `file`'s object literals that leave the name missing.
924
+ * Without a checker, as on a parse thread, array literals are taken to have the global Array type, and
925
+ * the result is undefined where an enum the scan can't resolve decides which literals subtype reduction
926
+ * leaves out (`enumKeyOfMemberAccess`); the checker then scans the file itself.
927
+ */
928
+ export function scanSiblingProperties(file: SourceFile, c: Checker | undefined): Map<string, Node> | undefined {
929
+ if (file.isDeclarationFile) return new Map<string, Node>();
930
+ const scan = new SiblingPropertyScan(c);
931
+ file.forEachChild(scan);
932
+ scan.widenAll();
933
+ return scan.unresolved ? undefined : scan.members;
934
+ }
935
+
936
+ /** The first property of each name among the files of one of tsgo's checkers, scanned in order. */
937
+ export class SlotSiblingProperties {
938
+ readonly members = new Map<string, Node>();
939
+ /** The index of the file of each name's property. */
940
+ readonly fileIndexes = new Map<string, number>();
941
+ /** The index of the next file of the checker to scan. */
942
+ next: number;
943
+
944
+ constructor(first: number) {
945
+ this.next = first;
946
+ }
947
+ }
948
+
949
+ /** Whether an object literal is in an `as const` assertion, through arrays, spreads and properties. */
950
+ function isInConstAssertion(literal: Node): boolean {
951
+ for (let node = literal; node.parent !== undefined; node = node.parent) {
952
+ switch (node.parent.kind) {
953
+ case SyntaxKind.ParenthesizedExpression:
954
+ case SyntaxKind.ArrayLiteralExpression:
955
+ case SyntaxKind.SpreadElement:
956
+ case SyntaxKind.PropertyAssignment:
957
+ case SyntaxKind.ObjectLiteralExpression:
958
+ continue;
959
+ case SyntaxKind.AsExpression:
960
+ case SyntaxKind.TypeAssertionExpression:
961
+ return isConstTypeReference(getType(node.parent)!);
962
+ }
963
+ return false;
964
+ }
965
+ return false;
966
+ }
967
+
968
+ /** An expression whose type is widened, at the position where a checker widens it. */
969
+ class WideningRoot {
970
+ constructor(
971
+ readonly position: number,
972
+ /** The function whose returned expressions these are, widened once. */
973
+ readonly func: Node | undefined,
974
+ /** The expressions whose types are unioned and widened. */
975
+ readonly alternatives: Node[],
976
+ /** The expressions whose calls, innermost first, infer their functions' return types before. */
977
+ readonly sources: Node[],
978
+ /** The calls of `sources` that may call a function of the file (`SiblingPropertyScan.callees`). */
979
+ public calls: Node[] | undefined = undefined,
980
+ ) {}
981
+ }
982
+
983
+ /**
984
+ * The unions a checker widens while checking the file's statements or the body of one deferred function, and
985
+ * the deferred functions it queues there.
986
+ */
987
+ class CheckScope {
988
+ readonly roots: WideningRoot[] = [];
989
+ readonly deferred: CheckScope[] = [];
990
+ /** The calls by a name (`calleeName`), which infer where they end. */
991
+ readonly calls: Node[] = [];
992
+
993
+ constructor(readonly position: number) {}
994
+ }
995
+
996
+ /**
997
+ * Whether a checker checks `node`'s body only as a deferred node: a function expression, arrow
998
+ * function or object literal method or accessor whose return type is written. Without one, the
999
+ * checker infers the return type where it reaches the function, from the returned expressions and
1000
+ * what they refer to.
1001
+ */
1002
+ function isDeferredCheck(node: Node): boolean {
1003
+ switch (node.kind) {
1004
+ case SyntaxKind.FunctionExpression:
1005
+ case SyntaxKind.ArrowFunction:
1006
+ return getType(node) !== undefined;
1007
+ case SyntaxKind.MethodDeclaration:
1008
+ case SyntaxKind.GetAccessor:
1009
+ return node.parent?.kind === SyntaxKind.ObjectLiteralExpression && getType(node) !== undefined;
1010
+ case SyntaxKind.SetAccessor:
1011
+ return node.parent?.kind === SyntaxKind.ObjectLiteralExpression;
1012
+ }
1013
+ return false;
1014
+ }
1015
+
1016
+ /**
1017
+ * Finds the unions of a file's object literals in the order a checker widens them: an initializer
1018
+ * where it is, and a function's returned expressions at its first `return`, where the checker
1019
+ * infers its return type. A call of a function of the file, by a name that resolves to it or as
1020
+ * `this.name(...)` in a class, infers that function's return type where the call is, and before the
1021
+ * union of the expression the call is in. The bodies of function expressions, arrow functions and
1022
+ * object literal methods and accessors with a written return type are checked after the file's
1023
+ * statements, so their unions widen then.
1024
+ */
1025
+ class SiblingPropertyScan extends NodeVisitor {
1026
+ /** The checker that resolves imported enums, or undefined on a parse thread. */
1027
+ readonly c: Checker | undefined;
1028
+ /** By name, the property of the first union that leaves the name missing in a literal. */
1029
+ readonly members = new Map<string, Node>();
1030
+ /**
1031
+ * Whether array literals have an array type, whose elements are widened as one union. Without the
1032
+ * global Array type (--noLib) an array literal is an empty object type.
1033
+ */
1034
+ private readonly arrays: boolean;
1035
+ /** Whether an enum the scan can't resolve without a checker decided which literals were left out. */
1036
+ unresolved = false;
1037
+ /** The enums of the members that values refer to, numbered in the order the scan meets them. */
1038
+ private readonly enums = new Map<Symbol, number>();
1039
+ private readonly fileScope = new CheckScope(0);
1040
+ /** The scope of the innermost deferred function around the node being visited. */
1041
+ private scope = this.fileScope;
1042
+ /** The returned expressions of each function without a return type. */
1043
+ private readonly returned = new Map<Node, WideningRoot>();
1044
+ /** The names a call can call those functions by (`functionName`). */
1045
+ private readonly callees = new Set<string>();
1046
+ private readonly widened = new Set<Node>();
1047
+ /** The functions and methods of each class, by name. */
1048
+ private readonly classFunctions = new Map<Node, Map<string, Node>>();
1049
+
1050
+ constructor(c: Checker | undefined) {
1051
+ super();
1052
+ this.c = c;
1053
+ this.arrays = c === undefined || c.globalArrayType !== c.emptyGenericType;
1054
+ }
1055
+
1056
+ /** The key of the values that are members of `symbol`, an enum, in this scan. */
1057
+ enumKey(symbol: Symbol): string {
1058
+ let id = this.enums.get(symbol);
1059
+ if (id === undefined) {
1060
+ id = this.enums.size;
1061
+ this.enums.set(symbol, id);
1062
+ }
1063
+ return 'enum ' + id;
1064
+ }
1065
+
1066
+ visitNode(node: Node): boolean {
1067
+ switch (node.kind) {
1068
+ case SyntaxKind.FunctionDeclaration:
1069
+ case SyntaxKind.FunctionExpression:
1070
+ case SyntaxKind.ArrowFunction:
1071
+ case SyntaxKind.MethodDeclaration:
1072
+ case SyntaxKind.GetAccessor: {
1073
+ const body = getBody(node);
1074
+ if (body === undefined || getType(node) !== undefined) break;
1075
+ const returned: Node[] = [];
1076
+ const sources: Node[] = [];
1077
+ let position = body.pos;
1078
+ if (body.kind === SyntaxKind.Block) {
1079
+ const collector = new ReturnCollector();
1080
+ collector.visitStatements(body as Block);
1081
+ if (collector.returns.length > 0) position = collector.returns[0]!.pos;
1082
+ for (const r of collector.returns)
1083
+ if (r.expression !== undefined) {
1084
+ addAlternatives(r.expression, returned, this.arrays);
1085
+ sources.push(r.expression);
1086
+ }
1087
+ } else {
1088
+ addAlternatives(body, returned, this.arrays);
1089
+ sources.push(body);
1090
+ }
1091
+ if (returned.length === 0) break;
1092
+ const root = new WideningRoot(position, node, returned, sources);
1093
+ this.returned.set(node, root);
1094
+ this.addRoot(root);
1095
+ if (node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.GetAccessor)
1096
+ this.addClassFunction(node, node);
1097
+ break;
1098
+ }
1099
+ case SyntaxKind.VariableDeclaration:
1100
+ case SyntaxKind.PropertyDeclaration:
1101
+ case SyntaxKind.Parameter: {
1102
+ const initializer = getInitializer(node);
1103
+ if (initializer === undefined || getType(node) !== undefined) break;
1104
+ this.addRoot(
1105
+ new WideningRoot(initializer.pos, undefined, addAlternatives(initializer, [], this.arrays), [initializer]),
1106
+ );
1107
+ const value = skipParentheses(initializer);
1108
+ if (
1109
+ node.kind === SyntaxKind.PropertyDeclaration &&
1110
+ (value.kind === SyntaxKind.ArrowFunction || value.kind === SyntaxKind.FunctionExpression)
1111
+ )
1112
+ this.addClassFunction(node, value);
1113
+ break;
1114
+ }
1115
+ case SyntaxKind.ExportAssignment:
1116
+ if (getType(node) === undefined) {
1117
+ const expression = getExpression(node)!;
1118
+ this.addRoot(
1119
+ new WideningRoot(node.pos, undefined, addAlternatives(expression, [], this.arrays), [expression]),
1120
+ );
1121
+ }
1122
+ break;
1123
+ case SyntaxKind.ForOfStatement: {
1124
+ // The variable of `for (const x of e)` has the widened type of e's elements, and each element
1125
+ // of a binding pattern the widened type of its part of them.
1126
+ const statement = node as ForInOrOfStatement;
1127
+ if (statement.initializer.kind !== SyntaxKind.VariableDeclarationList) break;
1128
+ const declaration = (statement.initializer as VariableDeclarationList).declarations.nodes[0];
1129
+ if (declaration === undefined || getType(declaration) !== undefined || !this.arrays) break;
1130
+ const elements: Node[] = [];
1131
+ for (const array of iteratedArrayLiterals(statement.expression)) addArrayElements(array, elements);
1132
+ if (elements.length === 0) break;
1133
+ for (const alternatives of bindingAlternatives(getName(declaration)!, elements))
1134
+ this.addRoot(new WideningRoot(statement.expression.pos, undefined, alternatives, [statement.expression]));
1135
+ break;
1136
+ }
1137
+ case SyntaxKind.CallExpression: {
1138
+ // The object of a method call is widened before the call is resolved.
1139
+ const callee = skipParentheses(getExpression(node)!);
1140
+ if (
1141
+ (callee.kind === SyntaxKind.PropertyAccessExpression || callee.kind === SyntaxKind.ElementAccessExpression) &&
1142
+ mayHoldLiterals(getExpression(callee)!)
1143
+ )
1144
+ this.addExpressionRoot(callee.pos, getExpression(callee)!);
1145
+ // Calls elsewhere, as in statements, infer where they end.
1146
+ if (calleeName(node) !== undefined) this.scope.calls.push(node);
1147
+ break;
1148
+ }
1149
+ }
1150
+ if (isTypeNodeKind(node.kind)) return false;
1151
+ if (
1152
+ (node.kind === SyntaxKind.AsExpression || node.kind === SyntaxKind.TypeAssertionExpression) &&
1153
+ !isConstTypeReference(getType(node)!)
1154
+ ) {
1155
+ // An assertion widens its expression's type in a deferred check, queued after the expression's.
1156
+ node.forEachChild(this);
1157
+ const inner = new CheckScope(node.end);
1158
+ this.scope.deferred.push(inner);
1159
+ const saveScope = this.scope;
1160
+ this.scope = inner;
1161
+ this.addExpressionRoot(node.end, getExpression(node)!);
1162
+ this.scope = saveScope;
1163
+ return false;
1164
+ }
1165
+ if (isDeferredCheck(node)) {
1166
+ const inner = new CheckScope(node.pos);
1167
+ this.scope.deferred.push(inner);
1168
+ const saveScope = this.scope;
1169
+ this.scope = inner;
1170
+ node.forEachChild(this);
1171
+ this.scope = saveScope;
1172
+ } else node.forEachChild(this);
1173
+ return false;
1174
+ }
1175
+
1176
+ private addRoot(root: WideningRoot): void {
1177
+ this.scope.roots.push(root);
1178
+ }
1179
+
1180
+ /** Adds a root for the widened type of `expression` where it holds object or array literals. */
1181
+ private addExpressionRoot(position: number, expression: Node): void {
1182
+ const alternatives = addAlternatives(expression, [], this.arrays);
1183
+ if (
1184
+ alternatives.some(
1185
+ (a) => a.kind === SyntaxKind.ObjectLiteralExpression || a.kind === SyntaxKind.ArrayLiteralExpression,
1186
+ )
1187
+ )
1188
+ this.addRoot(new WideningRoot(position, undefined, alternatives, [expression]));
1189
+ }
1190
+
1191
+ /** Names `func` in its class by `member`, a method or a property it initializes. */
1192
+ private addClassFunction(member: Node, func: Node): void {
1193
+ const name = getName(member);
1194
+ const parent = member.parent!;
1195
+ if (name === undefined || name.kind !== SyntaxKind.Identifier) return;
1196
+ if (parent.kind !== SyntaxKind.ClassDeclaration && parent.kind !== SyntaxKind.ClassExpression) return;
1197
+ let functions = this.classFunctions.get(parent);
1198
+ if (functions === undefined) this.classFunctions.set(parent, (functions = new Map()));
1199
+ if (!functions.has(getText(name))) functions.set(getText(name), func);
1200
+ }
1201
+
1202
+ /**
1203
+ * Widens the unions of the file's statements in order, then those of each deferred node in the
1204
+ * order the checker queues them, which includes the deferred nodes queued while checking one.
1205
+ */
1206
+ widenAll(): void {
1207
+ for (const func of this.returned.keys()) {
1208
+ const name = functionName(func);
1209
+ if (name !== undefined) this.callees.add(name);
1210
+ }
1211
+ const queue = [this.fileScope];
1212
+ for (let i = 0; i < queue.length; i++) {
1213
+ const scope = queue[i]!;
1214
+ if (this.callees.size !== 0)
1215
+ for (const call of scope.calls)
1216
+ if (this.callees.has(calleeName(call)!))
1217
+ scope.roots.push(new WideningRoot(call.end, undefined, [], [], [call]));
1218
+ scope.roots.sort((a, b) => a.position - b.position);
1219
+ scope.deferred.sort((a, b) => a.position - b.position);
1220
+ let next = 0;
1221
+ for (const root of scope.roots) {
1222
+ while (next < scope.deferred.length && scope.deferred[next]!.position < root.position)
1223
+ queue.push(scope.deferred[next++]!);
1224
+ this.widenRoot(root);
1225
+ }
1226
+ while (next < scope.deferred.length) queue.push(scope.deferred[next++]!);
1227
+ }
1228
+ }
1229
+
1230
+ /** Widens a root's unions, after the returned expressions of the functions its calls infer. */
1231
+ private widenRoot(root: WideningRoot): void {
1232
+ if (root.func !== undefined) {
1233
+ if (this.widened.has(root.func)) return;
1234
+ this.widened.add(root.func);
1235
+ }
1236
+ if (this.callees.size === 0) {
1237
+ this.widen(root.alternatives, true);
1238
+ return;
1239
+ }
1240
+ if (root.calls === undefined) {
1241
+ const collector = new CallCollector(this.callees);
1242
+ for (const source of root.sources) collector.visitNode(source);
1243
+ root.calls = collector.calls;
1244
+ }
1245
+ for (const call of root.calls) {
1246
+ const callee = this.calledFunction(call);
1247
+ const returned = callee !== undefined ? this.returned.get(callee) : undefined;
1248
+ if (returned !== undefined) this.widenRoot(returned);
1249
+ }
1250
+ this.widen(root.alternatives, true);
1251
+ }
1252
+
1253
+ /**
1254
+ * The function a call calls: a function declaration or a variable initialized with a function that
1255
+ * the callee's name resolves to (`getLocals` of the scopes around the call), or a method or property
1256
+ * of the class for `this.name(...)` in a class member.
1257
+ */
1258
+ private calledFunction(call: Node): Node | undefined {
1259
+ const callee = skipParentheses(getExpression(call)!);
1260
+ if (callee.kind === SyntaxKind.Identifier) {
1261
+ const name = getText(callee);
1262
+ for (let scope = call.parent; scope !== undefined; scope = scope.parent) {
1263
+ const symbol = getLocals(scope)?.get(name);
1264
+ // A type alias, interface or type parameter of the name doesn't hide a value of an outer scope.
1265
+ if (symbol === undefined || (symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) === 0) continue;
1266
+ // An exported declaration's local symbol has no value declaration of its own.
1267
+ const declaration =
1268
+ symbol.valueDeclaration ??
1269
+ symbol.declarations.find(
1270
+ (d) => d.kind === SyntaxKind.FunctionDeclaration || d.kind === SyntaxKind.VariableDeclaration,
1271
+ );
1272
+ if (declaration === undefined) return undefined;
1273
+ if (declaration.kind === SyntaxKind.FunctionDeclaration) return declaration;
1274
+ const initializer =
1275
+ declaration.kind === SyntaxKind.VariableDeclaration ? getInitializer(declaration) : undefined;
1276
+ const value = initializer !== undefined ? skipParentheses(initializer) : undefined;
1277
+ return value !== undefined &&
1278
+ (value.kind === SyntaxKind.ArrowFunction || value.kind === SyntaxKind.FunctionExpression)
1279
+ ? value
1280
+ : undefined;
1281
+ }
1282
+ return undefined;
1283
+ }
1284
+ if (callee.kind !== SyntaxKind.PropertyAccessExpression || getExpression(callee)!.kind !== SyntaxKind.ThisKeyword)
1285
+ return undefined;
1286
+ const name = getName(callee)!;
1287
+ const container = thisClass(call);
1288
+ const functions = container !== undefined ? this.classFunctions.get(container) : undefined;
1289
+ return functions !== undefined && name.kind === SyntaxKind.Identifier ? functions.get(getText(name)) : undefined;
1290
+ }
1291
+
1292
+ /**
1293
+ * Records the unions of the alternatives of one widened expression, and of their nested values.
1294
+ * With `reduced`, the alternatives make up one union with subtype reduction (the alternatives of an
1295
+ * expression, the elements of an array literal), which drops a literal whose widened type is the
1296
+ * type of an earlier one (`removeSubtypes` removes the later of two identical types). The values of
1297
+ * a property across literals are not reduced: their union is one of each literal's value.
1298
+ */
1299
+ private widen(alternatives: Node[], reduced: boolean): void {
1300
+ let literals: ObjectLiteralExpression[] = [];
1301
+ for (const a of alternatives)
1302
+ if (a.kind === SyntaxKind.ObjectLiteralExpression) literals.push(a as ObjectLiteralExpression);
1303
+ // The elements of a tuple, an array literal in an `as const` assertion, widen separately.
1304
+ else if (this.arrays && a.kind === SyntaxKind.ArrayLiteralExpression && !isInConstAssertion(a))
1305
+ this.widen(addArrayElements(a as ArrayLiteralExpression, []), true);
1306
+ if (literals.length === 0) return;
1307
+ if (reduced) literals = withoutRepeatedLiterals(this, literals);
1308
+ if (literals.length > 1) this.record(literals);
1309
+ const values = new Map<string, Node[]>();
1310
+ for (const literal of literals)
1311
+ for (const member of literal.properties.nodes) {
1312
+ if (member.kind !== SyntaxKind.PropertyAssignment) continue;
1313
+ const name = staticPropertyName(member);
1314
+ if (name === undefined) continue;
1315
+ let list = values.get(name);
1316
+ if (list === undefined) values.set(name, (list = []));
1317
+ // A value's own alternatives make up one reduced union, one member of the property's values.
1318
+ const own = addAlternatives((member as PropertyAssignment).initializer, [], this.arrays);
1319
+ for (const value of withoutRepeatedLiterals(this, own)) list.push(value);
1320
+ }
1321
+ for (const list of values.values()) this.widen(list, false);
1322
+ }
1323
+
1324
+ /**
1325
+ * Records, for each name that some literal without a spread lacks, the last such literal's property
1326
+ * of that name, unless an earlier union has the name.
1327
+ */
1328
+ private record(literals: ObjectLiteralExpression[]): void {
1329
+ const plain: Map<string, Node>[] = [];
1330
+ for (const literal of literals) {
1331
+ let members: Map<string, Node> | undefined = new Map();
1332
+ for (const member of literal.properties.nodes) {
1333
+ if (member.kind === SyntaxKind.SpreadAssignment) {
1334
+ members = undefined;
1335
+ break;
1336
+ }
1337
+ const name = staticPropertyName(member);
1338
+ if (name !== undefined) members.set(name, member);
1339
+ }
1340
+ if (members !== undefined) plain.push(members);
1341
+ }
1342
+ const last = new Map<string, Node>();
1343
+ for (const members of plain) for (const [name, member] of members) last.set(name, member);
1344
+ for (const [name, member] of last) {
1345
+ // A method's or accessor's symbol has other flags than the property checkObjectLiteral makes of it.
1346
+ if (member.kind !== SyntaxKind.PropertyAssignment && member.kind !== SyntaxKind.ShorthandPropertyAssignment)
1347
+ continue;
1348
+ if (this.members.has(name) || plain.every((members) => members.has(name))) continue;
1349
+ this.members.set(name, member);
1350
+ }
1351
+ }
1352
+ }
1353
+
1354
+ /**
1355
+ * `nodes` without the object literals whose widened types are certainly those of an earlier one:
1356
+ * the same property names, each with a value of the same widened type (`widenedTypeKey`). Literals
1357
+ * that can have a contextual type are kept: it can keep their properties' literal types.
1358
+ */
1359
+ function withoutRepeatedLiterals<T extends Node>(scan: SiblingPropertyScan, nodes: T[]): T[] {
1360
+ let keys: string[] | undefined;
1361
+ let result: T[] | undefined;
1362
+ for (let i = 0; i < nodes.length; i++) {
1363
+ const node = nodes[i]!;
1364
+ const key =
1365
+ node.kind === SyntaxKind.ObjectLiteralExpression && !mayHaveContextualType(node)
1366
+ ? widenedTypeKey(scan, node)
1367
+ : undefined;
1368
+ if (key !== undefined) {
1369
+ if (keys === undefined) keys = [];
1370
+ else if (keys.includes(key)) {
1371
+ // Without a checker, an enum the scan can't resolve may or may not make the types the same.
1372
+ if (key.includes(pendingEnumKey)) scan.unresolved = true;
1373
+ if (result === undefined) result = nodes.slice(0, i);
1374
+ continue;
1375
+ }
1376
+ keys.push(key);
1377
+ }
1378
+ result?.push(node);
1379
+ }
1380
+ return result ?? nodes;
1381
+ }
1382
+
1383
+ /**
1384
+ * A key that two expressions of one file share only when their types widen to the same type, or
1385
+ * undefined where that isn't known from the syntax: literals by their widened type, members of an
1386
+ * enum by the enum, and object literals by their properties' names and keys. Other references can
1387
+ * name different symbols or be narrowed differently.
1388
+ */
1389
+ function widenedTypeKey(scan: SiblingPropertyScan, node: Node): string | undefined {
1390
+ switch (node.kind) {
1391
+ case SyntaxKind.StringLiteral:
1392
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
1393
+ case SyntaxKind.TemplateExpression:
1394
+ return 'string';
1395
+ case SyntaxKind.NumericLiteral:
1396
+ return 'number';
1397
+ case SyntaxKind.BigIntLiteral:
1398
+ return 'bigint';
1399
+ case SyntaxKind.TrueKeyword:
1400
+ case SyntaxKind.FalseKeyword:
1401
+ return 'boolean';
1402
+ case SyntaxKind.RegularExpressionLiteral:
1403
+ return 'RegExp';
1404
+ case SyntaxKind.PrefixUnaryExpression: {
1405
+ const unary = node as PrefixUnaryExpression;
1406
+ const operand = unary.operand.kind;
1407
+ if (unary.operator === SyntaxKind.MinusToken || unary.operator === SyntaxKind.PlusToken) {
1408
+ if (operand === SyntaxKind.NumericLiteral) return 'number';
1409
+ if (operand === SyntaxKind.BigIntLiteral && unary.operator === SyntaxKind.MinusToken) return 'bigint';
1410
+ }
1411
+ return undefined;
1412
+ }
1413
+ case SyntaxKind.PropertyAccessExpression:
1414
+ return enumKeyOfMemberAccess(scan, node);
1415
+ case SyntaxKind.ObjectLiteralExpression: {
1416
+ const members: string[] = [];
1417
+ for (const member of (node as ObjectLiteralExpression).properties.nodes) {
1418
+ if (member.kind !== SyntaxKind.PropertyAssignment) return undefined;
1419
+ const name = staticPropertyName(member);
1420
+ const value = widenedTypeKey(scan, (member as PropertyAssignment).initializer);
1421
+ if (name === undefined || value === undefined) return undefined;
1422
+ members.push(JSON.stringify(name) + ': ' + value);
1423
+ }
1424
+ members.sort();
1425
+ return '{' + members.join(', ') + '}';
1426
+ }
1427
+ }
1428
+ return undefined;
1429
+ }
1430
+
1431
+ /** The key of a member of an enum that the scan can't resolve without a checker. */
1432
+ const pendingEnumKey = 'enum ?';
1433
+
1434
+ /**
1435
+ * The key of `E.Member` where `E` names an enum in the scopes around the access (`getLocals`, as
1436
+ * `calledFunction` finds names) and `Member` is one of its members, not an export of a namespace merged
1437
+ * with it: the key of the enum. With a checker, `E` can be imported. Without one, an import, and a
1438
+ * global of a script file (whose enum other files can extend), give `pendingEnumKey`.
1439
+ */
1440
+ function enumKeyOfMemberAccess(scan: SiblingPropertyScan, node: Node): string | undefined {
1441
+ const target = getExpression(node)!;
1442
+ const member = getName(node)!;
1443
+ if (target.kind !== SyntaxKind.Identifier || member.kind !== SyntaxKind.Identifier) return undefined;
1444
+ const name = getText(target);
1445
+ const c = scan.c;
1446
+ for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
1447
+ let symbol = getLocals(scope)?.get(name);
1448
+ if (
1449
+ symbol === undefined ||
1450
+ (symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) === 0
1451
+ )
1452
+ continue;
1453
+ if (c === undefined) {
1454
+ if ((symbol.flags & SymbolFlags.Alias) !== 0) return pendingEnumKey;
1455
+ if (
1456
+ (symbol.flags & (SymbolFlags.Enum | SymbolFlags.Module)) !== 0 &&
1457
+ !isExternalModule(getSourceFileOfNode(node))
1458
+ )
1459
+ return pendingEnumKey;
1460
+ }
1461
+ if (symbol.exportSymbol !== undefined) symbol = symbol.exportSymbol;
1462
+ if (c !== undefined) {
1463
+ symbol = getMergedSymbol(c, symbol);
1464
+ if ((symbol.flags & SymbolFlags.Alias) !== 0) symbol = resolveAlias(c, symbol);
1465
+ }
1466
+ if ((symbol.flags & SymbolFlags.Enum) === 0) return undefined;
1467
+ // An enum merged with a namespace also exports the namespace's values.
1468
+ const memberSymbol = symbol.exports?.get(getText(member));
1469
+ return memberSymbol !== undefined && (memberSymbol.flags & SymbolFlags.EnumMember) !== 0
1470
+ ? scan.enumKey(symbol)
1471
+ : undefined;
1472
+ }
1473
+ return undefined;
1474
+ }
1475
+
1476
+ /**
1477
+ * Whether an object literal of a widened expression can have a contextual type: under `satisfies`,
1478
+ * an assertion or an argument, as a parameter's default, or returned from a function that can have
1479
+ * a contextual signature (a function expression that doesn't initialize an untyped declaration, or a
1480
+ * method of an object literal). Initializers of untyped declarations, the returns of function
1481
+ * declarations and class members, and the objects of property and element accesses have none.
1482
+ */
1483
+ function mayHaveContextualType(node: Node): boolean {
1484
+ for (let current = node.parent; current !== undefined; current = current.parent) {
1485
+ switch (current.kind) {
1486
+ case SyntaxKind.ParenthesizedExpression:
1487
+ case SyntaxKind.NonNullExpression:
1488
+ case SyntaxKind.AwaitExpression:
1489
+ case SyntaxKind.ConditionalExpression:
1490
+ case SyntaxKind.BinaryExpression:
1491
+ case SyntaxKind.ArrayLiteralExpression:
1492
+ case SyntaxKind.ObjectLiteralExpression:
1493
+ case SyntaxKind.PropertyAssignment:
1494
+ case SyntaxKind.ReturnStatement:
1495
+ case SyntaxKind.Block:
1496
+ case SyntaxKind.IfStatement:
1497
+ case SyntaxKind.DoStatement:
1498
+ case SyntaxKind.WhileStatement:
1499
+ case SyntaxKind.ForStatement:
1500
+ case SyntaxKind.ForInStatement:
1501
+ case SyntaxKind.ForOfStatement:
1502
+ case SyntaxKind.SwitchStatement:
1503
+ case SyntaxKind.CaseBlock:
1504
+ case SyntaxKind.CaseClause:
1505
+ case SyntaxKind.DefaultClause:
1506
+ case SyntaxKind.LabeledStatement:
1507
+ case SyntaxKind.TryStatement:
1508
+ case SyntaxKind.CatchClause:
1509
+ continue;
1510
+ case SyntaxKind.VariableDeclaration:
1511
+ case SyntaxKind.PropertyDeclaration:
1512
+ case SyntaxKind.ExportAssignment:
1513
+ case SyntaxKind.FunctionDeclaration:
1514
+ // The object of a property access, as in a method call, has no contextual type.
1515
+ case SyntaxKind.PropertyAccessExpression:
1516
+ case SyntaxKind.ElementAccessExpression:
1517
+ return false;
1518
+ case SyntaxKind.MethodDeclaration:
1519
+ case SyntaxKind.GetAccessor: {
1520
+ const parent = current.parent!.kind;
1521
+ return parent !== SyntaxKind.ClassDeclaration && parent !== SyntaxKind.ClassExpression;
1522
+ }
1523
+ case SyntaxKind.ArrowFunction:
1524
+ case SyntaxKind.FunctionExpression: {
1525
+ let outer = current.parent!;
1526
+ while (outer.kind === SyntaxKind.ParenthesizedExpression) outer = outer.parent!;
1527
+ return (
1528
+ (outer.kind !== SyntaxKind.VariableDeclaration && outer.kind !== SyntaxKind.PropertyDeclaration) ||
1529
+ getType(outer) !== undefined
1530
+ );
1531
+ }
1532
+ default:
1533
+ return true;
1534
+ }
1535
+ }
1536
+ return true;
1537
+ }
1538
+
1539
+ /** Collects the calls by one of `names` in an expression, innermost first, outside the functions in it. */
1540
+ class CallCollector extends NodeVisitor {
1541
+ readonly calls: Node[] = [];
1542
+
1543
+ constructor(private readonly names: Set<string>) {
1544
+ super();
1545
+ }
1546
+
1547
+ visitNode(node: Node): boolean {
1548
+ if (isFunctionLike(node) || node.kind === SyntaxKind.ClassExpression) return false;
1549
+ node.forEachChild(this);
1550
+ if (node.kind === SyntaxKind.CallExpression) {
1551
+ const name = calleeName(node);
1552
+ if (name !== undefined && this.names.has(name)) this.calls.push(node);
1553
+ }
1554
+ return false;
1555
+ }
1556
+ }
1557
+
1558
+ /** The name a call calls its function by, `f` in `f(...)` and `this.f(...)`, if the scan can resolve it. */
1559
+ function calleeName(call: Node): string | undefined {
1560
+ const callee = skipParentheses(getExpression(call)!);
1561
+ if (callee.kind === SyntaxKind.Identifier) return getText(callee);
1562
+ if (callee.kind !== SyntaxKind.PropertyAccessExpression || getExpression(callee)!.kind !== SyntaxKind.ThisKeyword)
1563
+ return undefined;
1564
+ const name = getName(callee)!;
1565
+ return name.kind === SyntaxKind.Identifier ? getText(name) : undefined;
1566
+ }
1567
+
1568
+ /**
1569
+ * The name calls can call a function without a return type by: a function declaration's, a method's or
1570
+ * accessor's, or that of the variable or property a function expression initializes.
1571
+ */
1572
+ function functionName(func: Node): string | undefined {
1573
+ let node = func;
1574
+ if (func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction) {
1575
+ while (node.parent?.kind === SyntaxKind.ParenthesizedExpression) node = node.parent;
1576
+ if (node.parent?.kind !== SyntaxKind.VariableDeclaration && node.parent?.kind !== SyntaxKind.PropertyDeclaration)
1577
+ return undefined;
1578
+ node = node.parent;
1579
+ }
1580
+ const name = getName(node);
1581
+ return name?.kind === SyntaxKind.Identifier ? getText(name) : undefined;
1582
+ }
1583
+
1584
+ /** Whether an expression's alternatives (`addAlternatives`) can hold object or array literals. */
1585
+ function mayHoldLiterals(node: Node): boolean {
1586
+ switch (skipParentheses(node).kind) {
1587
+ case SyntaxKind.ObjectLiteralExpression:
1588
+ case SyntaxKind.ArrayLiteralExpression:
1589
+ case SyntaxKind.ConditionalExpression:
1590
+ case SyntaxKind.BinaryExpression:
1591
+ case SyntaxKind.NonNullExpression:
1592
+ case SyntaxKind.SatisfiesExpression:
1593
+ case SyntaxKind.AwaitExpression:
1594
+ case SyntaxKind.AsExpression:
1595
+ case SyntaxKind.TypeAssertionExpression:
1596
+ return true;
1597
+ }
1598
+ return false;
1599
+ }
1600
+
1601
+ /** The class whose instance `this` is at `node`, through arrow functions, in a member or property initializer. */
1602
+ function thisClass(node: Node): Node | undefined {
1603
+ for (let current = node.parent; current !== undefined; current = current.parent) {
1604
+ if (current.kind === SyntaxKind.ArrowFunction) continue;
1605
+ if (
1606
+ current.kind === SyntaxKind.MethodDeclaration ||
1607
+ current.kind === SyntaxKind.GetAccessor ||
1608
+ current.kind === SyntaxKind.SetAccessor ||
1609
+ current.kind === SyntaxKind.Constructor ||
1610
+ current.kind === SyntaxKind.PropertyDeclaration
1611
+ ) {
1612
+ const parent = current.parent!;
1613
+ return parent.kind === SyntaxKind.ClassDeclaration || parent.kind === SyntaxKind.ClassExpression
1614
+ ? parent
1615
+ : undefined;
1616
+ }
1617
+ if (isFunctionLike(current) || current.kind === SyntaxKind.SourceFile) return undefined;
1618
+ }
1619
+ return undefined;
1620
+ }
1621
+
1622
+ /**
1623
+ * Adds the expressions whose types make up the type of `node`: both sides of `?:`, `||` and `??`,
1624
+ * and the elements of an array literal that `node` indexes when `arrays` (see SiblingPropertyScan).
1625
+ */
1626
+ function addAlternatives(node: Node, out: Node[], arrays: boolean): Node[] {
1627
+ switch (node.kind) {
1628
+ case SyntaxKind.ParenthesizedExpression:
1629
+ case SyntaxKind.NonNullExpression:
1630
+ case SyntaxKind.SatisfiesExpression:
1631
+ case SyntaxKind.AwaitExpression:
1632
+ return addAlternatives(getExpression(node)!, out, arrays);
1633
+ case SyntaxKind.AsExpression:
1634
+ case SyntaxKind.TypeAssertionExpression:
1635
+ // An object literal in an `as const` assertion is still an object literal type, with readonly
1636
+ // properties; an array literal in one is a tuple, whose elements widen separately.
1637
+ if (isConstAssertion(node) && skipParentheses(getExpression(node)!).kind === SyntaxKind.ObjectLiteralExpression)
1638
+ return addAlternatives(getExpression(node)!, out, arrays);
1639
+ break;
1640
+ case SyntaxKind.ConditionalExpression:
1641
+ addAlternatives((node as ConditionalExpression).whenTrue, out, arrays);
1642
+ return addAlternatives((node as ConditionalExpression).whenFalse, out, arrays);
1643
+ case SyntaxKind.BinaryExpression: {
1644
+ const binary = node as BinaryExpression;
1645
+ switch (binary.operatorToken.kind) {
1646
+ case SyntaxKind.BarBarToken:
1647
+ case SyntaxKind.QuestionQuestionToken:
1648
+ addAlternatives(binary.left, out, arrays);
1649
+ return addAlternatives(binary.right, out, arrays);
1650
+ case SyntaxKind.AmpersandAmpersandToken:
1651
+ case SyntaxKind.CommaToken:
1652
+ return addAlternatives(binary.right, out, arrays);
1653
+ }
1654
+ break;
1655
+ }
1656
+ case SyntaxKind.ElementAccessExpression: {
1657
+ // `[a, b][i]` has the type of the array's elements, which the declaration widens as one union.
1658
+ const array = skipParentheses(getExpression(node)!);
1659
+ if (!arrays || array.kind !== SyntaxKind.ArrayLiteralExpression) break;
1660
+ for (const e of (array as ArrayLiteralExpression).elements.nodes)
1661
+ if (e.kind !== SyntaxKind.SpreadElement) addAlternatives(e, out, arrays);
1662
+ return out;
1663
+ }
1664
+ }
1665
+ out.push(node);
1666
+ return out;
1667
+ }
1668
+
1669
+ /**
1670
+ * The alternatives of the elements of an array literal, whose types make its element type, with those
1671
+ * of the array literals that its spread elements spread. Used where array literals have an array type
1672
+ * (`SiblingPropertyScan.arrays`), as are `iteratedArrayLiterals` and `bindingAlternatives`.
1673
+ */
1674
+ function addArrayElements(array: ArrayLiteralExpression, out: Node[]): Node[] {
1675
+ for (const e of array.elements.nodes)
1676
+ if (e.kind !== SyntaxKind.SpreadElement) addAlternatives(e, out, true);
1677
+ else
1678
+ for (const spread of addAlternatives(getExpression(e)!, [], true))
1679
+ if (spread.kind === SyntaxKind.ArrayLiteralExpression) addArrayElements(spread as ArrayLiteralExpression, out);
1680
+ return out;
1681
+ }
1682
+
1683
+ /** The array literals whose elements a `for...of` over `expression` iterates, through `as const`. */
1684
+ function iteratedArrayLiterals(expression: Node): ArrayLiteralExpression[] {
1685
+ const arrays: ArrayLiteralExpression[] = [];
1686
+ for (const a of addAlternatives(expression, [], true)) {
1687
+ const value = isConstAssertion(a) ? skipParentheses(getExpression(a)!) : a;
1688
+ if (value.kind === SyntaxKind.ArrayLiteralExpression) arrays.push(value as ArrayLiteralExpression);
1689
+ }
1690
+ return arrays;
1691
+ }
1692
+
1693
+ function isConstAssertion(node: Node): boolean {
1694
+ return (
1695
+ (node.kind === SyntaxKind.AsExpression || node.kind === SyntaxKind.TypeAssertionExpression) &&
1696
+ isConstTypeReference(getType(node)!)
1697
+ );
1698
+ }
1699
+
1700
+ /**
1701
+ * The alternatives whose union each variable of a binding name is widened from, given the alternatives
1702
+ * of the iterated elements: all of them for an identifier; for an element of an array pattern, its
1703
+ * element of each `as const` array literal, or every element of another array literal; for an element
1704
+ * of an object pattern, the values of its property in each object literal.
1705
+ */
1706
+ function bindingAlternatives(name: Node, elements: Node[]): Node[][] {
1707
+ if (name.kind === SyntaxKind.Identifier) return [elements];
1708
+ const result: Node[][] = [];
1709
+ const isArrayPattern = name.kind === SyntaxKind.ArrayBindingPattern;
1710
+ const bindings = (name as BindingPattern).elements.nodes;
1711
+ for (let k = 0; k < bindings.length; k++) {
1712
+ const binding = bindings[k]!;
1713
+ if (binding.kind !== SyntaxKind.BindingElement) continue;
1714
+ const element = binding as BindingElement;
1715
+ if (element.dotDotDotToken !== undefined || element.initializer !== undefined) continue;
1716
+ if (element.name?.kind !== SyntaxKind.Identifier) continue;
1717
+ const alternatives: Node[] = [];
1718
+ if (isArrayPattern) {
1719
+ for (const e of elements) {
1720
+ const value = isConstAssertion(e) ? skipParentheses(getExpression(e)!) : e;
1721
+ if (value.kind !== SyntaxKind.ArrayLiteralExpression) continue;
1722
+ const array = value as ArrayLiteralExpression;
1723
+ if (value !== e || isInConstAssertion(array)) {
1724
+ const item = array.elements.nodes[k];
1725
+ if (item !== undefined && item.kind !== SyntaxKind.SpreadElement) addAlternatives(item, alternatives, true);
1726
+ } else addArrayElements(array, alternatives);
1727
+ }
1728
+ } else {
1729
+ const property = element.propertyName ?? element.name;
1730
+ const propertyName = property.kind === SyntaxKind.Identifier ? getText(property) : undefined;
1731
+ if (propertyName === undefined) continue;
1732
+ for (const e of elements) {
1733
+ if (e.kind !== SyntaxKind.ObjectLiteralExpression) continue;
1734
+ for (const member of (e as ObjectLiteralExpression).properties.nodes)
1735
+ if (member.kind === SyntaxKind.PropertyAssignment && staticPropertyName(member) === propertyName)
1736
+ addAlternatives((member as PropertyAssignment).initializer, alternatives, true);
1737
+ }
1738
+ }
1739
+ if (alternatives.length !== 0) result.push(alternatives);
1740
+ }
1741
+ return result;
1742
+ }
1743
+
1744
+ /** The name of an object literal member with a literal name. */
1745
+ function staticPropertyName(member: Node): string | undefined {
1746
+ switch (member.kind) {
1747
+ case SyntaxKind.PropertyAssignment:
1748
+ case SyntaxKind.ShorthandPropertyAssignment:
1749
+ case SyntaxKind.MethodDeclaration:
1750
+ case SyntaxKind.GetAccessor:
1751
+ case SyntaxKind.SetAccessor:
1752
+ break;
1753
+ default:
1754
+ return undefined;
1755
+ }
1756
+ const name = getName(member)!;
1757
+ switch (name.kind) {
1758
+ case SyntaxKind.Identifier:
1759
+ case SyntaxKind.StringLiteral:
1760
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
1761
+ return getText(name);
1762
+ case SyntaxKind.NumericLiteral:
1763
+ return String(Number(getText(name)));
1764
+ }
1765
+ return undefined;
1766
+ }
1767
+
1768
+ function getPropertiesOfContext(c: Checker, context: WideningContext): Symbol[] {
1769
+ if (context.resolvedProperties === undefined) {
1770
+ const names = new Map<string, Symbol>();
1771
+ for (const t of getSiblingsOfContext(c, context))
1772
+ if (
1773
+ (t.objectFlags & ObjectFlags.ObjectLiteral) !== 0 &&
1774
+ (t.flags & TypeFlags.Object) !== 0 &&
1775
+ (t.objectFlags & ObjectFlags.ContainsSpread) === 0
1776
+ )
1777
+ for (const prop of getPropertiesOfType(c, t)) names.set(prop.name, prop);
1778
+ context.resolvedProperties = [...names.values()];
1779
+ }
1780
+ return context.resolvedProperties;
1781
+ }
1782
+
1783
+ function getSiblingsOfContext(c: Checker, context: WideningContext): Type[] {
1784
+ if (context.siblings === undefined) {
1785
+ const siblings: Type[] = [];
1786
+ for (const t of getSiblingsOfContext(c, context.parent!))
1787
+ if ((t.objectFlags & ObjectFlags.ObjectLiteral) !== 0 && (t.flags & TypeFlags.Object) !== 0) {
1788
+ const prop = getPropertyOfObjectType(c, t, context.propertyName);
1789
+ if (prop !== undefined) {
1790
+ const propType = getTypeOfSymbol(c, prop);
1791
+ if ((propType.flags & TypeFlags.Union) !== 0) for (const u of typesOf(propType)) siblings.push(u);
1792
+ else siblings.push(propType);
1793
+ }
1794
+ }
1795
+ context.siblings = siblings;
1796
+ }
1797
+ return context.siblings;
1798
+ }
1799
+
1800
+ // ---- Optionality
1801
+
1802
+ export function addOptionality(c: Checker, t: Type, isProperty: boolean, isOptional: boolean): Type {
1803
+ return c.strictNullChecks && isOptional ? getOptionalType(c, t, isProperty) : t;
1804
+ }
1805
+
1806
+ export function getOptionalType(c: Checker, t: Type, isProperty: boolean): Type {
1807
+ const missingOrUndefined = isProperty ? c.undefinedOrMissingType : c.undefinedType;
1808
+ if (t === missingOrUndefined || ((t.flags & TypeFlags.Union) !== 0 && typesOf(t)[0]! === missingOrUndefined))
1809
+ return t;
1810
+ const key = cachedTypeKey(isProperty ? CachedTypeKind.OptionalProperty : CachedTypeKind.Optional, t.id);
1811
+ let result = c.cachedTypesById.get(key);
1812
+ if (result === undefined) {
1813
+ result = getUnionType(c, [t, missingOrUndefined]);
1814
+ c.cachedTypesById.set(key, result);
1815
+ }
1816
+ return result;
1817
+ }
1818
+
1819
+ export function addOptionalTypeMarker(c: Checker, t: Type): Type {
1820
+ return c.strictNullChecks ? getUnionType(c, [t, c.optionalType]) : t;
1821
+ }
1822
+
1823
+ export function removeOptionalTypeMarker(c: Checker, t: Type): Type {
1824
+ return c.strictNullChecks ? removeType(c, t, c.optionalType) : t;
1825
+ }
1826
+
1827
+ export function removeMissingType(c: Checker, t: Type, isOptional: boolean): Type {
1828
+ return c.exactOptionalPropertyTypes && isOptional ? removeType(c, t, c.missingType) : t;
1829
+ }
1830
+
1831
+ export function removeMissingOrUndefinedType(c: Checker, t: Type): Type {
1832
+ return c.exactOptionalPropertyTypes ? removeType(c, t, c.missingType) : getTypeWithFacts(c, t, TypeFacts.NEUndefined);
1833
+ }
1834
+
1835
+ export function getNullableType(c: Checker, t: Type, flags: TypeFlags): Type {
1836
+ const missing = flags & ~t.flags & (TypeFlags.Undefined | TypeFlags.Null);
1837
+ if (missing === 0) return t;
1838
+ if (missing === TypeFlags.Undefined) return getUnionType(c, [t, c.undefinedType]);
1839
+ if (missing === TypeFlags.Null) return getUnionType(c, [t, c.nullType]);
1840
+ return getUnionType(c, [t, c.undefinedType, c.nullType]);
1841
+ }
1842
+
1843
+ // ---- Readonly
1844
+
1845
+ export function isReadonlySymbol(c: Checker, symbol: Symbol): boolean {
1846
+ // Readonly properties, const variables, getters without setters and enum members.
1847
+ return (
1848
+ (symbol.checkFlags & CheckFlags.Readonly) !== 0 ||
1849
+ ((symbol.flags & SymbolFlags.Property) !== 0 &&
1850
+ (getDeclarationModifierFlags(c, symbol) & ModifierFlags.Readonly) !== 0) ||
1851
+ ((symbol.flags & SymbolFlags.Variable) !== 0 &&
1852
+ (getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Constant) !== 0) ||
1853
+ ((symbol.flags & SymbolFlags.Accessor) !== 0 && (symbol.flags & SymbolFlags.SetAccessor) === 0) ||
1854
+ (symbol.flags & SymbolFlags.EnumMember) !== 0 ||
1855
+ someReadonlyAssignmentDeclaration(c, symbol.declarations)
1856
+ );
1857
+ }
1858
+
1859
+ function someReadonlyAssignmentDeclaration(c: Checker, declarations: Node[]): boolean {
1860
+ for (const d of declarations) if (isReadonlyAssignmentDeclaration(c, d)) return true;
1861
+ return false;
1862
+ }
1863
+
1864
+ /** An `Object.defineProperty` call with a value and no `writable: true`, or without a setter. */
1865
+ function isReadonlyAssignmentDeclaration(c: Checker, node: Node): boolean {
1866
+ if (node.kind !== SyntaxKind.CallExpression) return false;
1867
+ const args = getArguments(node);
1868
+ if (args === undefined || args.nodes.length < 3) return false;
1869
+ const descriptorType = checkExpressionCached(c, args.nodes[2]!);
1870
+ if (getTypeOfPropertyOfType(c, descriptorType, 'value') !== undefined) {
1871
+ const writable = getPropertyOfType(c, descriptorType, 'writable');
1872
+ if (writable === undefined) return true;
1873
+ const declaration = writable.valueDeclaration;
1874
+ const writableType =
1875
+ declaration !== undefined && declaration.kind === SyntaxKind.PropertyAssignment
1876
+ ? checkExpression(c, getInitializer(declaration)!)
1877
+ : getTypeOfSymbol(c, writable);
1878
+ return writableType === c.falseType || writableType === c.regularFalseType;
1879
+ }
1880
+ return getTypeOfPropertyOfType(c, descriptorType, 'set') === undefined;
1881
+ }
1882
+
1883
+ import {
1884
+ getDeclarationModifierFlagsFromSymbol,
1885
+ getPropertyOfType,
1886
+ getTypeOfPropertyOfType,
1887
+ } from './checker-members.js';
1888
+ import { checkExpression } from './checker-expressions.js';
1889
+ import { getParameterTypeOfFullSignature } from './checker-jsdoc.js';
1890
+ import { isFunctionLike, isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
1891
+ import { isEmptyArrayLiteralType } from './relations.js';
1892
+ import { isCheckJsEnabledForFile } from './program-utilities.js';
1893
+
1894
+ function getDeclarationModifierFlags(c: Checker, symbol: Symbol): ModifierFlags {
1895
+ return getDeclarationModifierFlagsFromSymbol(c, symbol, false);
1896
+ }
1897
+
1898
+ function getDeclarationNodeFlagsFromSymbol(symbol: Symbol): NodeFlags {
1899
+ return symbol.valueDeclaration !== undefined ? getCombinedNodeFlags(symbol.valueDeclaration) : 0;
1900
+ }
1901
+
1902
+ // ---- Accessors
1903
+
1904
+ export function getAnnotatedAccessorTypeNode(accessor: Node | undefined): Node | undefined {
1905
+ if (accessor === undefined) return undefined;
1906
+ if (accessor.kind === SyntaxKind.GetAccessor || accessor.kind === SyntaxKind.PropertyDeclaration)
1907
+ return getType(accessor);
1908
+ if (accessor.kind === SyntaxKind.SetAccessor) {
1909
+ const params = getParameters(accessor)?.nodes ?? [];
1910
+ if (params.length === 0) return undefined;
1911
+ return getType(params.length === 2 ? params[1]! : params[0]!);
1912
+ }
1913
+ return undefined;
1914
+ }
1915
+
1916
+ function getAnnotatedAccessorType(c: Checker, accessor: Node | undefined): Type | undefined {
1917
+ const node = getAnnotatedAccessorTypeNode(accessor);
1918
+ return node !== undefined ? getTypeFromTypeNode(c, node) : undefined;
1919
+ }
1920
+
1921
+ /** The `accessor` property declaration of `symbol`, if any. */
1922
+ function getAutoAccessorDeclaration(symbol: Symbol): Node | undefined {
1923
+ for (const d of symbol.declarations)
1924
+ if (d.kind === SyntaxKind.PropertyDeclaration && (getCombinedModifierFlags(d) & ModifierFlags.Accessor) !== 0)
1925
+ return d;
1926
+ return undefined;
1927
+ }
1928
+
1929
+ function getTypeOfAccessors(c: Checker, symbol: Symbol): Type {
1930
+ const id = getSymbolId(symbol);
1931
+ const type = c.symbolTypes.get(id);
1932
+ if (type !== undefined) return type;
1933
+ if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.Type)) return c.errorType;
1934
+ const getter = symbol.declarations.find((d) => d.kind === SyntaxKind.GetAccessor);
1935
+ const setter = symbol.declarations.find((d) => d.kind === SyntaxKind.SetAccessor);
1936
+ const accessor = getAutoAccessorDeclaration(symbol);
1937
+ // Getter annotation, setter annotation, accessor annotation, then getter body.
1938
+ let t =
1939
+ getAnnotatedAccessorType(c, getter) ?? getAnnotatedAccessorType(c, setter) ?? getAnnotatedAccessorType(c, accessor);
1940
+ if (t === undefined && getter !== undefined && getBody(getter) !== undefined) t = getReturnTypeFromBody(c, getter);
1941
+ if (t === undefined && accessor !== undefined) t = getWidenedTypeForVariableLikeDeclaration(c, accessor, true);
1942
+ if (t === undefined) {
1943
+ if (setter !== undefined && !isPrivateWithinAmbient(setter))
1944
+ c.addErrorOrSuggestion(
1945
+ c.noImplicitAny,
1946
+ createDiagnosticForNode(
1947
+ setter,
1948
+ Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation,
1949
+ [symbolToString(c, symbol)],
1950
+ ),
1951
+ );
1952
+ else if (getter !== undefined && !isPrivateWithinAmbient(getter))
1953
+ c.addErrorOrSuggestion(
1954
+ c.noImplicitAny,
1955
+ createDiagnosticForNode(
1956
+ getter,
1957
+ Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation,
1958
+ [symbolToString(c, symbol)],
1959
+ ),
1960
+ );
1961
+ t = c.anyType;
1962
+ }
1963
+ if (!popTypeResolution(c)) {
1964
+ if (getAnnotatedAccessorTypeNode(getter) !== undefined)
1965
+ c.error(
1966
+ getter,
1967
+ Diagnostics.X_0_is_referenced_directly_or_indirectly_in_its_own_type_annotation,
1968
+ symbolToString(c, symbol),
1969
+ );
1970
+ else if (getAnnotatedAccessorTypeNode(setter) !== undefined)
1971
+ c.error(
1972
+ setter,
1973
+ Diagnostics.X_0_is_referenced_directly_or_indirectly_in_its_own_type_annotation,
1974
+ symbolToString(c, symbol),
1975
+ );
1976
+ else if (getter !== undefined && c.noImplicitAny)
1977
+ c.error(
1978
+ getter,
1979
+ Diagnostics.X_0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions,
1980
+ symbolToString(c, symbol),
1981
+ );
1982
+ t = c.anyType;
1983
+ }
1984
+ let cached = c.symbolTypes.get(id);
1985
+ if (cached === undefined) {
1986
+ cached = t;
1987
+ c.symbolTypes.set(id, cached);
1988
+ }
1989
+ return cached;
1990
+ }
1991
+
1992
+ export function getWriteTypeOfAccessors(c: Checker, symbol: Symbol): Type {
1993
+ const links = c.symbolLinks(symbol);
1994
+ if (links.writeType !== undefined) return links.writeType;
1995
+ if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.WriteType)) return c.errorType;
1996
+ const setter =
1997
+ symbol.declarations.find((d) => d.kind === SyntaxKind.SetAccessor) ?? getAutoAccessorDeclaration(symbol);
1998
+ let writeType = getAnnotatedAccessorType(c, setter);
1999
+ if (!popTypeResolution(c)) {
2000
+ if (getAnnotatedAccessorTypeNode(setter) !== undefined)
2001
+ c.error(
2002
+ setter,
2003
+ Diagnostics.X_0_is_referenced_directly_or_indirectly_in_its_own_type_annotation,
2004
+ symbolToString(c, symbol),
2005
+ );
2006
+ writeType = c.anyType;
2007
+ }
2008
+ // Without a setter annotation, the write type is the read type.
2009
+ if (links.writeType === undefined) links.writeType = writeType ?? getTypeOfAccessors(c, symbol);
2010
+ return links.writeType;
2011
+ }
2012
+
2013
+ // ---- Functions, classes, enums, modules and aliases
2014
+
2015
+ function getTypeOfFuncClassEnumModule(c: Checker, symbol: Symbol): Type {
2016
+ const id = getSymbolId(symbol);
2017
+ let type = c.symbolTypes.get(id);
2018
+ if (type === undefined) {
2019
+ type = getTypeOfFuncClassEnumModuleWorker(c, symbol);
2020
+ c.symbolTypes.set(id, type);
2021
+ }
2022
+ return type;
2023
+ }
2024
+
2025
+ function getTypeOfFuncClassEnumModuleWorker(c: Checker, symbol: Symbol): Type {
2026
+ if ((symbol.flags & SymbolFlags.Module) !== 0 && isShorthandAmbientModuleSymbol(symbol)) return c.anyType;
2027
+ if (
2028
+ (symbol.flags & SymbolFlags.ValueModule) !== 0 &&
2029
+ symbol.valueDeclaration !== undefined &&
2030
+ symbol.valueDeclaration.kind === SyntaxKind.SourceFile
2031
+ ) {
2032
+ const resolvedModule = resolveExternalModuleSymbol(c, symbol, false);
2033
+ if (resolvedModule !== undefined && resolvedModule !== symbol) return getTypeOfSymbol(c, resolvedModule);
2034
+ }
2035
+ const t = newObjectType(c, ObjectFlags.Anonymous, symbol);
2036
+ if ((symbol.flags & SymbolFlags.Class) !== 0) {
2037
+ const baseTypeVariable = getBaseTypeVariableOfClass(c, symbol);
2038
+ return baseTypeVariable !== undefined ? getIntersectionType(c, [t, baseTypeVariable]) : t;
2039
+ }
2040
+ if (c.strictNullChecks && (symbol.flags & SymbolFlags.Optional) !== 0) return getOptionalType(c, t, true);
2041
+ return t;
2042
+ }
2043
+
2044
+ function isShorthandAmbientModuleSymbol(symbol: Symbol): boolean {
2045
+ const d = symbol.valueDeclaration;
2046
+ return d !== undefined && d.kind === SyntaxKind.ModuleDeclaration && getBody(d) === undefined;
2047
+ }
2048
+
2049
+ export function getBaseTypeVariableOfClass(c: Checker, symbol: Symbol): Type | undefined {
2050
+ const baseConstructorType = getBaseConstructorTypeOfClass(c, getDeclaredTypeOfClassOrInterface(c, symbol));
2051
+ if ((baseConstructorType.flags & TypeFlags.TypeVariable) !== 0) return baseConstructorType;
2052
+ if ((baseConstructorType.flags & TypeFlags.Intersection) !== 0)
2053
+ return typesOf(baseConstructorType).find((t) => (t.flags & TypeFlags.TypeVariable) !== 0);
2054
+ return undefined;
2055
+ }
2056
+
2057
+ function getTypeOfEnumMember(c: Checker, symbol: Symbol): Type {
2058
+ const id = getSymbolId(symbol);
2059
+ let type = c.symbolTypes.get(id);
2060
+ if (type === undefined) {
2061
+ type = getDeclaredTypeOfEnumMember(c, symbol);
2062
+ c.symbolTypes.set(id, type);
2063
+ }
2064
+ return type;
2065
+ }
2066
+
2067
+ function getTypeOfAlias(c: Checker, symbol: Symbol): Type {
2068
+ const id = getSymbolId(symbol);
2069
+ const type = c.symbolTypes.get(id);
2070
+ if (type !== undefined) return type;
2071
+ if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.Type)) return c.errorType;
2072
+ const targetSymbol = resolveAlias(c, symbol);
2073
+ const exportSymbol = getTargetOfAliasDeclaration(c, getDeclarationOfAliasSymbol(symbol));
2074
+ // Only values have types.
2075
+ if (c.symbolTypes.get(id) === undefined)
2076
+ c.symbolTypes.set(
2077
+ id,
2078
+ (getSymbolFlags(c, targetSymbol) & SymbolFlags.Value) !== 0 ? getTypeOfSymbol(c, targetSymbol) : c.errorType,
2079
+ );
2080
+ if (!popTypeResolution(c)) {
2081
+ reportCircularityError(c, exportSymbol ?? symbol);
2082
+ if (c.symbolTypes.get(id) === undefined) c.symbolTypes.set(id, c.errorType);
2083
+ }
2084
+ return c.symbolTypes.get(id)!;
2085
+ }
2086
+
2087
+ function getTypeOfPrototypeProperty(c: Checker, prototype: Symbol): Type {
2088
+ // Every class has a `prototype` of its instance type with `any` type arguments.
2089
+ const classType = getDeclaredTypeOfSymbol(c, getParentOfSymbol(c, prototype)!) as InterfaceType;
2090
+ const typeParameters = classType.typeParameters();
2091
+ return typeParameters.length !== 0
2092
+ ? createTypeReference(
2093
+ c,
2094
+ classType,
2095
+ typeParameters.map(() => c.anyType),
2096
+ )
2097
+ : classType;
2098
+ }
2099
+
2100
+ export { getDeclarationOfAliasSymbol, filterType, hasStaticModifier };
2101
+ export type { UnionType };
2102
+
2103
+ /** `Extract<t, string>`, or string without the global `Extract`. */
2104
+ export function getExtractStringType(c: Checker, t: Type): Type {
2105
+ const extract = getGlobalTypeAliasSymbol(c, 'Extract', 2, true);
2106
+ return extract !== undefined ? getTypeAliasInstantiation(c, extract, [t, c.stringType], undefined) : c.stringType;
2107
+ }