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,2501 @@
1
+ // Creating and interning types: object types, literals, unions, intersections, references,
2
+ // arrays and tuples. Interning keys follow tsgo's, so identity matches.
3
+ import { sideOf, sideOfIfAny } from './side-state.js';
4
+ import { getNodeId, getName, getText } from './ast.js';
5
+ import { symbolToString } from './type-printer.js';
6
+ import { CompositeTypeMapper, instantiateTypes } from './instantiation.js';
7
+ import { getAliasForTypeNode, getTypeArgumentsOfDeferredReference } from './checker-type-nodes.js';
8
+ import { popTypeResolution, pushTypeResolution } from './checker-symbols.js';
9
+ import { isGenericIndexType } from './checker-type-operators.js';
10
+ import { compareCodePoints } from './diagnostics.js';
11
+ import { getSymbolId } from './symbol.js';
12
+ import { cachedTypeKey, EvolvingArrayType, IndexInfo, IndexType, IndexedAccessType, InstantiationExpressionType, InterfaceType, IntersectionType, MappedType, ObjectType, ReverseMappedType, SubstitutionType, TemplateLiteralType, TupleElementInfo, TupleType, TypeAlias, TypeReference, UnionType, ArrayTypeMapper, ArrayToSingleTypeMapper, MergedTypeMapper, SimpleTypeMapper, emptyIndexInfos, emptySignatures, emptySymbols, emptyTypes, ReferenceCache, } from './checker-types.js';
13
+ import { getNamedMembers, getMembersOfSymbol } from './checker-symbols.js';
14
+ import { getBaseConstraintOfType, getBaseConstraintOrType, getIndexTypeOfType, getPropertiesOfType, getTypeOfPropertyOfType, isArrayLikeType, resolveStructuredTypeMembers, hasResolvedProperties, memberResolutionFlags, membersHolder, } from './checker-members.js';
15
+ import { getTypeOfSymbol } from './checker-symbol-types.js';
16
+ import { getDeclaredTypeOfSymbol, getParentOfSymbol } from './checker-declared-types.js';
17
+ import { isTypeRelatedTo, isTypeStrictSubtypeOf, isTypeSubtypeOf, isTypeDerivedFrom } from './relations.js';
18
+ import { getIndexedAccessType, isGenericMappedType, isTypeMatchedByTemplateLiteralType, isMemberOfStringMapping, } from './checker-type-operators.js';
19
+ import { addOptionality } from './checker-symbol-types.js';
20
+ import { getSourceFileOfNode } from './binder-utilities.js';
21
+ import { isPartOfTypeNode } from './transformers/alias-references.js';
22
+ // ---- Keys
23
+ export function typeListKey(types) {
24
+ let key = '';
25
+ for (let i = 0; i < types.length; i++)
26
+ key += (i === 0 ? '' : ',') + types[i].id;
27
+ return key;
28
+ }
29
+ export function aliasKey(alias) {
30
+ return alias === undefined ? '' : '@' + getSymbolId(alias.symbol) + ':' + typeListKey(alias.typeArguments);
31
+ }
32
+ function unionKey(types, origin, alias) {
33
+ let key;
34
+ if (origin === undefined)
35
+ key = typeListKey(types);
36
+ else if ((origin.flags & 134217728 /* TypeFlags.Union */) !== 0)
37
+ key = '|' + typeListKey(origin.types);
38
+ else if ((origin.flags & 268435456 /* TypeFlags.Intersection */) !== 0)
39
+ key = '&' + typeListKey(origin.types);
40
+ else
41
+ key = '#' + origin.id + '|' + typeListKey(types);
42
+ return key + aliasKey(alias);
43
+ }
44
+ /** The list `unionKey` spells for `types` and `origin`. */
45
+ function unionKeyTypes(types, origin) {
46
+ return origin !== undefined && (origin.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0
47
+ ? origin.types
48
+ : types;
49
+ }
50
+ /** The kind of `unionKey`'s spelling: no origin, a union origin, an intersection origin, another origin. */
51
+ function unionKeyKind(origin) {
52
+ if (origin === undefined)
53
+ return 0;
54
+ if ((origin.flags & 134217728 /* TypeFlags.Union */) !== 0)
55
+ return 1;
56
+ if ((origin.flags & 268435456 /* TypeFlags.Intersection */) !== 0)
57
+ return 2;
58
+ return 3;
59
+ }
60
+ /**
61
+ * A number for `unionKey(types, origin, alias)` without spelling it: two 32-bit hashes of the same
62
+ * parts, combined below 2^53. Equal keys hash alike; unequal keys rarely do, and a union found by
63
+ * hash is checked against the key (`isUnionOfKey`).
64
+ */
65
+ function unionKeyHash(types, origin, alias) {
66
+ const kind = unionKeyKind(origin);
67
+ let h1 = 0x811c9dc5 ^ kind;
68
+ let h2 = 0x9e3779b9 + kind;
69
+ if (kind === 3) {
70
+ h1 = Math.imul(h1 ^ origin.id, 0x01000193);
71
+ h2 = Math.imul(h2 + origin.id, 0x85ebca6b);
72
+ }
73
+ const list = unionKeyTypes(types, origin);
74
+ for (let i = 0; i < list.length; i++) {
75
+ const id = list[i].id;
76
+ h1 = Math.imul(h1 ^ id, 0x01000193);
77
+ h2 = Math.imul(h2 + id, 0x85ebca6b) ^ (h2 >>> 15);
78
+ }
79
+ if (alias !== undefined) {
80
+ h1 = Math.imul(h1 ^ (getSymbolId(alias.symbol) + 0x40000000), 0x01000193);
81
+ for (const t of alias.typeArguments) {
82
+ h1 = Math.imul(h1 ^ t.id, 0x01000193);
83
+ h2 = Math.imul(h2 + t.id, 0x85ebca6b) ^ (h2 >>> 15);
84
+ }
85
+ h2 = Math.imul(h2 ^ alias.typeArguments.length, 0xc2b2ae35);
86
+ }
87
+ h1 ^= h1 >>> 16;
88
+ h1 = Math.imul(h1, 0x7feb352d);
89
+ h1 ^= h1 >>> 15;
90
+ return (h1 >>> 0) * 0x100000 + ((h2 ^ list.length) & 0xfffff);
91
+ }
92
+ /**
93
+ * The hash a `TypeListCache` finds `types`, `alias` and `tag` by: two 32-bit hashes of the ids,
94
+ * combined below 2^53. Takes the alias symbol's id as `aliasKey` would.
95
+ */
96
+ export function typeListHash(types, alias, tag) {
97
+ let h1 = 0x811c9dc5 ^ tag;
98
+ let h2 = 0x9e3779b9 + tag;
99
+ for (let i = 0; i < types.length; i++) {
100
+ const id = types[i].id;
101
+ h1 = Math.imul(h1 ^ id, 0x01000193);
102
+ h2 = Math.imul(h2 + id, 0x85ebca6b) ^ (h2 >>> 15);
103
+ }
104
+ if (alias !== undefined) {
105
+ h1 = Math.imul(h1 ^ (getSymbolId(alias.symbol) + 0x40000000), 0x01000193);
106
+ for (const t of alias.typeArguments) {
107
+ h1 = Math.imul(h1 ^ t.id, 0x01000193);
108
+ h2 = Math.imul(h2 + t.id, 0x85ebca6b) ^ (h2 >>> 15);
109
+ }
110
+ h2 = Math.imul(h2 ^ alias.typeArguments.length, 0xc2b2ae35);
111
+ }
112
+ h1 ^= h1 >>> 16;
113
+ h1 = Math.imul(h1, 0x7feb352d);
114
+ h1 ^= h1 >>> 15;
115
+ return (h1 >>> 0) * 0x100000 + ((h2 ^ types.length) & 0xfffff);
116
+ }
117
+ /** Ids below this pack in pairs into exact numbers (`getUnionTypeEx`). */
118
+ const UnionPairIdLimit = 1 << 24;
119
+ function sameTypeLists(a, b) {
120
+ if (a.length !== b.length)
121
+ return false;
122
+ for (let i = 0; i < a.length; i++)
123
+ if (a[i] !== b[i])
124
+ return false;
125
+ return true;
126
+ }
127
+ /** Whether `union` was created for `unionKey(types, origin, alias)`. */
128
+ function isUnionOfKey(union, types, origin, alias) {
129
+ const kind = unionKeyKind(origin);
130
+ const own = union.origin;
131
+ if (unionKeyKind(own) !== kind)
132
+ return false;
133
+ if (kind === 3 && own !== origin)
134
+ return false;
135
+ if (!sameTypeLists(unionKeyTypes(union.types, own), unionKeyTypes(types, origin)))
136
+ return false;
137
+ const ownAlias = union.alias;
138
+ if (alias === undefined || ownAlias === undefined)
139
+ return alias === ownAlias;
140
+ return ownAlias.symbol === alias.symbol && sameTypeLists(ownAlias.typeArguments, alias.typeArguments);
141
+ }
142
+ // ---- Constructors
143
+ export function newObjectType(c, objectFlags, symbol) {
144
+ const id = c.nextTypeId();
145
+ const flags = objectFlags &
146
+ ~(524288 /* ObjectFlags.CouldContainTypeVariablesComputed */ |
147
+ 1048576 /* ObjectFlags.CouldContainTypeVariables */ |
148
+ 2097152 /* ObjectFlags.MembersResolved */ |
149
+ -2147483648 /* ObjectFlags.SideState */);
150
+ if ((objectFlags & 3 /* ObjectFlags.ClassOrInterface */) !== 0) {
151
+ const declared = new InterfaceType(flags, id, symbol);
152
+ declared.checking = c;
153
+ return declared;
154
+ }
155
+ if ((objectFlags & 8 /* ObjectFlags.Tuple */) !== 0) {
156
+ const tuple = new TupleType(flags, id);
157
+ tuple.checking = c;
158
+ return tuple;
159
+ }
160
+ if ((objectFlags & 4 /* ObjectFlags.Reference */) !== 0)
161
+ return new TypeReference(flags, id, symbol);
162
+ if ((objectFlags & 32 /* ObjectFlags.Mapped */) !== 0)
163
+ return new MappedType(flags, id, symbol);
164
+ if ((objectFlags & 1024 /* ObjectFlags.ReverseMapped */) !== 0)
165
+ return new ReverseMappedType(flags, id);
166
+ if ((objectFlags & 256 /* ObjectFlags.EvolvingArray */) !== 0)
167
+ return new EvolvingArrayType(flags, id);
168
+ if ((objectFlags & 16777216 /* ObjectFlags.InstantiationExpressionType */) !== 0)
169
+ return new InstantiationExpressionType(flags, id, symbol);
170
+ return new ObjectType(flags, id, symbol);
171
+ }
172
+ export function newAnonymousType(c, symbol) {
173
+ const type = newObjectType(c, 16 /* ObjectFlags.Anonymous */, symbol);
174
+ setStructuredTypeMembers(c, type, undefined, emptySignatures, emptySignatures, emptyIndexInfos);
175
+ return type;
176
+ }
177
+ export function createAnonymousType(c, symbol, members, callSignatures, constructSignatures, indexInfos) {
178
+ const type = newObjectType(c, 16 /* ObjectFlags.Anonymous */, symbol);
179
+ setStructuredTypeMembers(c, type, members, callSignatures, constructSignatures, indexInfos);
180
+ return type;
181
+ }
182
+ export function setStructuredTypeMembers(c, t, members, callSignatures, constructSignatures, indexInfos) {
183
+ const type = membersHolder(c, t);
184
+ type.objectFlags |= 2097152 /* ObjectFlags.MembersResolved */;
185
+ type.pendingMembers = undefined;
186
+ type.members = members;
187
+ // Resolved member lists are never modified, so every empty one shares one array.
188
+ const properties = getNamedMembers(c, members, type.symbol);
189
+ type.properties = properties.length === 0 ? emptySymbols : properties;
190
+ type.callSignatures = callSignatures.length === 0 ? emptySignatures : callSignatures;
191
+ type.constructSignatures = constructSignatures.length === 0 ? emptySignatures : constructSignatures;
192
+ type.indexInfos = indexInfos.length === 0 ? emptyIndexInfos : indexInfos;
193
+ }
194
+ export function newUnionType(c, objectFlags, types) {
195
+ return new UnionType(objectFlags, c.nextTypeId(), types);
196
+ }
197
+ export function newIntersectionType(c, objectFlags, types) {
198
+ return new IntersectionType(objectFlags, c.nextTypeId(), types);
199
+ }
200
+ export function newIndexType(c, target, indexFlags) {
201
+ return new IndexType(c.nextTypeId(), target, indexFlags);
202
+ }
203
+ export function newIndexedAccessType(c, objectType, indexType, accessFlags) {
204
+ return new IndexedAccessType(c.nextTypeId(), objectType, indexType, accessFlags);
205
+ }
206
+ export function newSubstitutionType(c, baseType, constraint) {
207
+ return new SubstitutionType(c.nextTypeId(), baseType, constraint);
208
+ }
209
+ export function newIndexInfo(keyType, valueType, isReadonly, declaration) {
210
+ return new IndexInfo(keyType, valueType, isReadonly, declaration, []);
211
+ }
212
+ /** Propagates widening, literal and non-inferrable flags from constituents. */
213
+ export function getPropagatingFlagsOfTypes(types, excludeKinds) {
214
+ let result = 0;
215
+ for (let i = 0; i < types.length; i++) {
216
+ const t = types[i];
217
+ if ((t.flags & excludeKinds) === 0)
218
+ result |= t.objectFlags;
219
+ }
220
+ return result & 458752 /* ObjectFlags.PropagatingFlags */;
221
+ }
222
+ // ---- Literals
223
+ export function getStringLiteralType(c, value) {
224
+ let type = c.stringLiteralTypes.get(value);
225
+ if (type === undefined) {
226
+ type = c.newLiteralType(1024 /* TypeFlags.StringLiteral */, value, 0, undefined);
227
+ c.stringLiteralTypes.set(value, type);
228
+ }
229
+ return type;
230
+ }
231
+ export function getNumberLiteralType(c, value) {
232
+ if (value !== value) {
233
+ let type = c.nanLiteralType;
234
+ if (type === undefined) {
235
+ type = c.newLiteralType(2048 /* TypeFlags.NumberLiteral */, 'NaN', value, undefined);
236
+ c.nanLiteralType = type;
237
+ }
238
+ return type;
239
+ }
240
+ // -0 and 0 are the same key in a Map, as in Go.
241
+ let type = c.numberLiteralTypes.get(value);
242
+ if (type === undefined) {
243
+ type = c.newLiteralType(2048 /* TypeFlags.NumberLiteral */, '', value, undefined);
244
+ c.numberLiteralTypes.set(value, type);
245
+ }
246
+ return type;
247
+ }
248
+ /** `value` is the decimal digits of the bigint, with a leading `-` when negative. */
249
+ export function getBigIntLiteralType(c, value) {
250
+ let type = c.bigintLiteralTypes.get(value);
251
+ if (type === undefined) {
252
+ type = c.newLiteralType(4096 /* TypeFlags.BigIntLiteral */, value, 0, undefined);
253
+ c.bigintLiteralTypes.set(value, type);
254
+ }
255
+ return type;
256
+ }
257
+ export function getStringEnumLiteralType(c, value, enumSymbol, symbol) {
258
+ const key = getSymbolId(enumSymbol) + 's' + value;
259
+ let type = c.enumLiteralTypes.get(key);
260
+ if (type === undefined) {
261
+ type = c.newLiteralType(32768 /* TypeFlags.EnumLiteral */ | 1024 /* TypeFlags.StringLiteral */, value, 0, symbol);
262
+ c.enumLiteralTypes.set(key, type);
263
+ }
264
+ return type;
265
+ }
266
+ export function getNumberEnumLiteralType(c, value, enumSymbol, symbol) {
267
+ const key = getSymbolId(enumSymbol) + 'n' + (value !== value ? 'NaN' : Object.is(value, -0) ? '0' : String(value));
268
+ let type = c.enumLiteralTypes.get(key);
269
+ if (type === undefined) {
270
+ type = c.newLiteralType(32768 /* TypeFlags.EnumLiteral */ | 2048 /* TypeFlags.NumberLiteral */, '', value, symbol);
271
+ c.enumLiteralTypes.set(key, type);
272
+ }
273
+ return type;
274
+ }
275
+ export function isFreshLiteralType(type) {
276
+ if ((type.flags & 80896 /* TypeFlags.Freshable */) === 0)
277
+ return false;
278
+ // An identity test, like tsgo's: comparing ids would load the fresh type as well.
279
+ return type.freshType === type;
280
+ }
281
+ export function getFreshTypeOfLiteralType(c, type) {
282
+ if ((type.flags & 80896 /* TypeFlags.Freshable */) === 0)
283
+ return type;
284
+ const literal = type;
285
+ // The fresh twin of a literal created with one is inline; others are created on first use.
286
+ if (literal.freshType !== undefined)
287
+ return literal.freshType;
288
+ const side = sideOf(c, literal);
289
+ const existing = side.freshType;
290
+ if (existing !== undefined)
291
+ return existing;
292
+ const fresh = c.newLiteralType(type.flags, literal.stringValue, literal.numberValue, type.symbol);
293
+ fresh.regularType = literal;
294
+ fresh.freshType = fresh;
295
+ side.freshType = fresh;
296
+ return fresh;
297
+ }
298
+ export function getRegularTypeOfLiteralType(c, type) {
299
+ if ((type.flags & 80896 /* TypeFlags.Freshable */) !== 0)
300
+ return type.regularType;
301
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0) {
302
+ const side = sideOf(c, type);
303
+ let regular = side.regularType;
304
+ if (regular === undefined) {
305
+ regular = mapType(c, type, (t) => getRegularTypeOfLiteralType(c, t));
306
+ side.regularType = regular;
307
+ }
308
+ return regular;
309
+ }
310
+ return type;
311
+ }
312
+ export function isUnitType(type) {
313
+ return (type.flags & 97292 /* TypeFlags.Unit */) !== 0;
314
+ }
315
+ export function isLiteralType(type) {
316
+ if ((type.flags & 256 /* TypeFlags.Boolean */) !== 0)
317
+ return true;
318
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0) {
319
+ if ((type.flags & 32768 /* TypeFlags.EnumLiteral */) !== 0)
320
+ return true;
321
+ for (const t of type.types)
322
+ if (!isUnitType(t))
323
+ return false;
324
+ return true;
325
+ }
326
+ return isUnitType(type);
327
+ }
328
+ export function getBaseTypeOfEnumLikeType(c, type) {
329
+ if ((type.flags & 98304 /* TypeFlags.EnumLike */) !== 0 &&
330
+ type.symbol !== undefined &&
331
+ (type.symbol.flags & 8 /* SymbolFlags.EnumMember */) !== 0)
332
+ return getDeclaredTypeOfSymbol(c, getParentOfSymbol(c, type.symbol));
333
+ return type;
334
+ }
335
+ export function getBaseTypeOfLiteralType(c, type) {
336
+ const flags = type.flags;
337
+ if ((flags & 98304 /* TypeFlags.EnumLike */) !== 0)
338
+ return getBaseTypeOfEnumLikeType(c, type);
339
+ if ((flags & (1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0)
340
+ return c.stringType;
341
+ if ((flags & 2048 /* TypeFlags.NumberLiteral */) !== 0)
342
+ return c.numberType;
343
+ if ((flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0)
344
+ return c.bigintType;
345
+ if ((flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0)
346
+ return c.booleanType;
347
+ if ((flags & 134217728 /* TypeFlags.Union */) !== 0) {
348
+ const key = cachedTypeKey(0 /* CachedTypeKind.LiteralUnionBaseType */, type.id);
349
+ let cached = c.cachedTypesById.get(key);
350
+ if (cached === undefined) {
351
+ cached = mapType(c, type, (t) => getBaseTypeOfLiteralType(c, t));
352
+ c.cachedTypesById.set(key, cached);
353
+ }
354
+ return cached;
355
+ }
356
+ return type;
357
+ }
358
+ export function getBaseTypeOfLiteralTypeForComparison(c, type) {
359
+ const flags = type.flags;
360
+ if ((flags & (1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0)
361
+ return c.stringType;
362
+ if ((flags & (2048 /* TypeFlags.NumberLiteral */ | 65536 /* TypeFlags.Enum */)) !== 0)
363
+ return c.numberType;
364
+ if ((flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0)
365
+ return c.bigintType;
366
+ if ((flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0)
367
+ return c.booleanType;
368
+ if ((flags & 134217728 /* TypeFlags.Union */) !== 0)
369
+ return mapType(c, type, (t) => getBaseTypeOfLiteralTypeForComparison(c, t));
370
+ return type;
371
+ }
372
+ export function getWidenedLiteralType(c, type) {
373
+ const flags = type.flags;
374
+ if ((flags & 134217728 /* TypeFlags.Union */) !== 0)
375
+ return mapType(c, type, (t) => getWidenedLiteralType(c, t));
376
+ if (!isFreshLiteralType(type))
377
+ return type;
378
+ if ((flags & 98304 /* TypeFlags.EnumLike */) !== 0)
379
+ return getBaseTypeOfEnumLikeType(c, type);
380
+ if ((flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
381
+ return c.stringType;
382
+ if ((flags & 2048 /* TypeFlags.NumberLiteral */) !== 0)
383
+ return c.numberType;
384
+ if ((flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0)
385
+ return c.bigintType;
386
+ if ((flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0)
387
+ return c.booleanType;
388
+ return type;
389
+ }
390
+ export function getWidenedUniqueESSymbolType(c, type) {
391
+ if ((type.flags & 16384 /* TypeFlags.UniqueESSymbol */) !== 0)
392
+ return c.esSymbolType;
393
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0)
394
+ return mapType(c, type, (t) => getWidenedUniqueESSymbolType(c, t));
395
+ return type;
396
+ }
397
+ // ---- Ordering (tsgo's CompareTypes)
398
+ export function compareNodes(c, n1, n2) {
399
+ if (n1 === n2)
400
+ return 0;
401
+ if (n1 === undefined)
402
+ return 1;
403
+ if (n2 === undefined)
404
+ return -1;
405
+ const s1 = sourceFileOf(n1);
406
+ const s2 = sourceFileOf(n2);
407
+ if (s1 !== s2)
408
+ return (c.fileIndex.get(s1) ?? 0) - (c.fileIndex.get(s2) ?? 0);
409
+ return n1.pos - n2.pos;
410
+ }
411
+ function sourceFileOf(node) {
412
+ return getSourceFileOfNode(node);
413
+ }
414
+ export function compareSymbols(c, s1, s2) {
415
+ if (s1 === s2)
416
+ return 0;
417
+ if (s1 === undefined)
418
+ return 1;
419
+ if (s2 === undefined)
420
+ return -1;
421
+ if (s1.declarations.length !== 0 && s2.declarations.length !== 0) {
422
+ const r = compareNodes(c, s1.declarations[0], s2.declarations[0]);
423
+ if (r !== 0)
424
+ return r;
425
+ }
426
+ else if (s1.declarations.length !== 0)
427
+ return -1;
428
+ else if (s2.declarations.length !== 0)
429
+ return 1;
430
+ const r = compareCodePoints(s1.name, s2.name);
431
+ if (r !== 0)
432
+ return r;
433
+ if (structuralTies)
434
+ return 0;
435
+ usedIds = true;
436
+ return compareIds(getSymbolId(s1), getSymbolId(s2));
437
+ }
438
+ /**
439
+ * `TSC_TS_ID_ORDER` (a test of output that must not depend on the order in which types and symbols
440
+ * are created): `reverse` reverses, and `scramble` permutes, the order of the ids that
441
+ * `compareTypes` and `compareSymbols` fall back to. Intrinsic types, created in a fixed order, keep
442
+ * theirs.
443
+ */
444
+ const idOrder = process.env.TSC_TS_ID_ORDER === 'reverse' ? 1 : process.env.TSC_TS_ID_ORDER === 'scramble' ? 2 : 0;
445
+ /** The order of two ids as a last resort: ascending, or as `TSC_TS_ID_ORDER` chooses. */
446
+ function compareIds(a, b) {
447
+ if (idOrder === 0)
448
+ return a - b;
449
+ if (idOrder === 1)
450
+ return b - a;
451
+ // A bijection on 32-bit ids (multiplication by an odd constant), so the order stays total.
452
+ return (Math.imul(a, 0x9e3779b1) >>> 0) - (Math.imul(b, 0x9e3779b1) >>> 0);
453
+ }
454
+ function getSortOrderFlags(t) {
455
+ if ((t.flags & (32768 /* TypeFlags.EnumLiteral */ | 65536 /* TypeFlags.Enum */)) !== 0 && (t.flags & 134217728 /* TypeFlags.Union */) === 0)
456
+ return 65536 /* TypeFlags.Enum */;
457
+ return t.flags >>> 0;
458
+ }
459
+ function getTypeNameSymbol(t) {
460
+ if (t.alias !== undefined)
461
+ return t.alias.symbol;
462
+ if ((t.flags & (524288 /* TypeFlags.TypeParameter */ | 8388608 /* TypeFlags.StringMapping */)) !== 0)
463
+ return t.symbol;
464
+ if ((t.objectFlags & (3 /* ObjectFlags.ClassOrInterface */ | 4 /* ObjectFlags.Reference */)) !== 0 &&
465
+ (t.flags & 1048576 /* TypeFlags.Object */) !== 0)
466
+ return t.symbol;
467
+ return undefined;
468
+ }
469
+ function compareTypeNames(c, t1, t2) {
470
+ const s1 = getTypeNameSymbol(t1);
471
+ const s2 = getTypeNameSymbol(t2);
472
+ if (s1 === s2)
473
+ return t1.alias !== undefined ? compareTypeLists(c, t1.alias.typeArguments, t2.alias.typeArguments) : 0;
474
+ if (s1 === undefined)
475
+ return 1;
476
+ if (s2 === undefined)
477
+ return -1;
478
+ return compareCodePoints(s1.name, s2.name);
479
+ }
480
+ function compareTypeLists(c, s1, s2) {
481
+ // Index the parameters directly: an alias such as `s1 ?? emptyTypes` would hold its own reference.
482
+ const n1 = s1 === undefined ? 0 : s1.length;
483
+ const n2 = s2 === undefined ? 0 : s2.length;
484
+ if (n1 !== n2)
485
+ return n1 - n2;
486
+ for (let i = 0; i < n1; i++) {
487
+ const r = compareTypes(c, s1[i], s2[i]);
488
+ if (r !== 0)
489
+ return r;
490
+ }
491
+ return 0;
492
+ }
493
+ function compareElementLabels(n1, n2) {
494
+ if (n1 === n2)
495
+ return 0;
496
+ if (n1 === undefined)
497
+ return -1;
498
+ if (n2 === undefined)
499
+ return 1;
500
+ return compareCodePoints(labelText(n1), labelText(n2));
501
+ }
502
+ function labelText(node) {
503
+ const name = getName(node);
504
+ return getText(name);
505
+ }
506
+ function compareTupleTypes(t1, t2) {
507
+ if (t1 === t2)
508
+ return 0;
509
+ if (t1.readonly !== t2.readonly)
510
+ return t1.readonly ? 1 : -1;
511
+ if (t1.elementInfos.length !== t2.elementInfos.length)
512
+ return t1.elementInfos.length - t2.elementInfos.length;
513
+ for (let i = 0; i < t1.elementInfos.length; i++) {
514
+ const r = t1.elementInfos[i].flags - t2.elementInfos[i].flags;
515
+ if (r !== 0)
516
+ return r;
517
+ }
518
+ for (let i = 0; i < t1.elementInfos.length; i++) {
519
+ const r = compareElementLabels(t1.elementInfos[i].labeledDeclaration, t2.elementInfos[i].labeledDeclaration);
520
+ if (r !== 0)
521
+ return r;
522
+ }
523
+ return 0;
524
+ }
525
+ export function compareTypeMappers(c, m1, m2) {
526
+ if (m1 === m2)
527
+ return 0;
528
+ if (m1 === undefined)
529
+ return 1;
530
+ if (m2 === undefined)
531
+ return -1;
532
+ const k1 = m1.kind();
533
+ const k2 = m2.kind();
534
+ if (k1 !== k2)
535
+ return k1 - k2;
536
+ switch (k1) {
537
+ case 1 /* TypeMapperKind.Simple */: {
538
+ const a = m1;
539
+ const b = m2;
540
+ const r = compareTypes(c, a.source, b.source);
541
+ return r !== 0 ? r : compareTypes(c, a.target, b.target);
542
+ }
543
+ case 2 /* TypeMapperKind.Array */: {
544
+ const a = m1;
545
+ const b = m2;
546
+ const r = compareTypeLists(c, a.sources, b.sources);
547
+ return r !== 0 ? r : compareTypeLists(c, a.targets, b.targets);
548
+ }
549
+ case 3 /* TypeMapperKind.Merged */: {
550
+ const a = m1;
551
+ const b = m2;
552
+ const r = compareTypeMappers(c, a.m1, b.m1);
553
+ return r !== 0 ? r : compareTypeMappers(c, a.m2, b.m2);
554
+ }
555
+ }
556
+ return structuralTies ? compareOtherTypeMappers(c, m1, m2) : 0;
557
+ }
558
+ /**
559
+ * With `structuralTies`: the order of two mappers of the kinds `compareTypeMappers` doesn't compare,
560
+ * by their parts: composite mappers by their two mappers, mappers of several sources to one target by
561
+ * the sources and the target.
562
+ */
563
+ function compareOtherTypeMappers(c, m1, m2) {
564
+ const r = otherMapperRank(m1) - otherMapperRank(m2);
565
+ if (r !== 0)
566
+ return r;
567
+ if (m1 instanceof CompositeTypeMapper && m2 instanceof CompositeTypeMapper) {
568
+ const r1 = compareTypeMappers(c, m1.m1, m2.m1);
569
+ return r1 !== 0 ? r1 : compareTypeMappers(c, m1.m2, m2.m2);
570
+ }
571
+ if (m1 instanceof ArrayToSingleTypeMapper && m2 instanceof ArrayToSingleTypeMapper) {
572
+ const r1 = compareTypeLists(c, m1.sources, m2.sources);
573
+ return r1 !== 0 ? r1 : compareTypes(c, m1.target, m2.target);
574
+ }
575
+ return 0;
576
+ }
577
+ function otherMapperRank(m) {
578
+ return m instanceof CompositeTypeMapper ? 1 : m instanceof ArrayToSingleTypeMapper ? 2 : 3;
579
+ }
580
+ /**
581
+ * The places of the declarations that `t`'s identity reaches, breadth first: its symbol and alias, then
582
+ * those of its type arguments, constituents and mapper targets, as numbers ordered by file and
583
+ * position; those in the program's own files first, then those in declaration files. Types that
584
+ * differ only in a part the program wrote, such as the fields of two instantiations of a library
585
+ * generic, first differ where that part was written, which is the order in which checking the source
586
+ * creates them.
587
+ */
588
+ function declarationPlaces(c, t) {
589
+ const places = [];
590
+ const libraryPlaces = [];
591
+ const queue = [t];
592
+ const seen = new Set();
593
+ const visit = (symbol) => {
594
+ if (symbol === undefined || symbol.declarations.length === 0)
595
+ return;
596
+ const node = symbol.declarations[0];
597
+ const file = sourceFileOf(node);
598
+ (file.isDeclarationFile ? libraryPlaces : places).push((c.fileIndex.get(file) ?? 0) * 0x4000000 + node.pos);
599
+ };
600
+ const mapper = (m) => {
601
+ if (m === undefined)
602
+ return;
603
+ if (m instanceof SimpleTypeMapper)
604
+ queue.push(m.target);
605
+ else if (m instanceof ArrayTypeMapper)
606
+ for (const u of m.targets)
607
+ queue.push(u);
608
+ else if (m instanceof ArrayToSingleTypeMapper)
609
+ queue.push(m.target);
610
+ else if (m instanceof MergedTypeMapper || m instanceof CompositeTypeMapper) {
611
+ mapper(m.m1);
612
+ mapper(m.m2);
613
+ }
614
+ };
615
+ for (let k = 0; k < queue.length && places.length < declarationPlaceLimit && k < 64; k++) {
616
+ const u = queue[k];
617
+ if (seen.has(u))
618
+ continue;
619
+ seen.add(u);
620
+ visit(u.symbol);
621
+ if (u.alias !== undefined) {
622
+ visit(u.alias.symbol);
623
+ for (const v of u.alias.typeArguments)
624
+ queue.push(v);
625
+ }
626
+ if ((u.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0)
627
+ for (const v of u.types)
628
+ queue.push(v);
629
+ if ((u.flags & 1048576 /* TypeFlags.Object */) !== 0) {
630
+ if ((u.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 && u.node === undefined) {
631
+ const args = u.resolvedTypeArguments;
632
+ if (args !== undefined)
633
+ for (const v of args)
634
+ queue.push(v);
635
+ }
636
+ mapper(u.mapper);
637
+ }
638
+ }
639
+ for (const place of libraryPlaces)
640
+ places.push(place);
641
+ return places;
642
+ }
643
+ const declarationPlaceLimit = 16;
644
+ function comparePlaces(a, b) {
645
+ const n = Math.min(a.length, b.length);
646
+ for (let i = 0; i < n; i++)
647
+ if (a[i] !== b[i])
648
+ return a[i] - b[i];
649
+ return a.length - b.length;
650
+ }
651
+ /**
652
+ * While set (`inDeclarationOrder`), `compareTypes`, `compareSymbols` and `compareTypeMappers` don't fall
653
+ * back to ids, which follow the order in which the checker created the types, and compare all mapper
654
+ * kinds by their parts.
655
+ */
656
+ let structuralTies = false;
657
+ /** Set when a comparison falls back to an id (`inDeclarationOrder`). */
658
+ let usedIds = false;
659
+ /**
660
+ * The constituents of a union in the order declaration emit writes them: as in the union, except that
661
+ * neighbors the union orders by ids, at any depth of `compareTypes`, are ordered by their structure
662
+ * where that tells them apart. Ids follow the order in which the checker created the types, which
663
+ * with several checkers depends on the files a checker checked first, so declaration files would
664
+ * otherwise differ from run to run.
665
+ */
666
+ export function inDeclarationOrder(c, types) {
667
+ let result;
668
+ let start = 0;
669
+ for (let i = 1; i <= types.length; i++) {
670
+ let tied = false;
671
+ if (i < types.length) {
672
+ usedIds = false;
673
+ compareTypes(c, types[i - 1], types[i]);
674
+ tied = usedIds;
675
+ }
676
+ if (tied)
677
+ continue;
678
+ // types[start..i) are neighbors ordered by ids.
679
+ if (i - start > 1) {
680
+ const run = types.slice(start, i);
681
+ const order = [];
682
+ for (let k = 0; k < run.length; k++)
683
+ order.push(k);
684
+ const places = [];
685
+ for (const t of run)
686
+ places.push(declarationPlaces(c, t));
687
+ structuralTies = true;
688
+ order.sort((a, b) => comparePlaces(places[a], places[b]) || compareTypes(c, run[a], run[b]) || a - b);
689
+ structuralTies = false;
690
+ for (let k = 0; k < order.length; k++)
691
+ if (order[k] !== k) {
692
+ if (result === undefined)
693
+ result = types.slice();
694
+ result[start + k] = run[order[k]];
695
+ }
696
+ }
697
+ start = i;
698
+ }
699
+ return result ?? types;
700
+ }
701
+ function compareNumbers(a, b) {
702
+ // Go's cmp.Compare: NaN sorts first.
703
+ const an = a !== a;
704
+ const bn = b !== b;
705
+ if (an || bn)
706
+ return an && bn ? 0 : an ? -1 : 1;
707
+ return a < b ? -1 : a > b ? 1 : 0;
708
+ }
709
+ /** tsgo's deterministic type order, which orders union constituents. */
710
+ export function compareTypes(c, t1, t2) {
711
+ if (t1 === t2)
712
+ return 0;
713
+ let r = getSortOrderFlags(t1) - getSortOrderFlags(t2);
714
+ if (r !== 0)
715
+ return r;
716
+ r = compareTypeNames(c, t1, t2);
717
+ if (r !== 0)
718
+ return r;
719
+ const flags = t1.flags;
720
+ if ((flags & 394239 /* TypeFlags.Singleton */) !== 0) {
721
+ // Only distinguished by ids.
722
+ }
723
+ else if ((flags & 1048576 /* TypeFlags.Object */) !== 0) {
724
+ r = compareSymbols(c, t1.symbol, t2.symbol);
725
+ if (r !== 0)
726
+ return r;
727
+ if ((t1.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 && (t2.objectFlags & 4 /* ObjectFlags.Reference */) !== 0) {
728
+ const r1 = t1;
729
+ const r2 = t2;
730
+ const target1 = r1.target;
731
+ const target2 = r2.target;
732
+ if ((target1.objectFlags & 8 /* ObjectFlags.Tuple */) !== 0 && (target2.objectFlags & 8 /* ObjectFlags.Tuple */) !== 0) {
733
+ r = compareTupleTypes(target1, target2);
734
+ if (r !== 0)
735
+ return r;
736
+ }
737
+ if (r1.node === undefined && r2.node === undefined) {
738
+ r = compareTypeLists(c, r1.resolvedTypeArguments, r2.resolvedTypeArguments);
739
+ if (r !== 0)
740
+ return r;
741
+ }
742
+ else {
743
+ r = compareNodes(c, r1.node, r2.node);
744
+ if (r !== 0)
745
+ return r;
746
+ r = compareTypeMappers(c, r1.mapper, r2.mapper);
747
+ if (r !== 0)
748
+ return r;
749
+ }
750
+ }
751
+ else if ((t1.objectFlags & 4 /* ObjectFlags.Reference */) !== 0)
752
+ return -1;
753
+ else if ((t2.objectFlags & 4 /* ObjectFlags.Reference */) !== 0)
754
+ return 1;
755
+ else {
756
+ r = (t1.objectFlags & 50332991 /* ObjectFlags.ObjectTypeKindMask */) - (t2.objectFlags & 50332991 /* ObjectFlags.ObjectTypeKindMask */);
757
+ if (r !== 0)
758
+ return r;
759
+ r = compareTypeMappers(c, t1.mapper, t2.mapper);
760
+ if (r !== 0)
761
+ return r;
762
+ }
763
+ }
764
+ else if ((flags & 134217728 /* TypeFlags.Union */) !== 0) {
765
+ const o1 = t1.origin;
766
+ const o2 = t2.origin;
767
+ if (o1 === undefined && o2 === undefined) {
768
+ r = compareTypeLists(c, t1.types, t2.types);
769
+ if (r !== 0)
770
+ return r;
771
+ }
772
+ else if (o1 === undefined)
773
+ return 1;
774
+ else if (o2 === undefined)
775
+ return -1;
776
+ else {
777
+ r = compareTypes(c, o1, o2);
778
+ if (r !== 0)
779
+ return r;
780
+ }
781
+ }
782
+ else if ((flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
783
+ r = compareTypeLists(c, t1.types, t2.types);
784
+ if (r !== 0)
785
+ return r;
786
+ }
787
+ else if ((flags & (65536 /* TypeFlags.Enum */ | 32768 /* TypeFlags.EnumLiteral */ | 16384 /* TypeFlags.UniqueESSymbol */)) !== 0) {
788
+ r = compareSymbols(c, t1.symbol, t2.symbol);
789
+ if (r !== 0)
790
+ return r;
791
+ }
792
+ else if ((flags & 1024 /* TypeFlags.StringLiteral */) !== 0) {
793
+ r = compareCodePoints(t1.stringValue, t2.stringValue);
794
+ if (r !== 0)
795
+ return r;
796
+ }
797
+ else if ((flags & 2048 /* TypeFlags.NumberLiteral */) !== 0) {
798
+ r = compareNumbers(t1.numberValue, t2.numberValue);
799
+ if (r !== 0)
800
+ return r;
801
+ }
802
+ else if ((flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0) {
803
+ const b1 = t1.stringValue === 'true';
804
+ const b2 = t2.stringValue === 'true';
805
+ if (b1 !== b2)
806
+ return b1 ? 1 : -1;
807
+ }
808
+ else if ((flags & 524288 /* TypeFlags.TypeParameter */) !== 0) {
809
+ r = compareSymbols(c, t1.symbol, t2.symbol);
810
+ if (r !== 0)
811
+ return r;
812
+ }
813
+ else if ((flags & 2097152 /* TypeFlags.Index */) !== 0) {
814
+ r = compareTypes(c, t1.target, t2.target);
815
+ if (r !== 0)
816
+ return r;
817
+ r = t1.indexFlags - t2.indexFlags;
818
+ if (r !== 0)
819
+ return r;
820
+ }
821
+ else if ((flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0) {
822
+ r = compareTypes(c, t1.objectType, t2.objectType);
823
+ if (r !== 0)
824
+ return r;
825
+ r = compareTypes(c, t1.indexType, t2.indexType);
826
+ if (r !== 0)
827
+ return r;
828
+ }
829
+ else if ((flags & 67108864 /* TypeFlags.Conditional */) !== 0) {
830
+ r = compareNodes(c, t1.root.node, t2.root.node);
831
+ if (r !== 0)
832
+ return r;
833
+ r = compareTypeMappers(c, t1.mapper, t2.mapper);
834
+ if (r !== 0)
835
+ return r;
836
+ }
837
+ else if ((flags & 16777216 /* TypeFlags.Substitution */) !== 0) {
838
+ r = compareTypes(c, t1.baseType, t2.baseType);
839
+ if (r !== 0)
840
+ return r;
841
+ r = compareTypes(c, t1.constraint, t2.constraint);
842
+ if (r !== 0)
843
+ return r;
844
+ }
845
+ else if ((flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
846
+ const a = t1.texts;
847
+ const b = t2.texts;
848
+ const n = Math.min(a.length, b.length);
849
+ for (let i = 0; i < n; i++) {
850
+ r = compareCodePoints(a[i], b[i]);
851
+ if (r !== 0)
852
+ return r;
853
+ }
854
+ if (a.length !== b.length)
855
+ return a.length - b.length;
856
+ r = compareTypeLists(c, t1.types, t2.types);
857
+ if (r !== 0)
858
+ return r;
859
+ }
860
+ else if ((flags & 8388608 /* TypeFlags.StringMapping */) !== 0) {
861
+ r = compareTypes(c, t1.target, t2.target);
862
+ if (r !== 0)
863
+ return r;
864
+ }
865
+ if ((flags & 394239 /* TypeFlags.Singleton */) !== 0)
866
+ return t1.id - t2.id;
867
+ if (structuralTies)
868
+ return 0;
869
+ usedIds = true;
870
+ return compareIds(t1.id, t2.id);
871
+ }
872
+ /** Binary search position of `t` in the sorted `types`: index if found, else `-(insertion point) - 1`. */
873
+ export function binarySearchTypes(c, types, t) {
874
+ let low = 0;
875
+ let high = types.length - 1;
876
+ while (low <= high) {
877
+ const middle = (low + high) >> 1;
878
+ const r = compareTypes(c, types[middle], t);
879
+ if (r === 0)
880
+ return middle;
881
+ if (r < 0)
882
+ low = middle + 1;
883
+ else
884
+ high = middle - 1;
885
+ }
886
+ return ~low;
887
+ }
888
+ /**
889
+ * Sorted type lists shorter than this are searched by identity. `compareTypes` is 0 only for
890
+ * identical types (the last criterion is the unique id), so membership needs no ordering.
891
+ */
892
+ const linearTypeSearchLimit = 16;
893
+ /** The index of `t` in `types` at or after `from`, or -1. */
894
+ function indexOfType(types, t, from) {
895
+ for (let i = from; i < types.length; i++)
896
+ if (types[i] === t)
897
+ return i;
898
+ return -1;
899
+ }
900
+ export function containsType(c, types, t) {
901
+ if (types.length < linearTypeSearchLimit)
902
+ return indexOfType(types, t, 0) >= 0;
903
+ return binarySearchTypes(c, types, t) >= 0;
904
+ }
905
+ /**
906
+ * Whether `t` is a constituent of `union`. Equal types are identical, so a large union tested more
907
+ * than once looks `t` up in a set instead of searching its sorted constituents. Most large unions
908
+ * are tested once; a few are tested very often.
909
+ */
910
+ export function unionContainsType(c, union, t) {
911
+ const types = union.types;
912
+ if (types.length < linearTypeSearchLimit)
913
+ return indexOfType(types, t, 0) >= 0;
914
+ const side = sideOf(c, union);
915
+ let set = side.constituentSet;
916
+ if (set === undefined) {
917
+ if (side.membershipTests++ === 0)
918
+ return binarySearchTypes(c, types, t) >= 0;
919
+ set = new Set(types);
920
+ side.constituentSet = set;
921
+ }
922
+ return set.has(t);
923
+ }
924
+ /**
925
+ * Whether `union` contains every type of the sorted `types` (the constituents of another union).
926
+ * Both lists follow `compareTypes`, so in a small union each type is found by identity after the
927
+ * previous one; large unions keep `unionContainsType`.
928
+ */
929
+ export function unionContainsSortedTypes(c, union, types) {
930
+ const targets = union.types;
931
+ if (targets.length >= linearTypeSearchLimit) {
932
+ for (const t of types)
933
+ if (!unionContainsType(c, union, t))
934
+ return false;
935
+ return true;
936
+ }
937
+ let next = 0;
938
+ for (const t of types) {
939
+ const index = indexOfType(targets, t, next);
940
+ if (index < 0)
941
+ return false;
942
+ next = index + 1;
943
+ }
944
+ return true;
945
+ }
946
+ /** Inserts `t` into the sorted `types`; returns false if already present. */
947
+ export function insertType(c, types, t) {
948
+ const index = binarySearchTypes(c, types, t);
949
+ if (index >= 0)
950
+ return false;
951
+ types.splice(~index, 0, t);
952
+ return true;
953
+ }
954
+ // ---- Iterating unions
955
+ export function someType(type, f) {
956
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0) {
957
+ for (const t of type.types)
958
+ if (f(t))
959
+ return true;
960
+ return false;
961
+ }
962
+ return f(type);
963
+ }
964
+ export function everyType(type, f) {
965
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0) {
966
+ for (const t of type.types)
967
+ if (!f(t))
968
+ return false;
969
+ return true;
970
+ }
971
+ return f(type);
972
+ }
973
+ export function everyContainedType(type, f) {
974
+ if ((type.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
975
+ for (const t of type.types)
976
+ if (!f(t))
977
+ return false;
978
+ return true;
979
+ }
980
+ return f(type);
981
+ }
982
+ export function typesOf(type) {
983
+ return type.types;
984
+ }
985
+ export function mapType(c, type, f) {
986
+ return mapTypeEx(c, type, f, false) ?? c.neverType;
987
+ }
988
+ /** Maps union constituents; undefined when every constituent maps to undefined. */
989
+ export function mapTypeEx(c, type, f, noReductions) {
990
+ if ((type.flags & 262144 /* TypeFlags.Never */) !== 0)
991
+ return type;
992
+ if ((type.flags & 134217728 /* TypeFlags.Union */) === 0)
993
+ return f(type);
994
+ const union = type;
995
+ const origin = union.origin;
996
+ const types = origin !== undefined && (origin.flags & 134217728 /* TypeFlags.Union */) !== 0 ? origin.types : union.types;
997
+ const mapped = [];
998
+ let changed = false;
999
+ for (const s of types) {
1000
+ const m = (s.flags & 134217728 /* TypeFlags.Union */) !== 0 ? mapTypeEx(c, s, f, noReductions) : f(s);
1001
+ if (m !== s)
1002
+ changed = true;
1003
+ if (m !== undefined)
1004
+ mapped.push(m);
1005
+ }
1006
+ if (!changed)
1007
+ return type;
1008
+ if (mapped.length === 0)
1009
+ return undefined;
1010
+ return getUnionTypeEx(c, mapped, noReductions ? 0 /* UnionReduction.None */ : 1 /* UnionReduction.Literal */, undefined, undefined);
1011
+ }
1012
+ export function mapTypeWithAlias(c, type, f, alias) {
1013
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0 && alias !== undefined) {
1014
+ const mapped = [];
1015
+ for (const t of type.types)
1016
+ mapped.push(f(t));
1017
+ return getUnionTypeEx(c, mapped, 1 /* UnionReduction.Literal */, alias, undefined);
1018
+ }
1019
+ return mapType(c, type, f);
1020
+ }
1021
+ export function filterType(c, type, f) {
1022
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1023
+ const union = type;
1024
+ const types = union.types;
1025
+ // The list is copied from the first constituent removed; most filters keep every one.
1026
+ let removed = 0;
1027
+ while (removed < types.length && f(types[removed]))
1028
+ removed++;
1029
+ if (removed === types.length)
1030
+ return type;
1031
+ const filtered = [];
1032
+ for (let i = 0; i < removed; i++)
1033
+ filtered.push(types[i]);
1034
+ for (let i = removed + 1; i < types.length; i++) {
1035
+ const t = types[i];
1036
+ if (f(t))
1037
+ filtered.push(t);
1038
+ }
1039
+ const origin = union.origin;
1040
+ let newOrigin;
1041
+ if (origin !== undefined && (origin.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1042
+ const originTypes = origin.types;
1043
+ const originFiltered = [];
1044
+ for (const u of originTypes)
1045
+ if ((u.flags & 134217728 /* TypeFlags.Union */) !== 0 || f(u))
1046
+ originFiltered.push(u);
1047
+ if (originTypes.length - originFiltered.length === types.length - filtered.length) {
1048
+ if (originFiltered.length === 1)
1049
+ return originFiltered[0];
1050
+ newOrigin = newUnionType(c, 0 /* ObjectFlags.None */, originFiltered);
1051
+ }
1052
+ }
1053
+ return getUnionTypeFromSortedList(c, filtered, union.objectFlags & (32768 /* ObjectFlags.PrimitiveUnion */ | 33554432 /* ObjectFlags.ContainsIntersections */), undefined, newOrigin);
1054
+ }
1055
+ return (type.flags & 262144 /* TypeFlags.Never */) !== 0 || f(type) ? type : c.neverType;
1056
+ }
1057
+ export function removeType(c, type, target) {
1058
+ return filterType(c, type, (t) => t !== target);
1059
+ }
1060
+ export function countTypes(type) {
1061
+ if ((type.flags & 134217728 /* TypeFlags.Union */) !== 0)
1062
+ return type.types.length;
1063
+ return (type.flags & 262144 /* TypeFlags.Never */) !== 0 ? 0 : 1;
1064
+ }
1065
+ // ---- Unions
1066
+ export function isErrorType(c, type) {
1067
+ // Only `any` types with alias symbols are those from getTypeFromTypeAliasReference for failed lookups.
1068
+ return type === c.errorType || ((type.flags & 1 /* TypeFlags.Any */) !== 0 && type.alias !== undefined);
1069
+ }
1070
+ export function getUnionType(c, types) {
1071
+ return getUnionTypeEx(c, types, 1 /* UnionReduction.Literal */, undefined, undefined);
1072
+ }
1073
+ export function getUnionTypeEx(c, types, unionReduction, alias, origin) {
1074
+ if (types.length === 0)
1075
+ return c.neverType;
1076
+ if (types.length === 1)
1077
+ return types[0];
1078
+ // Optimize the common case of unioning a union with some other type, like `undefined`.
1079
+ if (types.length === 2 &&
1080
+ origin === undefined &&
1081
+ ((types[0].flags & 134217728 /* TypeFlags.Union */) !== 0 || (types[1].flags & 134217728 /* TypeFlags.Union */) !== 0)) {
1082
+ let id1 = types[0].id;
1083
+ let id2 = types[1].id;
1084
+ if (id1 > id2) {
1085
+ const t = id1;
1086
+ id1 = id2;
1087
+ id2 = t;
1088
+ }
1089
+ // Without an alias, both ids and the reduction pack into one exact number.
1090
+ if (alias === undefined && id2 < UnionPairIdLimit) {
1091
+ const id = (id1 * UnionPairIdLimit + id2) * 4 + unionReduction;
1092
+ let result = c.unionOfUnionTypesById.get(id);
1093
+ if (result === undefined) {
1094
+ result = getUnionTypeWorker(c, types, unionReduction, alias, undefined);
1095
+ c.unionOfUnionTypesById.set(id, result);
1096
+ }
1097
+ return result;
1098
+ }
1099
+ const key = id1 + ',' + id2 + ',' + unionReduction + aliasKey(alias);
1100
+ let result = c.unionOfUnionTypes.get(key);
1101
+ if (result === undefined) {
1102
+ result = getUnionTypeWorker(c, types, unionReduction, alias, undefined);
1103
+ c.unionOfUnionTypes.set(key, result);
1104
+ }
1105
+ return result;
1106
+ }
1107
+ return getUnionTypeWorker(c, types, unionReduction, alias, origin);
1108
+ }
1109
+ class TypeSet {
1110
+ types = [];
1111
+ includes = 0;
1112
+ /** Index of the type added or found last, or -1. */
1113
+ last = -1;
1114
+ }
1115
+ /**
1116
+ * Inserts `t` into the sorted `set.types` unless present, starting next to the previous type.
1117
+ * Constituents arrive in sorted runs (nested unions, union members mapped in order), so most
1118
+ * inserts land at or just after the previous position; otherwise the search covers only the side
1119
+ * of the previous position that holds `t`. The set equals binary-search insertion's: the types
1120
+ * stay sorted under the total order `compareTypes`.
1121
+ */
1122
+ function insertTypeNearLast(c, set, t) {
1123
+ const types = set.types;
1124
+ let low = 0;
1125
+ let high = types.length - 1;
1126
+ const last = set.last;
1127
+ if (last >= 0) {
1128
+ const r = compareTypes(c, types[last], t);
1129
+ if (r === 0)
1130
+ return;
1131
+ if (r < 0) {
1132
+ low = last + 1;
1133
+ if (low <= high) {
1134
+ const next = compareTypes(c, types[low], t);
1135
+ if (next === 0) {
1136
+ set.last = low;
1137
+ return;
1138
+ }
1139
+ if (next > 0)
1140
+ high = low - 1;
1141
+ else
1142
+ low++;
1143
+ }
1144
+ }
1145
+ else
1146
+ high = last - 1;
1147
+ }
1148
+ while (low <= high) {
1149
+ const middle = (low + high) >> 1;
1150
+ const r = compareTypes(c, types[middle], t);
1151
+ if (r === 0) {
1152
+ set.last = middle;
1153
+ return;
1154
+ }
1155
+ if (r < 0)
1156
+ low = middle + 1;
1157
+ else
1158
+ high = middle - 1;
1159
+ }
1160
+ types.splice(low, 0, t);
1161
+ set.last = low;
1162
+ }
1163
+ function getUnionTypeWorker(c, types, unionReduction, alias, origin) {
1164
+ const set = new TypeSet();
1165
+ addTypesToUnion(c, set, types);
1166
+ let typeSet = set.types;
1167
+ const includes = set.includes;
1168
+ if (unionReduction !== 0 /* UnionReduction.None */) {
1169
+ if ((includes & 3 /* TypeFlags.AnyOrUnknown */) !== 0) {
1170
+ if ((includes & 1 /* TypeFlags.Any */) !== 0) {
1171
+ if ((includes & 33554432 /* TypeFlags.IncludesWildcard */) !== 0)
1172
+ return c.wildcardType;
1173
+ if ((includes & 1073741824 /* TypeFlags.IncludesError */) !== 0)
1174
+ return c.errorType;
1175
+ return c.anyType;
1176
+ }
1177
+ return c.unknownType;
1178
+ }
1179
+ if ((includes & 4 /* TypeFlags.Undefined */) !== 0) {
1180
+ if (typeSet.length >= 2 && typeSet[0] === c.undefinedType && typeSet[1] === c.missingType)
1181
+ typeSet.splice(1, 1);
1182
+ }
1183
+ if ((includes &
1184
+ (65536 /* TypeFlags.Enum */ |
1185
+ 15360 /* TypeFlags.Literal */ |
1186
+ 16384 /* TypeFlags.UniqueESSymbol */ |
1187
+ 4194304 /* TypeFlags.TemplateLiteral */ |
1188
+ 8388608 /* TypeFlags.StringMapping */)) !==
1189
+ 0 ||
1190
+ ((includes & 16 /* TypeFlags.Void */) !== 0 && (includes & 4 /* TypeFlags.Undefined */) !== 0))
1191
+ removeRedundantLiteralTypes(c, typeSet, includes, (unionReduction & 2 /* UnionReduction.Subtype */) !== 0);
1192
+ if ((includes & 1024 /* TypeFlags.StringLiteral */) !== 0 &&
1193
+ (includes & (4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0)
1194
+ removeStringLiteralsMatchedByTemplateLiterals(c, typeSet);
1195
+ if ((includes & 536870912 /* TypeFlags.IncludesConstrainedTypeVariable */) !== 0)
1196
+ removeConstrainedTypeVariables(c, typeSet);
1197
+ if (unionReduction === 2 /* UnionReduction.Subtype */) {
1198
+ const reduced = removeSubtypes(c, typeSet, (includes & 1048576 /* TypeFlags.Object */) !== 0);
1199
+ if (reduced === undefined)
1200
+ return c.errorType;
1201
+ typeSet = reduced;
1202
+ }
1203
+ if (typeSet.length === 0) {
1204
+ if ((includes & 8 /* TypeFlags.Null */) !== 0)
1205
+ return (includes & 2097152 /* TypeFlags.IncludesNonWideningType */) !== 0 ? c.nullType : c.nullWideningType;
1206
+ if ((includes & 4 /* TypeFlags.Undefined */) !== 0)
1207
+ return (includes & 2097152 /* TypeFlags.IncludesNonWideningType */) !== 0 ? c.undefinedType : c.undefinedWideningType;
1208
+ return c.neverType;
1209
+ }
1210
+ }
1211
+ if (origin === undefined && (includes & 134217728 /* TypeFlags.Union */) !== 0) {
1212
+ const namedUnions = [];
1213
+ addNamedUnions(namedUnions, types);
1214
+ const reducedTypes = [];
1215
+ for (const t of typeSet) {
1216
+ let inNamed = false;
1217
+ for (const u of namedUnions)
1218
+ if (unionContainsType(c, u, t)) {
1219
+ inNamed = true;
1220
+ break;
1221
+ }
1222
+ if (!inNamed)
1223
+ reducedTypes.push(t);
1224
+ }
1225
+ if (alias === undefined && namedUnions.length === 1 && reducedTypes.length === 0)
1226
+ return namedUnions[0];
1227
+ // A denormalized origin only when the union came from named unions with no overlap.
1228
+ let namedTypesCount = 0;
1229
+ for (const u of namedUnions)
1230
+ namedTypesCount += u.types.length;
1231
+ if (namedTypesCount + reducedTypes.length === typeSet.length) {
1232
+ for (const t of namedUnions)
1233
+ insertType(c, reducedTypes, t);
1234
+ origin = newUnionType(c, 0 /* ObjectFlags.None */, reducedTypes);
1235
+ }
1236
+ }
1237
+ const objectFlags = ((includes & 286523411 /* TypeFlags.NotPrimitiveUnion */) !== 0 ? 0 : 32768 /* ObjectFlags.PrimitiveUnion */) |
1238
+ ((includes & 268435456 /* TypeFlags.Intersection */) !== 0 ? 33554432 /* ObjectFlags.ContainsIntersections */ : 0);
1239
+ return getUnionTypeFromSortedList(c, typeSet, objectFlags, alias, origin);
1240
+ }
1241
+ /** Assumes `types` is sorted and deduplicated. */
1242
+ export function getUnionTypeFromSortedList(c, types, precomputedObjectFlags, alias, origin) {
1243
+ if (types.length === 0)
1244
+ return c.neverType;
1245
+ if (types.length === 1)
1246
+ return types[0];
1247
+ // Unions are found by a hash of their key; the spelled key is only needed when two keys share a
1248
+ // hash, for the second one.
1249
+ const hash = unionKeyHash(types, origin, alias);
1250
+ let result = c.unionTypesByHash.get(hash);
1251
+ let key;
1252
+ if (result !== undefined && !isUnionOfKey(result, types, origin, alias)) {
1253
+ key = unionKey(types, origin, alias);
1254
+ result = c.unionTypes.get(key);
1255
+ }
1256
+ if (result === undefined) {
1257
+ const union = newUnionType(c, precomputedObjectFlags | getPropagatingFlagsOfTypes(types, 12 /* TypeFlags.Nullable */), types);
1258
+ union.origin = origin;
1259
+ union.alias = alias;
1260
+ if (types.length === 2 &&
1261
+ (types[0].flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0 &&
1262
+ (types[1].flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0)
1263
+ union.flags |= 256 /* TypeFlags.Boolean */;
1264
+ if (key === undefined)
1265
+ c.unionTypesByHash.set(hash, union);
1266
+ else
1267
+ c.unionTypes.set(key, union);
1268
+ result = union;
1269
+ }
1270
+ return result;
1271
+ }
1272
+ function addTypesToUnion(c, set, types) {
1273
+ let lastType;
1274
+ for (const t of types) {
1275
+ if (t === lastType)
1276
+ continue;
1277
+ if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1278
+ const u = t;
1279
+ if (t.alias !== undefined || u.origin !== undefined)
1280
+ set.includes |= 134217728 /* TypeFlags.Union */;
1281
+ addTypesToUnion(c, set, u.types);
1282
+ }
1283
+ else
1284
+ addTypeToUnion(c, set, t);
1285
+ lastType = t;
1286
+ }
1287
+ }
1288
+ function addTypeToUnion(c, set, t) {
1289
+ const flags = t.flags;
1290
+ if ((flags & 262144 /* TypeFlags.Never */) !== 0)
1291
+ return;
1292
+ let includes = set.includes | (flags & 416808959 /* TypeFlags.IncludesMask */);
1293
+ if ((flags & 132644864 /* TypeFlags.Instantiable */) !== 0)
1294
+ includes |= 16777216 /* TypeFlags.IncludesInstantiable */;
1295
+ if ((flags & 268435456 /* TypeFlags.Intersection */) !== 0 && (t.objectFlags & 134217728 /* ObjectFlags.IsConstrainedTypeVariable */) !== 0)
1296
+ includes |= 536870912 /* TypeFlags.IncludesConstrainedTypeVariable */;
1297
+ if (t === c.wildcardType)
1298
+ includes |= 33554432 /* TypeFlags.IncludesWildcard */;
1299
+ if (isErrorType(c, t))
1300
+ includes |= 1073741824 /* TypeFlags.IncludesError */;
1301
+ if (!c.strictNullChecks && (flags & 12 /* TypeFlags.Nullable */) !== 0) {
1302
+ if ((t.objectFlags & 65536 /* ObjectFlags.ContainsWideningType */) === 0)
1303
+ includes |= 2097152 /* TypeFlags.IncludesNonWideningType */;
1304
+ }
1305
+ else
1306
+ insertTypeNearLast(c, set, t);
1307
+ set.includes = includes;
1308
+ }
1309
+ function addNamedUnions(namedUnions, types) {
1310
+ for (const t of types) {
1311
+ if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
1312
+ continue;
1313
+ const u = t;
1314
+ if (t.alias !== undefined || (u.origin !== undefined && (u.origin.flags & 134217728 /* TypeFlags.Union */) === 0)) {
1315
+ if (!namedUnions.includes(t))
1316
+ namedUnions.push(t);
1317
+ }
1318
+ else if (u.origin !== undefined && (u.origin.flags & 134217728 /* TypeFlags.Union */) !== 0)
1319
+ addNamedUnions(namedUnions, u.origin.types);
1320
+ }
1321
+ }
1322
+ function removeRedundantLiteralTypes(c, types, includes, reduceVoidUndefined) {
1323
+ let i = types.length;
1324
+ while (i > 0) {
1325
+ i--;
1326
+ const t = types[i];
1327
+ const flags = t.flags;
1328
+ const remove = ((flags & (1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0 &&
1329
+ (includes & 32 /* TypeFlags.String */) !== 0) ||
1330
+ ((flags & 2048 /* TypeFlags.NumberLiteral */) !== 0 && (includes & 64 /* TypeFlags.Number */) !== 0) ||
1331
+ ((flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0 && (includes & 128 /* TypeFlags.BigInt */) !== 0) ||
1332
+ ((flags & 16384 /* TypeFlags.UniqueESSymbol */) !== 0 && (includes & 512 /* TypeFlags.ESSymbol */) !== 0) ||
1333
+ (reduceVoidUndefined && (flags & 4 /* TypeFlags.Undefined */) !== 0 && (includes & 16 /* TypeFlags.Void */) !== 0) ||
1334
+ (isFreshLiteralType(t) && containsType(c, types, t.regularType));
1335
+ if (remove)
1336
+ types.splice(i, 1);
1337
+ }
1338
+ }
1339
+ function removeStringLiteralsMatchedByTemplateLiterals(c, types) {
1340
+ const templates = [];
1341
+ for (const t of types)
1342
+ if (isPatternLiteralType(c, t))
1343
+ templates.push(t);
1344
+ if (templates.length === 0)
1345
+ return;
1346
+ // Every string literal is tested against every template, and most fail the first test of
1347
+ // inferTypesFromTemplateLiteralType: the literal starts with the template's first text, ends with
1348
+ // its last, and is long enough for both. Apply that test here with the texts read once per
1349
+ // template; only literals that pass it take the full match. (String mapping templates keep the
1350
+ // full test: their start and end are empty.)
1351
+ const starts = [];
1352
+ const ends = [];
1353
+ const minLengths = [];
1354
+ for (const template of templates) {
1355
+ if ((template.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
1356
+ const texts = template.texts;
1357
+ const start = texts[0];
1358
+ const end = texts[texts.length - 1];
1359
+ starts.push(start);
1360
+ ends.push(end);
1361
+ minLengths.push(start.length + end.length);
1362
+ }
1363
+ else {
1364
+ starts.push('');
1365
+ ends.push('');
1366
+ minLengths.push(0);
1367
+ }
1368
+ }
1369
+ let i = types.length;
1370
+ while (i > 0) {
1371
+ i--;
1372
+ const t = types[i];
1373
+ if ((t.flags & 1024 /* TypeFlags.StringLiteral */) === 0)
1374
+ continue;
1375
+ const text = t.stringValue;
1376
+ const length = text.length;
1377
+ for (let j = 0; j < templates.length; j++) {
1378
+ if (length < minLengths[j])
1379
+ continue;
1380
+ const start = starts[j];
1381
+ if (start.length !== 0 && !text.startsWith(start))
1382
+ continue;
1383
+ const end = ends[j];
1384
+ if (end.length !== 0 && !text.endsWith(end))
1385
+ continue;
1386
+ if (isTypeMatchedByTemplateLiteralOrStringMapping(c, t, templates[j])) {
1387
+ types.splice(i, 1);
1388
+ break;
1389
+ }
1390
+ }
1391
+ }
1392
+ }
1393
+ function isTypeMatchedByTemplateLiteralOrStringMapping(c, t, template) {
1394
+ if ((template.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0)
1395
+ return isTypeMatchedByTemplateLiteralType(c, t, template);
1396
+ return isMemberOfStringMapping(c, t, template);
1397
+ }
1398
+ function constrainedTypeVariableIndex(t) {
1399
+ return (t.types[0].flags & 34078720 /* TypeFlags.TypeVariable */) === 0 ? 1 : 0;
1400
+ }
1401
+ function removeConstrainedTypeVariables(c, types) {
1402
+ const typeVariables = [];
1403
+ for (const t of types) {
1404
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && (t.objectFlags & 134217728 /* ObjectFlags.IsConstrainedTypeVariable */) !== 0) {
1405
+ const v = t.types[constrainedTypeVariableIndex(t)];
1406
+ if (!typeVariables.includes(v))
1407
+ typeVariables.push(v);
1408
+ }
1409
+ }
1410
+ for (const typeVariable of typeVariables) {
1411
+ const primitives = [];
1412
+ for (const t of types) {
1413
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && (t.objectFlags & 134217728 /* ObjectFlags.IsConstrainedTypeVariable */) !== 0) {
1414
+ const index = constrainedTypeVariableIndex(t);
1415
+ if (t.types[index] === typeVariable)
1416
+ insertType(c, primitives, t.types[1 - index]);
1417
+ }
1418
+ }
1419
+ const constraint = getBaseConstraintOfType(c, typeVariable);
1420
+ if (constraint !== undefined && everyType(constraint, (t) => containsType(c, primitives, t))) {
1421
+ let i = types.length;
1422
+ while (i > 0) {
1423
+ i--;
1424
+ const t = types[i];
1425
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && (t.objectFlags & 134217728 /* ObjectFlags.IsConstrainedTypeVariable */) !== 0) {
1426
+ const index = constrainedTypeVariableIndex(t);
1427
+ const parts = t.types;
1428
+ if (parts[index] === typeVariable && containsType(c, primitives, parts[1 - index]))
1429
+ types.splice(i, 1);
1430
+ }
1431
+ }
1432
+ insertType(c, types, typeVariable);
1433
+ }
1434
+ }
1435
+ }
1436
+ function removeSubtypes(c, types, hasObjectTypes) {
1437
+ if (types.length < 2)
1438
+ return types;
1439
+ const hash = typeListHash(types, undefined, 0);
1440
+ const cached = c.subtypeReductionCache.get(hash, types, undefined, 0);
1441
+ if (cached !== undefined)
1442
+ return cached;
1443
+ // The reduction removes from `types`; the key is the list before.
1444
+ const keyTypes = types.slice();
1445
+ let hasEmptyObject = false;
1446
+ if (hasObjectTypes)
1447
+ for (const t of types)
1448
+ if ((t.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
1449
+ !isGenericMappedType(c, t) &&
1450
+ isEmptyResolvedType(c, t, resolveStructuredTypeMembers(c, t))) {
1451
+ hasEmptyObject = true;
1452
+ break;
1453
+ }
1454
+ const length = types.length;
1455
+ let i = length;
1456
+ let count = 0;
1457
+ while (i > 0) {
1458
+ i--;
1459
+ const source = types[i];
1460
+ if (!hasEmptyObject && (source.flags & 536346624 /* TypeFlags.StructuredOrInstantiable */) === 0)
1461
+ continue;
1462
+ // `T extends A | B` is a subtype of `A | B` but not of `A` or `B` alone.
1463
+ if ((source.flags & 524288 /* TypeFlags.TypeParameter */) !== 0 &&
1464
+ (getBaseConstraintOrType(c, source).flags & 134217728 /* TypeFlags.Union */) !== 0) {
1465
+ const others = [];
1466
+ for (const t of types)
1467
+ others.push(t === source ? c.neverType : t);
1468
+ if (isTypeRelatedTo(c, source, getUnionType(c, others), c.strictSubtypeRelation))
1469
+ types.splice(i, 1);
1470
+ continue;
1471
+ }
1472
+ // A property with a unit type quickly disqualifies constituents with a different unit type.
1473
+ let keyProperty;
1474
+ let keyPropertyType;
1475
+ if ((source.flags & (1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */)) !== 0) {
1476
+ for (const p of getPropertiesOfType(c, source))
1477
+ if (isUnitType(getTypeOfSymbol(c, p))) {
1478
+ keyProperty = p;
1479
+ break;
1480
+ }
1481
+ }
1482
+ if (keyProperty !== undefined)
1483
+ keyPropertyType = getRegularTypeOfLiteralType(c, getTypeOfSymbol(c, keyProperty));
1484
+ for (const target of types) {
1485
+ if (source === target)
1486
+ continue;
1487
+ if (count === 100000) {
1488
+ const estimatedCount = Math.trunc(count / (length - i)) * length;
1489
+ if (estimatedCount > 1000000) {
1490
+ c.error(c.currentNode, 738 /* Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent */);
1491
+ return undefined;
1492
+ }
1493
+ }
1494
+ count++;
1495
+ if (keyProperty !== undefined &&
1496
+ (target.flags & (1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */)) !== 0) {
1497
+ const t = getTypeOfPropertyOfType(c, target, keyProperty.name);
1498
+ if (t !== undefined && isUnitType(t) && getRegularTypeOfLiteralType(c, t) !== keyPropertyType)
1499
+ continue;
1500
+ }
1501
+ if ((source === c.emptyObjectType || source === c.unknownEmptyObjectType) &&
1502
+ target.symbol !== undefined &&
1503
+ isEmptyAnonymousObjectType(c, target))
1504
+ continue;
1505
+ if (isTypeRelatedTo(c, source, target, c.strictSubtypeRelation) &&
1506
+ ((getTargetType(source).objectFlags & 1 /* ObjectFlags.Class */) === 0 ||
1507
+ (getTargetType(target).objectFlags & 1 /* ObjectFlags.Class */) === 0 ||
1508
+ isTypeDerivedFrom(c, source, target))) {
1509
+ types.splice(i, 1);
1510
+ break;
1511
+ }
1512
+ }
1513
+ }
1514
+ c.subtypeReductionCache.set(hash, keyTypes, undefined, 0, types);
1515
+ return types;
1516
+ }
1517
+ export function getTargetType(type) {
1518
+ return (type.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 && (type.flags & 1048576 /* TypeFlags.Object */) !== 0
1519
+ ? type.target
1520
+ : type;
1521
+ }
1522
+ /** Whether `t`, whose members are `resolved`, has no members. */
1523
+ export function isEmptyResolvedType(c, t, resolved) {
1524
+ return (t !== c.anyFunctionType &&
1525
+ !hasResolvedProperties(resolved) &&
1526
+ resolved.callSignatures.length === 0 &&
1527
+ resolved.constructSignatures.length === 0 &&
1528
+ resolved.indexInfos.length === 0);
1529
+ }
1530
+ export function isEmptyAnonymousObjectType(c, t) {
1531
+ if ((t.objectFlags & 16 /* ObjectFlags.Anonymous */) === 0 || (t.flags & 1048576 /* TypeFlags.Object */) === 0)
1532
+ return false;
1533
+ if ((memberResolutionFlags(c, t) & 2097152 /* ObjectFlags.MembersResolved */) !== 0 &&
1534
+ isEmptyResolvedType(c, t, membersHolder(c, t)))
1535
+ return true;
1536
+ return (t.symbol !== undefined &&
1537
+ (t.symbol.flags & 2048 /* SymbolFlags.TypeLiteral */) !== 0 &&
1538
+ getMembersOfSymbol(c, t.symbol).size === 0);
1539
+ }
1540
+ export function isEmptyObjectType(c, t) {
1541
+ if ((t.flags & 1048576 /* TypeFlags.Object */) !== 0)
1542
+ return !isGenericMappedType(c, t) && isEmptyResolvedType(c, t, resolveStructuredTypeMembers(c, t));
1543
+ if ((t.flags & 131072 /* TypeFlags.NonPrimitive */) !== 0)
1544
+ return true;
1545
+ if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1546
+ for (const u of typesOf(t))
1547
+ if (isEmptyObjectType(c, u))
1548
+ return true;
1549
+ return false;
1550
+ }
1551
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
1552
+ for (const u of typesOf(t))
1553
+ if (!isEmptyObjectType(c, u))
1554
+ return false;
1555
+ return true;
1556
+ }
1557
+ return false;
1558
+ }
1559
+ export function isPatternLiteralPlaceholderType(c, t) {
1560
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
1561
+ let seenPlaceholder = false;
1562
+ for (const s of typesOf(t)) {
1563
+ if ((s.flags & (15360 /* TypeFlags.Literal */ | 12 /* TypeFlags.Nullable */)) !== 0 || isPatternLiteralPlaceholderType(c, s))
1564
+ seenPlaceholder = true;
1565
+ else if ((s.flags & 1048576 /* TypeFlags.Object */) === 0)
1566
+ return false;
1567
+ }
1568
+ return seenPlaceholder;
1569
+ }
1570
+ return ((t.flags & (1 /* TypeFlags.Any */ | 32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */ | 128 /* TypeFlags.BigInt */)) !== 0 ||
1571
+ isPatternLiteralType(c, t));
1572
+ }
1573
+ export function isPatternLiteralType(c, t) {
1574
+ if ((t.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
1575
+ for (const u of t.types)
1576
+ if (!isPatternLiteralPlaceholderType(c, u))
1577
+ return false;
1578
+ return true;
1579
+ }
1580
+ return ((t.flags & 8388608 /* TypeFlags.StringMapping */) !== 0 && isPatternLiteralPlaceholderType(c, t.target));
1581
+ }
1582
+ export function isGenericStringLikeType(c, t) {
1583
+ return (t.flags & (4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0 && !isPatternLiteralType(c, t);
1584
+ }
1585
+ // ---- Intersections
1586
+ export function intersectTypes(c, type1, type2) {
1587
+ if (type1 === undefined)
1588
+ return type2;
1589
+ if (type2 === undefined)
1590
+ return type1;
1591
+ return getIntersectionType(c, [type1, type2]);
1592
+ }
1593
+ export function getIntersectionType(c, types) {
1594
+ return getIntersectionTypeEx(c, types, 0 /* IntersectionFlags.None */, undefined);
1595
+ }
1596
+ class OrderedTypeSet {
1597
+ values = [];
1598
+ seen = new Set();
1599
+ includes = 0;
1600
+ has(t) {
1601
+ return this.seen.has(t);
1602
+ }
1603
+ add(t) {
1604
+ if (!this.seen.has(t)) {
1605
+ this.seen.add(t);
1606
+ this.values.push(t);
1607
+ }
1608
+ }
1609
+ }
1610
+ function addTypesToIntersection(c, set, types) {
1611
+ for (const t of types)
1612
+ addTypeToIntersection(c, set, getRegularTypeOfLiteralType(c, t));
1613
+ }
1614
+ function addTypeToIntersection(c, set, t) {
1615
+ const flags = t.flags;
1616
+ if ((flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
1617
+ addTypesToIntersection(c, set, t.types);
1618
+ return;
1619
+ }
1620
+ if (isEmptyAnonymousObjectType(c, t)) {
1621
+ if ((set.includes & 67108864 /* TypeFlags.IncludesEmptyObject */) === 0) {
1622
+ set.includes |= 67108864 /* TypeFlags.IncludesEmptyObject */;
1623
+ set.add(t);
1624
+ }
1625
+ return;
1626
+ }
1627
+ if ((flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0) {
1628
+ if (t === c.wildcardType)
1629
+ set.includes |= 33554432 /* TypeFlags.IncludesWildcard */;
1630
+ if (isErrorType(c, t))
1631
+ set.includes |= 1073741824 /* TypeFlags.IncludesError */;
1632
+ }
1633
+ else if (c.strictNullChecks || (flags & 12 /* TypeFlags.Nullable */) === 0) {
1634
+ if (t === c.missingType) {
1635
+ set.includes |= 524288 /* TypeFlags.IncludesMissingType */;
1636
+ t = c.undefinedType;
1637
+ }
1638
+ if (!set.has(t)) {
1639
+ // Two distinct unit types reduce to never; NonPrimitive triggers that.
1640
+ if ((t.flags & 97292 /* TypeFlags.Unit */) !== 0 && (set.includes & 97292 /* TypeFlags.Unit */) !== 0)
1641
+ set.includes |= 131072 /* TypeFlags.NonPrimitive */;
1642
+ set.add(t);
1643
+ }
1644
+ }
1645
+ set.includes |= flags & 416808959 /* TypeFlags.IncludesMask */;
1646
+ }
1647
+ function disjoint(includes, kind) {
1648
+ return (includes & kind) !== 0 && (includes & (12812284 /* TypeFlags.DisjointDomains */ & ~kind)) !== 0;
1649
+ }
1650
+ export function getIntersectionTypeEx(c, types, flags, alias) {
1651
+ const set = new OrderedTypeSet();
1652
+ addTypesToIntersection(c, set, types);
1653
+ const typeSet = set.values;
1654
+ const includes = set.includes;
1655
+ let objectFlags = 0 /* ObjectFlags.None */;
1656
+ if ((includes & 262144 /* TypeFlags.Never */) !== 0)
1657
+ return typeSet.includes(c.silentNeverType) ? c.silentNeverType : c.neverType;
1658
+ if ((c.strictNullChecks &&
1659
+ (includes & 12 /* TypeFlags.Nullable */) !== 0 &&
1660
+ (includes & (1048576 /* TypeFlags.Object */ | 131072 /* TypeFlags.NonPrimitive */ | 67108864 /* TypeFlags.IncludesEmptyObject */)) !== 0) ||
1661
+ disjoint(includes, 131072 /* TypeFlags.NonPrimitive */) ||
1662
+ disjoint(includes, 12583968 /* TypeFlags.StringLike */) ||
1663
+ disjoint(includes, 67648 /* TypeFlags.NumberLike */) ||
1664
+ disjoint(includes, 4224 /* TypeFlags.BigIntLike */) ||
1665
+ disjoint(includes, 16896 /* TypeFlags.ESSymbolLike */) ||
1666
+ disjoint(includes, 20 /* TypeFlags.VoidLike */))
1667
+ return c.neverType;
1668
+ if ((includes & (4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0 &&
1669
+ (includes & 1024 /* TypeFlags.StringLiteral */) !== 0) {
1670
+ if (extractRedundantTemplateLiterals(c, typeSet))
1671
+ return c.neverType;
1672
+ }
1673
+ if ((includes & 1 /* TypeFlags.Any */) !== 0) {
1674
+ if ((includes & 33554432 /* TypeFlags.IncludesWildcard */) !== 0)
1675
+ return c.wildcardType;
1676
+ if ((includes & 1073741824 /* TypeFlags.IncludesError */) !== 0)
1677
+ return c.errorType;
1678
+ return c.anyType;
1679
+ }
1680
+ if (!c.strictNullChecks && (includes & 12 /* TypeFlags.Nullable */) !== 0) {
1681
+ if ((includes & 67108864 /* TypeFlags.IncludesEmptyObject */) !== 0)
1682
+ return c.neverType;
1683
+ if ((includes & 4 /* TypeFlags.Undefined */) !== 0)
1684
+ return c.undefinedType;
1685
+ return c.nullType;
1686
+ }
1687
+ if (((includes & 32 /* TypeFlags.String */) !== 0 &&
1688
+ (includes & (1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0) ||
1689
+ ((includes & 64 /* TypeFlags.Number */) !== 0 && (includes & 2048 /* TypeFlags.NumberLiteral */) !== 0) ||
1690
+ ((includes & 128 /* TypeFlags.BigInt */) !== 0 && (includes & 4096 /* TypeFlags.BigIntLiteral */) !== 0) ||
1691
+ ((includes & 512 /* TypeFlags.ESSymbol */) !== 0 && (includes & 16384 /* TypeFlags.UniqueESSymbol */) !== 0) ||
1692
+ ((includes & 16 /* TypeFlags.Void */) !== 0 && (includes & 4 /* TypeFlags.Undefined */) !== 0) ||
1693
+ ((includes & 67108864 /* TypeFlags.IncludesEmptyObject */) !== 0 && (includes & 13893600 /* TypeFlags.DefinitelyNonNullable */) !== 0)) {
1694
+ if ((flags & 1 /* IntersectionFlags.NoSupertypeReduction */) === 0)
1695
+ removeRedundantSupertypes(c, typeSet, includes);
1696
+ }
1697
+ if ((includes & 524288 /* TypeFlags.IncludesMissingType */) !== 0)
1698
+ typeSet[typeSet.indexOf(c.undefinedType)] = c.missingType;
1699
+ if (typeSet.length === 0)
1700
+ return c.unknownType;
1701
+ if (typeSet.length === 1)
1702
+ return typeSet[0];
1703
+ if (typeSet.length === 2 && (flags & 2 /* IntersectionFlags.NoConstraintReduction */) === 0) {
1704
+ const typeVarIndex = (typeSet[0].flags & 34078720 /* TypeFlags.TypeVariable */) === 0 ? 1 : 0;
1705
+ const typeVariable = typeSet[typeVarIndex];
1706
+ const primitiveType = typeSet[1 - typeVarIndex];
1707
+ if ((typeVariable.flags & 34078720 /* TypeFlags.TypeVariable */) !== 0 &&
1708
+ (((primitiveType.flags & (12713980 /* TypeFlags.Primitive */ | 131072 /* TypeFlags.NonPrimitive */)) !== 0 &&
1709
+ !isGenericStringLikeType(c, primitiveType)) ||
1710
+ (includes & 67108864 /* TypeFlags.IncludesEmptyObject */) !== 0)) {
1711
+ // T & P where P is a primitive, `object` or {}.
1712
+ const constraint = getBaseConstraintOfType(c, typeVariable);
1713
+ if (constraint !== undefined && everyType(constraint, (t) => isPrimitiveOrObjectOrEmptyType(c, t))) {
1714
+ if (isTypeStrictSubtypeOf(c, constraint, primitiveType))
1715
+ return typeVariable;
1716
+ if (!((constraint.flags & 134217728 /* TypeFlags.Union */) !== 0 &&
1717
+ someType(constraint, (n) => isTypeStrictSubtypeOf(c, n, primitiveType)))) {
1718
+ if (!isTypeStrictSubtypeOf(c, primitiveType, constraint))
1719
+ return c.neverType;
1720
+ }
1721
+ objectFlags = 134217728 /* ObjectFlags.IsConstrainedTypeVariable */;
1722
+ }
1723
+ }
1724
+ }
1725
+ // Keys without constraint reduction ignore the alias.
1726
+ const keyAlias = (flags & 2 /* IntersectionFlags.NoConstraintReduction */) === 0 ? alias : undefined;
1727
+ const keyTag = (flags & 2 /* IntersectionFlags.NoConstraintReduction */) === 0 ? 0 : 1;
1728
+ const hash = typeListHash(typeSet, keyAlias, keyTag);
1729
+ let result = c.intersectionTypes.get(hash, typeSet, keyAlias, keyTag);
1730
+ let keyTypes = typeSet;
1731
+ if (result === undefined) {
1732
+ if ((includes & 134217728 /* TypeFlags.Union */) !== 0) {
1733
+ // The distributions below change `typeSet`; the key is the list before.
1734
+ keyTypes = typeSet.slice();
1735
+ if (intersectUnionsOfPrimitiveTypes(c, typeSet)) {
1736
+ // Restart with fresh flags; reduction happens at most once.
1737
+ result = getIntersectionTypeEx(c, typeSet, flags, alias);
1738
+ }
1739
+ else if (everyIsUnionWithUndefined(typeSet)) {
1740
+ let containedUndefinedType = c.undefinedType;
1741
+ for (const t of typeSet)
1742
+ if (containsMissingType(c, t))
1743
+ containedUndefinedType = c.missingType;
1744
+ for (let i = 0; i < typeSet.length; i++)
1745
+ typeSet[i] = filterType(c, typeSet[i], (t) => (t.flags & 4 /* TypeFlags.Undefined */) === 0);
1746
+ result = getUnionTypeEx(c, [getIntersectionTypeEx(c, typeSet, flags, undefined), containedUndefinedType], 1 /* UnionReduction.Literal */, alias, undefined);
1747
+ }
1748
+ else if (everyIsUnionWithNull(typeSet)) {
1749
+ for (let i = 0; i < typeSet.length; i++)
1750
+ typeSet[i] = filterType(c, typeSet[i], (t) => (t.flags & 8 /* TypeFlags.Null */) === 0);
1751
+ result = getUnionTypeEx(c, [getIntersectionTypeEx(c, typeSet, flags, undefined), c.nullType], 1 /* UnionReduction.Literal */, alias, undefined);
1752
+ }
1753
+ else if (typeSet.length >= 3 && types.length > 2) {
1754
+ // Divide and conquer: (A & B) & (C & D).
1755
+ const middle = typeSet.length >> 1;
1756
+ result = getIntersectionTypeEx(c, [
1757
+ getIntersectionTypeEx(c, typeSet.slice(0, middle), flags, undefined),
1758
+ getIntersectionTypeEx(c, typeSet.slice(middle), flags, undefined),
1759
+ ], flags, alias);
1760
+ }
1761
+ else {
1762
+ if (!checkCrossProductUnion(c, typeSet))
1763
+ return c.errorType;
1764
+ const constituents = getCrossProductIntersections(c, typeSet, flags);
1765
+ let origin;
1766
+ if (someHasFlags(constituents, 268435456 /* TypeFlags.Intersection */) &&
1767
+ getConstituentCountOfTypes(constituents) > getConstituentCountOfTypes(typeSet))
1768
+ origin = newIntersectionType(c, 0 /* ObjectFlags.None */, typeSet);
1769
+ result = getUnionTypeEx(c, constituents, 1 /* UnionReduction.Literal */, alias, origin);
1770
+ }
1771
+ }
1772
+ else {
1773
+ result = newIntersectionType(c, objectFlags | getPropagatingFlagsOfTypes(types, 12 /* TypeFlags.Nullable */), typeSet);
1774
+ result.alias = alias;
1775
+ }
1776
+ c.intersectionTypes.set(hash, keyTypes, keyAlias, keyTag, result);
1777
+ }
1778
+ return result;
1779
+ }
1780
+ export function isPrimitiveOrObjectOrEmptyType(c, t) {
1781
+ return (t.flags & (12713980 /* TypeFlags.Primitive */ | 131072 /* TypeFlags.NonPrimitive */)) !== 0 || isEmptyAnonymousObjectType(c, t);
1782
+ }
1783
+ export function containsMissingType(c, t) {
1784
+ return t === c.missingType || ((t.flags & 134217728 /* TypeFlags.Union */) !== 0 && typesOf(t)[0] === c.missingType);
1785
+ }
1786
+ function someHasFlags(types, flags) {
1787
+ for (const t of types)
1788
+ if ((t.flags & flags) !== 0)
1789
+ return true;
1790
+ return false;
1791
+ }
1792
+ function everyIsUnionWithUndefined(types) {
1793
+ for (const t of types)
1794
+ if (!isUnionWithUndefined(t))
1795
+ return false;
1796
+ return true;
1797
+ }
1798
+ function everyIsUnionWithNull(types) {
1799
+ for (const t of types)
1800
+ if (!isUnionWithNull(t))
1801
+ return false;
1802
+ return true;
1803
+ }
1804
+ function isUnionWithUndefined(t) {
1805
+ return (t.flags & 134217728 /* TypeFlags.Union */) !== 0 && (typesOf(t)[0].flags & 4 /* TypeFlags.Undefined */) !== 0;
1806
+ }
1807
+ function isUnionWithNull(t) {
1808
+ if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
1809
+ return false;
1810
+ const types = typesOf(t);
1811
+ return (types[0].flags & 8 /* TypeFlags.Null */) !== 0 || (types[1].flags & 8 /* TypeFlags.Null */) !== 0;
1812
+ }
1813
+ function removeRedundantSupertypes(c, types, includes) {
1814
+ let i = types.length;
1815
+ while (i > 0) {
1816
+ i--;
1817
+ const t = types[i];
1818
+ const remove = ((t.flags & 32 /* TypeFlags.String */) !== 0 &&
1819
+ (includes & (1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) !== 0) ||
1820
+ ((t.flags & 64 /* TypeFlags.Number */) !== 0 && (includes & 2048 /* TypeFlags.NumberLiteral */) !== 0) ||
1821
+ ((t.flags & 128 /* TypeFlags.BigInt */) !== 0 && (includes & 4096 /* TypeFlags.BigIntLiteral */) !== 0) ||
1822
+ ((t.flags & 512 /* TypeFlags.ESSymbol */) !== 0 && (includes & 16384 /* TypeFlags.UniqueESSymbol */) !== 0) ||
1823
+ ((t.flags & 16 /* TypeFlags.Void */) !== 0 && (includes & 4 /* TypeFlags.Undefined */) !== 0) ||
1824
+ (isEmptyAnonymousObjectType(c, t) && (includes & 13893600 /* TypeFlags.DefinitelyNonNullable */) !== 0);
1825
+ if (remove)
1826
+ types.splice(i, 1);
1827
+ }
1828
+ }
1829
+ /** Removes template literals that string literals satisfy; true if the intersection is empty. */
1830
+ function extractRedundantTemplateLiterals(c, types) {
1831
+ const literals = [];
1832
+ for (const t of types)
1833
+ if ((t.flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
1834
+ literals.push(t);
1835
+ let i = types.length;
1836
+ while (i > 0) {
1837
+ i--;
1838
+ const t = types[i];
1839
+ if ((t.flags & (4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */)) === 0)
1840
+ continue;
1841
+ for (const t2 of literals) {
1842
+ if (isTypeSubtypeOf(c, t2, t)) {
1843
+ types.splice(i, 1);
1844
+ break;
1845
+ }
1846
+ if (isPatternLiteralType(c, t))
1847
+ return true;
1848
+ }
1849
+ }
1850
+ return false;
1851
+ }
1852
+ function intersectUnionsOfPrimitiveTypes(c, types) {
1853
+ let index = 0;
1854
+ while (index < types.length && (types[index].objectFlags & 32768 /* ObjectFlags.PrimitiveUnion */) === 0)
1855
+ index++;
1856
+ if (index === types.length)
1857
+ return false;
1858
+ const unionTypes = [types[index]];
1859
+ let i = index + 1;
1860
+ while (i < types.length) {
1861
+ const t = types[i];
1862
+ if ((t.objectFlags & 32768 /* ObjectFlags.PrimitiveUnion */) !== 0) {
1863
+ unionTypes.push(t);
1864
+ types.splice(i, 1);
1865
+ }
1866
+ else
1867
+ i++;
1868
+ }
1869
+ if (unionTypes.length === 1)
1870
+ return false;
1871
+ const checked = [];
1872
+ const result = [];
1873
+ for (const u of unionTypes) {
1874
+ for (const t of typesOf(u)) {
1875
+ if (!insertType(c, checked, t))
1876
+ continue;
1877
+ if (!eachUnionContains(c, unionTypes, t))
1878
+ continue;
1879
+ // undefinedType and missingType sort first.
1880
+ if (t === c.undefinedType && result.length !== 0 && result[0] === c.missingType)
1881
+ continue;
1882
+ if (t === c.missingType && result.length !== 0 && result[0] === c.undefinedType) {
1883
+ result[0] = c.missingType;
1884
+ continue;
1885
+ }
1886
+ insertType(c, result, t);
1887
+ }
1888
+ }
1889
+ types[index] = getUnionTypeFromSortedList(c, result, 32768 /* ObjectFlags.PrimitiveUnion */, undefined, undefined);
1890
+ return true;
1891
+ }
1892
+ function eachUnionContains(c, unionTypes, t) {
1893
+ for (const type of unionTypes) {
1894
+ const u = type;
1895
+ if (unionContainsType(c, u, t))
1896
+ continue;
1897
+ if (t === c.missingType)
1898
+ return unionContainsType(c, u, c.undefinedType);
1899
+ if (t === c.undefinedType)
1900
+ return unionContainsType(c, u, c.missingType);
1901
+ let primitive;
1902
+ if ((t.flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
1903
+ primitive = c.stringType;
1904
+ else if ((t.flags & (65536 /* TypeFlags.Enum */ | 2048 /* TypeFlags.NumberLiteral */)) !== 0)
1905
+ primitive = c.numberType;
1906
+ else if ((t.flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0)
1907
+ primitive = c.bigintType;
1908
+ else if ((t.flags & 16384 /* TypeFlags.UniqueESSymbol */) !== 0)
1909
+ primitive = c.esSymbolType;
1910
+ if (primitive === undefined || !unionContainsType(c, u, primitive))
1911
+ return false;
1912
+ }
1913
+ return true;
1914
+ }
1915
+ export function getCrossProductUnionSize(types) {
1916
+ let size = 1;
1917
+ for (const t of types) {
1918
+ if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0)
1919
+ size *= typesOf(t).length;
1920
+ else if ((t.flags & 262144 /* TypeFlags.Never */) !== 0)
1921
+ return 0;
1922
+ }
1923
+ return size;
1924
+ }
1925
+ export function checkCrossProductUnion(c, types) {
1926
+ const size = getCrossProductUnionSize(types);
1927
+ if (size >= 100000) {
1928
+ c.error(c.currentNode, 738 /* Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent */);
1929
+ return false;
1930
+ }
1931
+ return true;
1932
+ }
1933
+ function getCrossProductIntersections(c, types, flags) {
1934
+ if (allWithoutMembersToResolve(c, types)) {
1935
+ const intersections = [];
1936
+ addCrossProductIntersections(c, types, flags, types.slice(), 0, undefined, intersections);
1937
+ return intersections;
1938
+ }
1939
+ const count = getCrossProductUnionSize(types);
1940
+ const intersections = [];
1941
+ for (let i = 0; i < count; i++) {
1942
+ const constituents = types.slice();
1943
+ let n = i;
1944
+ for (let j = types.length - 1; j >= 0; j--) {
1945
+ if ((types[j].flags & 134217728 /* TypeFlags.Union */) !== 0) {
1946
+ const sourceTypes = typesOf(types[j]);
1947
+ const length = sourceTypes.length;
1948
+ constituents[j] = sourceTypes[n % length];
1949
+ n = Math.trunc(n / length);
1950
+ }
1951
+ }
1952
+ const t = getIntersectionTypeEx(c, constituents, flags, undefined);
1953
+ if ((t.flags & 262144 /* TypeFlags.Never */) === 0)
1954
+ intersections.push(t);
1955
+ }
1956
+ return intersections;
1957
+ }
1958
+ /**
1959
+ * Adds the intersections of `constituents` with each combination of the constituents of `types` from
1960
+ * position `index` on, in the order of `getCrossProductIntersections`, except the never ones. A
1961
+ * combination with two distinct unit types (`unit` is the one taken before `index`) is never:
1962
+ * `addTypeToIntersection` marks it NonPrimitive, which is disjoint from the domain of the first unit
1963
+ * type, so `getIntersectionTypeEx` returns never before it creates or caches anything. Such
1964
+ * combinations, and every combination extending them, are skipped without calling it.
1965
+ */
1966
+ function addCrossProductIntersections(c, types, flags, constituents, index, unit, intersections) {
1967
+ if (index === types.length) {
1968
+ const t = getIntersectionTypeEx(c, constituents.slice(), flags, undefined);
1969
+ if ((t.flags & 262144 /* TypeFlags.Never */) === 0)
1970
+ intersections.push(t);
1971
+ return;
1972
+ }
1973
+ const t = types[index];
1974
+ const candidates = (t.flags & 134217728 /* TypeFlags.Union */) !== 0 ? typesOf(t) : [t];
1975
+ for (const candidate of candidates) {
1976
+ const candidateUnit = getIntersectionUnitType(c, candidate);
1977
+ if (candidateUnit !== undefined && unit !== undefined && candidateUnit !== unit)
1978
+ continue;
1979
+ constituents[index] = candidate;
1980
+ addCrossProductIntersections(c, types, flags, constituents, index + 1, unit ?? candidateUnit, intersections);
1981
+ }
1982
+ }
1983
+ /**
1984
+ * The unit type `t` adds to an intersection (`addTypeToIntersection`), or `undefined` when it adds
1985
+ * none: regular literal types for fresh ones, undefined for the missing type, and no nullable type
1986
+ * without strictNullChecks.
1987
+ */
1988
+ function getIntersectionUnitType(c, t) {
1989
+ if ((t.flags & 97292 /* TypeFlags.Unit */) === 0)
1990
+ return undefined;
1991
+ if (!c.strictNullChecks && (t.flags & 12 /* TypeFlags.Nullable */) !== 0)
1992
+ return undefined;
1993
+ const regular = getRegularTypeOfLiteralType(c, t);
1994
+ return regular === c.missingType ? c.undefinedType : regular;
1995
+ }
1996
+ /**
1997
+ * Whether adding `t`, or any constituent of the union `t`, to an intersection resolves nothing:
1998
+ * `isEmptyAnonymousObjectType` reads the members of a type literal's symbol, which binds its late
1999
+ * bound members the first time, unless the type's members are resolved already.
2000
+ */
2001
+ function isIntersectionConstituentWithoutMembersToResolve(c, t) {
2002
+ if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0)
2003
+ return allWithoutMembersToResolve(c, typesOf(t));
2004
+ return ((t.objectFlags & 16 /* ObjectFlags.Anonymous */) === 0 ||
2005
+ (t.flags & 1048576 /* TypeFlags.Object */) === 0 ||
2006
+ (memberResolutionFlags(c, t) & 2097152 /* ObjectFlags.MembersResolved */) !== 0 ||
2007
+ t.symbol === undefined ||
2008
+ (t.symbol.flags & 2048 /* SymbolFlags.TypeLiteral */) === 0);
2009
+ }
2010
+ function allWithoutMembersToResolve(c, types) {
2011
+ for (const t of types)
2012
+ if (!isIntersectionConstituentWithoutMembersToResolve(c, t))
2013
+ return false;
2014
+ return true;
2015
+ }
2016
+ function getConstituentCount(t) {
2017
+ if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) === 0 || t.alias !== undefined)
2018
+ return 1;
2019
+ if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0 && t.origin !== undefined)
2020
+ return getConstituentCount(t.origin);
2021
+ return getConstituentCountOfTypes(typesOf(t));
2022
+ }
2023
+ function getConstituentCountOfTypes(types) {
2024
+ let n = 0;
2025
+ for (const t of types)
2026
+ n += getConstituentCount(t);
2027
+ return n;
2028
+ }
2029
+ export function getUnionOrIntersectionType(c, types, isUnion, reduction) {
2030
+ return isUnion ? getUnionTypeEx(c, types, reduction, undefined, undefined) : getIntersectionType(c, types);
2031
+ }
2032
+ // ---- Type references, arrays and tuples
2033
+ export function createTypeReference(c, target, typeArguments) {
2034
+ return createTypeReferenceEx(c, target, typeArguments, 0 /* ObjectFlags.None */);
2035
+ }
2036
+ export function createTypeReferenceEx(c, target, typeArguments, objectFlags) {
2037
+ const hash = typeListHash(typeArguments, undefined, 0);
2038
+ const generic = sideOf(c, target);
2039
+ let references = generic.references;
2040
+ if (references === undefined) {
2041
+ references = new ReferenceCache();
2042
+ generic.references = references;
2043
+ }
2044
+ const existing = references.get(hash, typeArguments);
2045
+ if (existing !== undefined)
2046
+ return existing;
2047
+ const type = newObjectType(c, 4 /* ObjectFlags.Reference */ | objectFlags | getPropagatingFlagsOfTypes(typeArguments, 0 /* TypeFlags.None */), target.symbol);
2048
+ type.target = target;
2049
+ type.resolvedTypeArguments = typeArguments;
2050
+ references.add(hash, type);
2051
+ return type;
2052
+ }
2053
+ export function tryCreateTypeReference(c, target, typeArguments) {
2054
+ if (typeArguments.length !== 0 && target === c.emptyGenericType)
2055
+ return c.unknownType;
2056
+ return createTypeReference(c, target, typeArguments);
2057
+ }
2058
+ export function createDeferredTypeReference(c, target, node, mapper, alias) {
2059
+ if (alias === undefined) {
2060
+ // A reference written as a type alias's whole type goes by the alias.
2061
+ alias = getAliasForTypeNode(c, node);
2062
+ if (alias !== undefined && mapper !== undefined)
2063
+ alias = new TypeAlias(alias.symbol, instantiateTypes(c, alias.typeArguments, mapper));
2064
+ }
2065
+ const type = newObjectType(c, 4 /* ObjectFlags.Reference */, target.symbol);
2066
+ type.alias = alias;
2067
+ type.target = target;
2068
+ type.mapper = mapper;
2069
+ type.node = node;
2070
+ return type;
2071
+ }
2072
+ export function cloneTypeReference(c, source) {
2073
+ const type = newObjectType(c, 4 /* ObjectFlags.Reference */, source.symbol);
2074
+ // The clone keeps its own side state: a source's lazily computed state is not the clone's.
2075
+ type.objectFlags =
2076
+ (source.objectFlags & ~(2097152 /* ObjectFlags.MembersResolved */ | -2147483648 /* ObjectFlags.SideState */)) |
2077
+ (type.objectFlags & -2147483648 /* ObjectFlags.SideState */);
2078
+ type.target = source.target;
2079
+ type.resolvedTypeArguments = source.resolvedTypeArguments ?? sideOfIfAny(c, source)?.resolvedTypeArguments;
2080
+ return type;
2081
+ }
2082
+ export function createTypeFromGenericGlobalType(c, genericGlobalType, typeArguments) {
2083
+ return genericGlobalType !== c.emptyGenericType
2084
+ ? createTypeReference(c, genericGlobalType, typeArguments)
2085
+ : c.emptyObjectType;
2086
+ }
2087
+ export function createArrayType(c, elementType) {
2088
+ return createTypeFromGenericGlobalType(c, c.globalArrayType, [elementType]);
2089
+ }
2090
+ export function createArrayTypeEx(c, elementType, readonly) {
2091
+ return createTypeFromGenericGlobalType(c, readonly ? c.globalReadonlyArrayType : c.globalArrayType, [elementType]);
2092
+ }
2093
+ export function isTupleType(t) {
2094
+ return ((t.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 &&
2095
+ (t.target.objectFlags & 8 /* ObjectFlags.Tuple */) !== 0);
2096
+ }
2097
+ export function tupleTargetOf(t) {
2098
+ return t.target;
2099
+ }
2100
+ export function isGenericTupleType(t) {
2101
+ return isTupleType(t) && (tupleTargetOf(t).combinedFlags & 8 /* ElementFlags.Variadic */) !== 0;
2102
+ }
2103
+ export function isArrayType(c, t) {
2104
+ if ((t.objectFlags & 4 /* ObjectFlags.Reference */) === 0)
2105
+ return false;
2106
+ const target = t.target;
2107
+ return target === c.globalArrayType || target === c.globalReadonlyArrayType;
2108
+ }
2109
+ export function isReadonlyArrayType(c, t) {
2110
+ return (t.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 && t.target === c.globalReadonlyArrayType;
2111
+ }
2112
+ export function isArrayOrTupleType(c, t) {
2113
+ return isArrayType(c, t) || isTupleType(t);
2114
+ }
2115
+ export function isMutableArrayOrTuple(c, t) {
2116
+ return (isArrayType(c, t) && !isReadonlyArrayType(c, t)) || (isTupleType(t) && !tupleTargetOf(t).readonly);
2117
+ }
2118
+ export function getTypeReferenceArity(t) {
2119
+ return t.target.typeParameterCount();
2120
+ }
2121
+ export function getElementTypes(c, t) {
2122
+ const typeArguments = getTypeArguments(c, t);
2123
+ const arity = getTypeReferenceArity(t);
2124
+ return typeArguments.length === arity ? typeArguments : typeArguments.slice(0, arity);
2125
+ }
2126
+ export function getStartElementCount(t, flags) {
2127
+ for (let i = 0; i < t.elementInfos.length; i++)
2128
+ if ((t.elementInfos[i].flags & flags) === 0)
2129
+ return i;
2130
+ return t.elementInfos.length;
2131
+ }
2132
+ export function getEndElementCount(t, flags) {
2133
+ for (let i = t.elementInfos.length; i > 0; i--)
2134
+ if ((t.elementInfos[i - 1].flags & flags) === 0)
2135
+ return t.elementInfos.length - i;
2136
+ return t.elementInfos.length;
2137
+ }
2138
+ export function getTotalFixedElementCount(t) {
2139
+ return t.fixedLength + getEndElementCount(t, 3 /* ElementFlags.Fixed */);
2140
+ }
2141
+ export function createTupleType(c, elementTypes) {
2142
+ const infos = [];
2143
+ for (let i = 0; i < elementTypes.length; i++)
2144
+ infos.push(new TupleElementInfo(1 /* ElementFlags.Required */, undefined));
2145
+ return createTupleTypeEx(c, elementTypes, infos, false);
2146
+ }
2147
+ export function createTupleTypeEx(c, elementTypes, elementInfos, readonly) {
2148
+ const tupleTarget = getTupleTargetType(c, elementInfos, readonly);
2149
+ if (tupleTarget === c.emptyGenericType)
2150
+ return c.emptyObjectType;
2151
+ if (elementTypes.length !== 0)
2152
+ return createNormalizedTypeReference(c, tupleTarget, elementTypes);
2153
+ return tupleTarget;
2154
+ }
2155
+ function tupleKey(elementInfos, readonly) {
2156
+ let key = '';
2157
+ for (const e of elementInfos) {
2158
+ if ((e.flags & 1 /* ElementFlags.Required */) !== 0)
2159
+ key += '#';
2160
+ else if ((e.flags & 2 /* ElementFlags.Optional */) !== 0)
2161
+ key += '?';
2162
+ else if ((e.flags & 4 /* ElementFlags.Rest */) !== 0)
2163
+ key += '.';
2164
+ else
2165
+ key += '*';
2166
+ if (e.labeledDeclaration !== undefined)
2167
+ key += getNodeId(e.labeledDeclaration);
2168
+ }
2169
+ return readonly ? key + '!' : key;
2170
+ }
2171
+ export function getTupleTargetType(c, elementInfos, readonly) {
2172
+ if (elementInfos.length === 1 && (elementInfos[0].flags & 4 /* ElementFlags.Rest */) !== 0)
2173
+ return readonly ? c.globalReadonlyArrayType : c.globalArrayType;
2174
+ const key = tupleKey(elementInfos, readonly);
2175
+ let type = c.tupleTypes.get(key);
2176
+ if (type === undefined) {
2177
+ type = createTupleTargetType(c, elementInfos, readonly);
2178
+ c.tupleTypes.set(key, type);
2179
+ }
2180
+ return type;
2181
+ }
2182
+ /** `interface Tuple<T0, T1, …> extends Array<T0 | T1 | …> { 0: T0, 1: T1, … }`, without a symbol. */
2183
+ function createTupleTargetType(c, elementInfos, readonly) {
2184
+ const arity = elementInfos.length;
2185
+ let minLength = 0;
2186
+ for (const e of elementInfos)
2187
+ if ((e.flags & (1 /* ElementFlags.Required */ | 8 /* ElementFlags.Variadic */)) !== 0)
2188
+ minLength++;
2189
+ const typeParameters = [];
2190
+ const members = new Map();
2191
+ let combinedFlags = 0 /* ElementFlags.None */;
2192
+ for (let i = 0; i < arity; i++) {
2193
+ const typeParameter = c.newTypeParameter(undefined);
2194
+ typeParameters.push(typeParameter);
2195
+ const flags = elementInfos[i].flags;
2196
+ combinedFlags |= flags;
2197
+ if ((combinedFlags & 12 /* ElementFlags.Variable */) === 0) {
2198
+ const property = c.newSymbolWithCheckFlags(4 /* SymbolFlags.Property */ | ((flags & 2 /* ElementFlags.Optional */) !== 0 ? 16777216 /* SymbolFlags.Optional */ : 0), String(i), readonly ? 8 /* CheckFlags.Readonly */ : 0 /* CheckFlags.None */);
2199
+ c.symbolTypes.set(getSymbolId(property), typeParameter);
2200
+ members.set(property.name, property);
2201
+ }
2202
+ }
2203
+ const fixedLength = members.size;
2204
+ const lengthSymbol = c.newSymbolWithCheckFlags(4 /* SymbolFlags.Property */, 'length', readonly ? 8 /* CheckFlags.Readonly */ : 0 /* CheckFlags.None */);
2205
+ if ((combinedFlags & 12 /* ElementFlags.Variable */) !== 0)
2206
+ c.symbolTypes.set(getSymbolId(lengthSymbol), c.numberType);
2207
+ else {
2208
+ const literalTypes = [];
2209
+ for (let i = minLength; i <= arity; i++)
2210
+ literalTypes.push(getNumberLiteralType(c, i));
2211
+ c.symbolTypes.set(getSymbolId(lengthSymbol), getUnionType(c, literalTypes));
2212
+ }
2213
+ members.set(lengthSymbol.name, lengthSymbol);
2214
+ const type = newObjectType(c, 8 /* ObjectFlags.Tuple */ | 4 /* ObjectFlags.Reference */, undefined);
2215
+ const thisType = c.newTypeParameter(undefined);
2216
+ thisType.isThisType = true;
2217
+ thisType.constraint = type;
2218
+ type.thisType = thisType;
2219
+ typeParameters.push(thisType);
2220
+ type.allTypeParameters = typeParameters;
2221
+ const ownParameters = type.typeParameters();
2222
+ type.target = type;
2223
+ type.resolvedTypeArguments = ownParameters;
2224
+ const references = new ReferenceCache();
2225
+ sideOf(c, type).references = references;
2226
+ references.add(typeListHash(ownParameters, undefined, 0), type);
2227
+ const declared = sideOf(c, type);
2228
+ declared.declaredMembersResolved = true;
2229
+ declared.declaredMembers = members;
2230
+ type.elementInfos = elementInfos;
2231
+ type.minLength = minLength;
2232
+ type.fixedLength = fixedLength;
2233
+ type.combinedFlags = combinedFlags;
2234
+ type.readonly = readonly;
2235
+ return type;
2236
+ }
2237
+ export function createNormalizedTypeReference(c, target, typeArguments) {
2238
+ if ((target.objectFlags & 8 /* ObjectFlags.Tuple */) !== 0)
2239
+ return createNormalizedTupleType(c, target, typeArguments, 0 /* ObjectFlags.None */);
2240
+ return createTypeReference(c, target, typeArguments);
2241
+ }
2242
+ export function createNormalizedTupleType(c, target, elementTypes, objectFlags) {
2243
+ if ((target.combinedFlags & 14 /* ElementFlags.NonRequired */) === 0)
2244
+ return createTypeReferenceEx(c, target, elementTypes, objectFlags);
2245
+ const infos = target.elementInfos;
2246
+ if ((target.combinedFlags & 8 /* ElementFlags.Variadic */) !== 0) {
2247
+ for (let i = 0; i < elementTypes.length; i++) {
2248
+ const e = elementTypes[i];
2249
+ if (i < infos.length &&
2250
+ (infos[i].flags & 8 /* ElementFlags.Variadic */) !== 0 &&
2251
+ (e.flags & (262144 /* TypeFlags.Never */ | 134217728 /* TypeFlags.Union */)) !== 0) {
2252
+ // [A, ...(X | Y)] becomes [A, ...X] | [A, ...Y].
2253
+ const checkTypes = [];
2254
+ for (let j = 0; j < elementTypes.length; j++)
2255
+ checkTypes.push(j < infos.length && (infos[j].flags & 8 /* ElementFlags.Variadic */) !== 0 ? elementTypes[j] : c.unknownType);
2256
+ if (checkCrossProductUnion(c, checkTypes)) {
2257
+ return mapType(c, e, (t) => {
2258
+ const replaced = elementTypes.slice();
2259
+ replaced[i] = t;
2260
+ return createNormalizedTupleType(c, target, replaced, objectFlags);
2261
+ });
2262
+ }
2263
+ }
2264
+ }
2265
+ }
2266
+ const n = new TupleNormalizer(c);
2267
+ if (!n.normalize(elementTypes.slice(0, infos.length), infos))
2268
+ return c.errorType;
2269
+ if (elementTypes.length > infos.length)
2270
+ n.types.push(elementTypes[infos.length]);
2271
+ const tupleTarget = getTupleTargetType(c, n.infos, target.readonly);
2272
+ if (tupleTarget === c.emptyGenericType)
2273
+ return c.emptyObjectType;
2274
+ if (n.types.length !== 0)
2275
+ return createTypeReferenceEx(c, tupleTarget, n.types, objectFlags);
2276
+ return tupleTarget;
2277
+ }
2278
+ /**
2279
+ * Normalizes tuple elements to required, then optional, then one rest element, or required,
2280
+ * rest, required; generic variadic elements may appear anywhere.
2281
+ */
2282
+ class TupleNormalizer {
2283
+ c;
2284
+ types = [];
2285
+ infos = [];
2286
+ lastRequiredIndex = -1;
2287
+ firstRestIndex = -1;
2288
+ lastOptionalOrRestIndex = -1;
2289
+ constructor(c) {
2290
+ this.c = c;
2291
+ }
2292
+ normalize(elementTypes, elementInfos) {
2293
+ const c = this.c;
2294
+ for (let i = 0; i < elementTypes.length; i++) {
2295
+ const t = elementTypes[i];
2296
+ const info = elementInfos[i];
2297
+ if ((info.flags & 8 /* ElementFlags.Variadic */) === 0) {
2298
+ this.add(t, info);
2299
+ continue;
2300
+ }
2301
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
2302
+ this.add(t, new TupleElementInfo(4 /* ElementFlags.Rest */, info.labeledDeclaration));
2303
+ else if ((t.flags & 117964800 /* TypeFlags.InstantiableNonPrimitive */) !== 0 || isGenericMappedType(c, t))
2304
+ this.add(t, info);
2305
+ else if (isTupleType(t)) {
2306
+ const spreadTypes = getElementTypes(c, t);
2307
+ if (spreadTypes.length + this.types.length >= 10000) {
2308
+ c.error(c.currentNode, c.currentNode !== undefined && isPartOfTypeNode(c.currentNode)
2309
+ ? 931 /* Diagnostics.Type_produces_a_tuple_type_that_is_too_large_to_represent */
2310
+ : 932 /* Diagnostics.Expression_produces_a_tuple_type_that_is_too_large_to_represent */);
2311
+ return false;
2312
+ }
2313
+ const spreadInfos = tupleTargetOf(t).elementInfos;
2314
+ for (let j = 0; j < spreadTypes.length; j++)
2315
+ this.add(spreadTypes[j], spreadInfos[j]);
2316
+ }
2317
+ else {
2318
+ const s = (isArrayLikeType(c, t) ? getIndexTypeOfType(c, t, c.numberType) : undefined) ?? c.errorType;
2319
+ this.add(s, new TupleElementInfo(4 /* ElementFlags.Rest */, info.labeledDeclaration));
2320
+ }
2321
+ }
2322
+ // Optional elements before the last required one become required.
2323
+ for (let i = 0; i < this.lastRequiredIndex; i++) {
2324
+ const info = this.infos[i];
2325
+ if ((info.flags & 2 /* ElementFlags.Optional */) !== 0)
2326
+ this.infos[i] = new TupleElementInfo(1 /* ElementFlags.Required */, info.labeledDeclaration);
2327
+ }
2328
+ if (this.firstRestIndex >= 0 && this.firstRestIndex < this.lastOptionalOrRestIndex) {
2329
+ // Elements from the first rest to the last optional or rest become one rest element.
2330
+ const types = [];
2331
+ for (let i = this.firstRestIndex; i <= this.lastOptionalOrRestIndex; i++) {
2332
+ let t = this.types[i];
2333
+ if ((this.infos[i].flags & 8 /* ElementFlags.Variadic */) !== 0)
2334
+ t = getIndexedAccessType(c, t, c.numberType);
2335
+ types.push(t);
2336
+ }
2337
+ this.types[this.firstRestIndex] = getUnionType(c, types);
2338
+ const count = this.lastOptionalOrRestIndex - this.firstRestIndex;
2339
+ this.types.splice(this.firstRestIndex + 1, count);
2340
+ this.infos.splice(this.firstRestIndex + 1, count);
2341
+ }
2342
+ return true;
2343
+ }
2344
+ add(t, info) {
2345
+ if ((info.flags & 1 /* ElementFlags.Required */) !== 0)
2346
+ this.lastRequiredIndex = this.types.length;
2347
+ if ((info.flags & 4 /* ElementFlags.Rest */) !== 0 && this.firstRestIndex < 0)
2348
+ this.firstRestIndex = this.types.length;
2349
+ if ((info.flags & (2 /* ElementFlags.Optional */ | 4 /* ElementFlags.Rest */)) !== 0)
2350
+ this.lastOptionalOrRestIndex = this.types.length;
2351
+ this.types.push(addOptionality(this.c, t, true, (info.flags & 2 /* ElementFlags.Optional */) !== 0));
2352
+ this.infos.push(info);
2353
+ }
2354
+ }
2355
+ /** Type arguments of a reference, resolving deferred ones from their type node. */
2356
+ export function getTypeArguments(c, t) {
2357
+ const ref = t;
2358
+ let resolved = ref.resolvedTypeArguments;
2359
+ if (resolved !== undefined)
2360
+ return resolved;
2361
+ // A deferred reference resolves its type arguments on first use.
2362
+ const side = sideOf(c, ref);
2363
+ resolved = side.resolvedTypeArguments;
2364
+ if (resolved !== undefined)
2365
+ return resolved;
2366
+ const target = ref.target;
2367
+ const errors = () => {
2368
+ const result = [];
2369
+ for (let i = 0; i < target.typeParameterCount(); i++)
2370
+ result.push(c.errorType);
2371
+ return result;
2372
+ };
2373
+ if (!pushTypeResolution(c, t, 5 /* TypeSystemPropertyName.ResolvedTypeArguments */))
2374
+ return errors();
2375
+ const node = ref.node;
2376
+ let typeArguments = emptyTypes;
2377
+ if (node !== undefined)
2378
+ typeArguments = getTypeArgumentsOfDeferredReference(c, node, target);
2379
+ if (popTypeResolution(c)) {
2380
+ resolved = side.resolvedTypeArguments;
2381
+ if (resolved === undefined) {
2382
+ resolved = instantiateTypes(c, typeArguments, ref.mapper);
2383
+ side.resolvedTypeArguments = resolved;
2384
+ }
2385
+ }
2386
+ else {
2387
+ resolved = side.resolvedTypeArguments;
2388
+ if (resolved === undefined) {
2389
+ resolved = errors();
2390
+ side.resolvedTypeArguments = resolved;
2391
+ }
2392
+ const errorNode = node ?? c.currentNode;
2393
+ if (target.symbol !== undefined)
2394
+ c.error(errorNode, 1095 /* Diagnostics.Type_arguments_for_0_circularly_reference_themselves */, symbolToString(c, target.symbol));
2395
+ else
2396
+ c.error(errorNode, 1096 /* Diagnostics.Tuple_type_arguments_circularly_reference_themselves */);
2397
+ }
2398
+ return resolved;
2399
+ }
2400
+ // ---- Template literals
2401
+ function everyHasFlags(types, flags) {
2402
+ for (const t of types)
2403
+ if ((t.flags & flags) === 0)
2404
+ return false;
2405
+ return true;
2406
+ }
2407
+ export function getTemplateLiteralType(c, texts, types) {
2408
+ let unionIndex = 0;
2409
+ while (unionIndex < types.length && (types[unionIndex].flags & (262144 /* TypeFlags.Never */ | 134217728 /* TypeFlags.Union */)) === 0)
2410
+ unionIndex++;
2411
+ if (unionIndex < types.length) {
2412
+ // The distribution over a union depends only on the texts and types, so it is cached; results
2413
+ // that reported a too complex union are not, so each such use reports it.
2414
+ const key = 'templateLiteral:' + typeListKey(types) + '|' + texts.join('\u0000');
2415
+ const cached = c.cachedTypes.get(key);
2416
+ if (cached !== undefined)
2417
+ return cached;
2418
+ if (!checkCrossProductUnion(c, types))
2419
+ return c.errorType;
2420
+ const result = mapType(c, types[unionIndex], (t) => {
2421
+ const replaced = types.slice();
2422
+ replaced[unionIndex] = t;
2423
+ return getTemplateLiteralType(c, texts, replaced);
2424
+ });
2425
+ c.cachedTypes.set(key, result);
2426
+ return result;
2427
+ }
2428
+ if (types.includes(c.wildcardType))
2429
+ return c.wildcardType;
2430
+ const builder = new TemplateBuilder(c, texts[0]);
2431
+ if (!builder.addSpans(texts, types))
2432
+ return c.stringType;
2433
+ if (builder.newTypes.length === 0)
2434
+ return getStringLiteralType(c, builder.text);
2435
+ builder.newTexts.push(builder.text);
2436
+ const newTexts = builder.newTexts;
2437
+ const newTypes = builder.newTypes;
2438
+ if (newTexts.every((t) => t === '')) {
2439
+ if (everyHasFlags(newTypes, 32 /* TypeFlags.String */))
2440
+ return c.stringType;
2441
+ // `${Mapping<xxx>}` is Mapping<xxx>.
2442
+ if (newTypes.length === 1 && isPatternLiteralType(c, newTypes[0]))
2443
+ return newTypes[0];
2444
+ }
2445
+ const key = newTexts.join('\u0000') + '|' + typeListKey(newTypes);
2446
+ let type = c.templateLiteralTypes.get(key);
2447
+ if (type === undefined) {
2448
+ type = new TemplateLiteralType(c.nextTypeId(), newTexts, newTypes);
2449
+ c.templateLiteralTypes.set(key, type);
2450
+ }
2451
+ return type;
2452
+ }
2453
+ class TemplateBuilder {
2454
+ c;
2455
+ text;
2456
+ newTypes = [];
2457
+ newTexts = [];
2458
+ constructor(c, text) {
2459
+ this.c = c;
2460
+ this.text = text;
2461
+ }
2462
+ addSpans(texts, types) {
2463
+ const c = this.c;
2464
+ for (let i = 0; i < types.length; i++) {
2465
+ const t = types[i];
2466
+ if ((t.flags & (15360 /* TypeFlags.Literal */ | 8 /* TypeFlags.Null */ | 4 /* TypeFlags.Undefined */)) !== 0) {
2467
+ this.text += getTemplateStringForType(c, t) + texts[i + 1];
2468
+ }
2469
+ else if ((t.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
2470
+ const template = t;
2471
+ this.text += template.texts[0];
2472
+ if (!this.addSpans(template.texts, template.types))
2473
+ return false;
2474
+ this.text += texts[i + 1];
2475
+ }
2476
+ else if (isGenericIndexType(c, t) || isPatternLiteralPlaceholderType(c, t)) {
2477
+ this.newTypes.push(t);
2478
+ this.newTexts.push(this.text);
2479
+ this.text = texts[i + 1];
2480
+ }
2481
+ else
2482
+ return false;
2483
+ }
2484
+ return true;
2485
+ }
2486
+ }
2487
+ export function getTemplateStringForType(c, t) {
2488
+ if ((t.flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
2489
+ return t.stringValue;
2490
+ if ((t.flags & 2048 /* TypeFlags.NumberLiteral */) !== 0)
2491
+ return numberToString(t.numberValue);
2492
+ if ((t.flags & (8192 /* TypeFlags.BooleanLiteral */ | 4096 /* TypeFlags.BigIntLiteral */)) !== 0)
2493
+ return t.stringValue;
2494
+ if ((t.flags & 12 /* TypeFlags.Nullable */) !== 0)
2495
+ return t.intrinsicName;
2496
+ return '';
2497
+ }
2498
+ /** JavaScript's Number::toString. */
2499
+ export function numberToString(value) {
2500
+ return String(value);
2501
+ }