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,2105 @@
1
+ // Type inference: inference contexts, inferring from source to target types, and the inferred
2
+ // types of type parameters.
3
+ import { sideOf, signatureSideOf } from './side-state.js';
4
+ import { hasSyntacticModifier } from './binder-utilities.js';
5
+ import { getSymbolId } from './symbol.js';
6
+ import { ArrayToSingleTypeMapper, MergedTypeMapper, TupleElementInfo, TypeMapper, newTypeMapper, SimpleTypeMapper, emptyTypes, } from './checker-types.js';
7
+ import { createAnonymousType, createArrayType, createArrayTypeEx, createTupleType, createTupleTypeEx, filterType, getBaseTypeOfLiteralType, getBigIntLiteralType, getElementTypes, getEndElementCount, getIntersectionType, getNumberLiteralType, getRegularTypeOfLiteralType, getStringLiteralType, getTemplateLiteralType, getTypeArguments, getTypeReferenceArity, getUnionType, getUnionTypeEx, getWidenedLiteralType, isArrayOrTupleType, isErrorType, isArrayType, isGenericTupleType, isLiteralType, isMutableArrayOrTuple, isReadonlyArrayType, isTupleType, mapType, newIndexInfo, newObjectType, setStructuredTypeMembers, someType, tupleTargetOf, typesOf, } from './checker-type-factory.js';
8
+ import { getDeclaredTypeOfClassOrInterface, getDeclaredTypeOfSymbol, fillMissingTypeArguments, getConstraintOfTypeParameter, getDefaultFromTypeParameter, getMinTypeArgumentCount, getDeclaredTypeOfTypeParameter, } from './checker-declared-types.js';
9
+ import { applyStringMapping, getActualTypeVariable, getBranchTypesFromConditionalType, getConstraintFromConditionalType, getConstraintOfConditionalType, getConstraintTypeFromMappedType, getElementTypeOfSliceOfTupleType, getFalseTypeFromConditionalType, getHomomorphicTypeVariable, getIndexType, getIndexTypeEx, getIndexedAccessType, getLiteralTypeFromProperty, getMappedTypeModifiers, getNameTypeFromMappedType, getSimplifiedType, getSubstitutionIntersection, getTemplateTypeFromMappedType, getTrueTypeFromConditionalType, getTypeParameterFromMappedType, isGenericMappedType, isNoInferType, isTypeMatchedByTemplateLiteralType, shouldDeferIndexType, } from './checker-type-operators.js';
10
+ import { getApparentType, getApplicableIndexInfo, getBaseConstraintOfType, getBaseConstraintOrType, getConstraintOfType, getIndexInfoOfType, getIndexInfosOfType, getPropertiesOfObjectType, getPropertiesOfType, getPropertyOfType, getPropertyOfUnionOrIntersectionType, getReducedApparentType, getReducedType, getSignaturesOfType, getTypeWithThisArgument, isApplicableIndexType, isObjectLiteralType, } from './checker-members.js';
11
+ import { getEffectiveRestType, getErasedSignature, getParameterCount, getRestTypeAtPosition, getReturnTypeOfSignature, getSignatureInstantiation, getThisTypeOfSignature, getTypeAtPosition, getTypePredicateOfSignature, sliceTupleType, } from './checker-signatures.js';
12
+ import { getNullableType, getTypeOfSymbol, getWidenedType, isReadonlySymbol, removeMissingOrUndefinedType, removeMissingType, } from './checker-symbol-types.js';
13
+ import { combineTypeMappers, couldContainTypeVariables, instantiateSignature, instantiateSignatureEx, instantiateType, instantiateTypes, } from './instantiation.js';
14
+ import { getAliasVariances, getComparer, getUnmatchedProperty, getVariances, isUnmatchedProperty, isDeeplyNestedType, setStackEntry, isObjectTypeWithInferableIndex, isTypeAssignableTo, isTypeIdenticalTo, isTypeStrictSubtypeOf, isTypeSubtypeOf, } from './relations.js';
15
+ import { maybeTypeOfKind } from './checker-utilities.js';
16
+ import { getTypeFromTypeNode } from './checker-type-nodes.js';
17
+ import { getMergedSymbol, getSymbolOfDeclaration } from './checker-symbols.js';
18
+ import { parsePseudoBigInt } from './pseudo-bigint.js';
19
+ import { getContextualTypeForInference } from './checker-contextual.js';
20
+ // Shared empty candidate lists: an InferenceInfo gets its own on its first candidate, as tsgo's nil
21
+ // slices do. Never changed.
22
+ const noCandidates = [];
23
+ const noCandidateDepths = [];
24
+ /** Inferences for one type parameter. */
25
+ export class InferenceInfo {
26
+ typeParameter;
27
+ /** Covariant candidates, in decreasing depth order. */
28
+ candidates = noCandidates;
29
+ candidateDepths = noCandidateDepths;
30
+ contraCandidates = noCandidates;
31
+ inferredType = undefined;
32
+ priority = 2048 /* InferencePriority.MaxValue */;
33
+ /** Whether all inferences are to top-level occurrences. */
34
+ topLevel = true;
35
+ isFixed = false;
36
+ impliedArity = -1;
37
+ constructor(typeParameter) {
38
+ this.typeParameter = typeParameter;
39
+ }
40
+ clone() {
41
+ const info = new InferenceInfo(this.typeParameter);
42
+ if (this.candidates.length !== 0) {
43
+ info.candidates = this.candidates.slice();
44
+ info.candidateDepths = this.candidateDepths.slice();
45
+ }
46
+ if (this.contraCandidates.length !== 0)
47
+ info.contraCandidates = this.contraCandidates.slice();
48
+ info.inferredType = this.inferredType;
49
+ info.priority = this.priority;
50
+ info.topLevel = this.topLevel;
51
+ info.isFixed = this.isFixed;
52
+ info.impliedArity = this.impliedArity;
53
+ return info;
54
+ }
55
+ }
56
+ export class IntraExpressionInferenceSite {
57
+ node;
58
+ type;
59
+ constructor(node, type) {
60
+ this.node = node;
61
+ this.type = type;
62
+ }
63
+ }
64
+ export class InferenceContext {
65
+ inferences;
66
+ /** The generic signature inferences are made for, if any. */
67
+ signature;
68
+ flags;
69
+ compareTypes;
70
+ /** Fixes inferences. */
71
+ mapper;
72
+ nonFixingMapper;
73
+ /** Maps to inferences from the return type, if any. */
74
+ returnMapper = undefined;
75
+ outerReturnMapper = undefined;
76
+ inferredTypeParameters = undefined;
77
+ intraExpressionInferenceSites = [];
78
+ constructor(c, inferences, signature, flags, compareTypes) {
79
+ this.inferences = inferences;
80
+ this.signature = signature;
81
+ this.flags = flags;
82
+ this.compareTypes = compareTypes;
83
+ this.mapper = new InferenceTypeMapper(c, this, true);
84
+ this.nonFixingMapper = new InferenceTypeMapper(c, this, false);
85
+ }
86
+ }
87
+ /** Maps a context's type parameters to their inferred types, optionally fixing them. */
88
+ export class InferenceTypeMapper extends TypeMapper {
89
+ c;
90
+ context;
91
+ fixing;
92
+ constructor(c, context, fixing) {
93
+ super();
94
+ this.c = c;
95
+ this.context = context;
96
+ this.fixing = fixing;
97
+ }
98
+ map(type) {
99
+ const inferences = this.context.inferences;
100
+ for (let i = 0; i < inferences.length; i++) {
101
+ const inference = inferences[i];
102
+ if (type !== inference.typeParameter)
103
+ continue;
104
+ if (this.fixing && !inference.isFixed) {
105
+ // Infer from intra-expression sites before committing to an inference.
106
+ inferFromIntraExpressionSites(this.c, this.context);
107
+ clearCachedInferences(inferences);
108
+ inference.isFixed = true;
109
+ }
110
+ return getInferredType(this.c, this.context, i);
111
+ }
112
+ return type;
113
+ }
114
+ }
115
+ export function createInferenceContext(c, typeParameters, signature, flags, compareTypes) {
116
+ const inferences = [];
117
+ for (const tp of typeParameters)
118
+ inferences.push(new InferenceInfo(tp));
119
+ return new InferenceContext(c, inferences, signature, flags, compareTypes ?? getComparer(c, 2 /* ComparerKind.Assignable */));
120
+ }
121
+ export function cloneInferenceContext(c, context, extraFlags) {
122
+ return new InferenceContext(c, context.inferences.map((i) => i.clone()), context.signature, context.flags | extraFlags, context.compareTypes);
123
+ }
124
+ export function cloneInferredPartOfContext(c, context) {
125
+ const inferences = [];
126
+ for (const i of context.inferences)
127
+ if (hasInferenceCandidates(i))
128
+ inferences.push(i.clone());
129
+ if (inferences.length === 0)
130
+ return undefined;
131
+ return new InferenceContext(c, inferences, context.signature, context.flags, context.compareTypes);
132
+ }
133
+ export function getMapperFromContext(context) {
134
+ return context !== undefined ? context.mapper : undefined;
135
+ }
136
+ /** The context's return mapper combined with a mapper erasing its other type parameters to their current inferences. */
137
+ export function createOuterReturnMapper(c, context) {
138
+ if (context.outerReturnMapper === undefined) {
139
+ let mapper = cloneInferenceContext(c, context, 0 /* InferenceFlags.None */).mapper;
140
+ if (context.returnMapper !== undefined)
141
+ mapper = new MergedTypeMapper(context.returnMapper, mapper);
142
+ context.outerReturnMapper = mapper;
143
+ }
144
+ return context.outerReturnMapper;
145
+ }
146
+ export function addIntraExpressionInferenceSite(context, node, type) {
147
+ context.intraExpressionInferenceSites.push(new IntraExpressionInferenceSite(node, type));
148
+ }
149
+ export function clearCachedInferences(inferences) {
150
+ for (const inference of inferences)
151
+ if (!inference.isFixed)
152
+ inference.inferredType = undefined;
153
+ }
154
+ export function hasInferenceCandidates(info) {
155
+ return info.candidates.length !== 0 || info.contraCandidates.length !== 0;
156
+ }
157
+ export function hasInferenceCandidatesOrDefault(info) {
158
+ return hasInferenceCandidates(info) || hasTypeParameterDefault(info.typeParameter);
159
+ }
160
+ export function someInferenceHasCandidates(inferences) {
161
+ for (const info of inferences)
162
+ if (hasInferenceCandidates(info))
163
+ return true;
164
+ return false;
165
+ }
166
+ export function everyInferenceHasCandidates(inferences) {
167
+ for (const info of inferences)
168
+ if (!hasInferenceCandidates(info))
169
+ return false;
170
+ return true;
171
+ }
172
+ export function someInferenceHasCandidatesOrDefault(inferences) {
173
+ for (const info of inferences)
174
+ if (hasInferenceCandidatesOrDefault(info))
175
+ return true;
176
+ return false;
177
+ }
178
+ function hasTypeParameterDefault(tp) {
179
+ const symbol = tp.symbol;
180
+ if (symbol === undefined)
181
+ return false;
182
+ for (const d of symbol.declarations)
183
+ if (d.kind === 169 /* SyntaxKind.TypeParameter */ && d.defaultType !== undefined)
184
+ return true;
185
+ return false;
186
+ }
187
+ export function hasOverlappingInferences(a, b) {
188
+ for (let i = 0; i < a.length; i++)
189
+ if (hasInferenceCandidates(a[i]) && hasInferenceCandidates(b[i]))
190
+ return true;
191
+ return false;
192
+ }
193
+ export function mergeInferences(target, source) {
194
+ for (let i = 0; i < target.length; i++)
195
+ if (!hasInferenceCandidates(target[i]) && hasInferenceCandidates(source[i]))
196
+ target[i] = source[i];
197
+ }
198
+ // ---- Inferring from types
199
+ /** Infers from `source` to `target` for the type parameters of `inferences`. */
200
+ export function inferTypes(c, inferences, source, target, priority, contravariant) {
201
+ const state = new InferenceState(c, inferences, target, priority, contravariant);
202
+ state.inferFromTypes(source, target);
203
+ }
204
+ class MatchedTypes {
205
+ sources;
206
+ targets;
207
+ constructor(sources, targets) {
208
+ this.sources = sources;
209
+ this.targets = targets;
210
+ }
211
+ }
212
+ function typesMatch(c, s, t, kind) {
213
+ switch (kind) {
214
+ case 0 /* MatchKind.TypeOrBaseIdentical */:
215
+ return isTypeOrBaseIdenticalTo(c, s, t);
216
+ case 1 /* MatchKind.CloselyMatched */:
217
+ return isTypeCloselyMatchedBy(s, t);
218
+ case 2 /* MatchKind.Identical */:
219
+ return isTypeIdenticalTo(c, s, t);
220
+ }
221
+ }
222
+ /** The state of one `inferTypes` walk. */
223
+ class InferenceState {
224
+ c;
225
+ inferences;
226
+ originalTarget;
227
+ priority;
228
+ inferencePriority = 2048 /* InferencePriority.MaxValue */;
229
+ contravariant;
230
+ bivariant = false;
231
+ expandingFlags = 0 /* ExpandingFlags.None */;
232
+ propagationType = undefined;
233
+ /** invokeOnce's pairs by packed type ids (`visitKey`), or spelled when an id is too large to pack. */
234
+ visited = undefined;
235
+ visitedSpelled = undefined;
236
+ /** invokeOnce's recursion stacks; the first `stackDepth` entries of each are live. */
237
+ sourceStack = [];
238
+ targetStack = [];
239
+ stackDepth = 0;
240
+ depth = 0;
241
+ constructor(c, inferences, originalTarget, priority, contravariant) {
242
+ this.c = c;
243
+ this.inferences = inferences;
244
+ this.originalTarget = originalTarget;
245
+ this.priority = priority;
246
+ this.contravariant = contravariant;
247
+ }
248
+ infoFor(t) {
249
+ return getInferenceInfoForType(this.inferences, t);
250
+ }
251
+ inferFromTypes(source, target) {
252
+ const c = this.c;
253
+ if (!couldContainTypeVariables(c, target) || isNoInferType(c, target))
254
+ return;
255
+ if (source === c.wildcardType || source === c.blockedStringType) {
256
+ // Infer the `any`-like source for every type parameter referenced in the target.
257
+ const savePropagationType = this.propagationType;
258
+ this.propagationType = source;
259
+ this.inferFromTypes(target, target);
260
+ this.propagationType = savePropagationType;
261
+ return;
262
+ }
263
+ const sourceAlias = source.alias;
264
+ const targetAlias = target.alias;
265
+ if (sourceAlias !== undefined && targetAlias !== undefined && sourceAlias.symbol === targetAlias.symbol) {
266
+ if (sourceAlias.typeArguments.length !== 0 || targetAlias.typeArguments.length !== 0) {
267
+ // Instantiations of the same alias: infer between the type arguments, with defaults applied.
268
+ const params = c.symbolLinks(sourceAlias.symbol).typeParameters ?? [];
269
+ const minParams = getMinTypeArgumentCount(c, params);
270
+ const sourceTypes = fillMissingTypeArguments(c, sourceAlias.typeArguments, params, minParams, false);
271
+ const targetTypes = fillMissingTypeArguments(c, targetAlias.typeArguments, params, minParams, false);
272
+ this.inferFromTypeArguments(sourceTypes, targetTypes, someTypeCouldHoldInference(c, targetTypes) ? getAliasVariances(c, sourceAlias.symbol) : noVariances);
273
+ }
274
+ return;
275
+ }
276
+ if (source === target && (source.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
277
+ for (const t of typesOf(source))
278
+ this.inferFromTypes(t, t);
279
+ return;
280
+ }
281
+ if ((target.flags & 134217728 /* TypeFlags.Union */) !== 0) {
282
+ const sourceTypes = (source.flags & 134217728 /* TypeFlags.Union */) !== 0 ? typesOf(source) : [source];
283
+ // Infer between identically matching constituents, then closely matching ones, removing them.
284
+ const temp = this.inferFromMatchingTypes(sourceTypes, distributedTypes(target), 0 /* MatchKind.TypeOrBaseIdentical */);
285
+ const matched = this.inferFromMatchingTypes(temp.sources, temp.targets, 1 /* MatchKind.CloselyMatched */);
286
+ if (matched.targets.length === 0)
287
+ return;
288
+ target = getUnionType(c, matched.targets);
289
+ if (matched.sources.length === 0) {
290
+ // Everything matched; infer from the full source at lower priority so that `string` to
291
+ // `string | T` still infers `string` for T.
292
+ this.inferWithPriority(source, target, 1 /* InferencePriority.NakedTypeVariable */);
293
+ return;
294
+ }
295
+ source = getUnionType(c, matched.sources);
296
+ }
297
+ else if ((target.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && !everyNonGenericObjectType(c, typesOf(target))) {
298
+ // Reduce intersections unless they're simple combinations of object types.
299
+ if ((source.flags & 134217728 /* TypeFlags.Union */) === 0) {
300
+ const sourceTypes = (source.flags & 268435456 /* TypeFlags.Intersection */) !== 0 ? typesOf(source) : [source];
301
+ const matched = this.inferFromMatchingTypes(sourceTypes, typesOf(target), 2 /* MatchKind.Identical */);
302
+ if (matched.sources.length === 0 || matched.targets.length === 0)
303
+ return;
304
+ source = getIntersectionType(c, matched.sources);
305
+ target = getIntersectionType(c, matched.targets);
306
+ }
307
+ }
308
+ if ((target.flags & (33554432 /* TypeFlags.IndexedAccess */ | 16777216 /* TypeFlags.Substitution */)) !== 0) {
309
+ if (isNoInferType(c, target))
310
+ return;
311
+ target = getActualTypeVariable(c, target);
312
+ }
313
+ if ((target.flags & 34078720 /* TypeFlags.TypeVariable */) !== 0) {
314
+ const inference = this.infoFor(target);
315
+ if (inference !== undefined) {
316
+ this.inferToTypeVariable(inference, source, target);
317
+ return;
318
+ }
319
+ // Infer to the simplified indexed access to expose more bare type parameters.
320
+ const simplified = getSimplifiedType(c, target, false);
321
+ if (simplified !== target)
322
+ this.inferFromTypes(source, simplified);
323
+ else if ((target.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0) {
324
+ const access = target;
325
+ const indexType = getSimplifiedType(c, access.indexType, false);
326
+ if ((indexType.flags & 132644864 /* TypeFlags.Instantiable */) !== 0) {
327
+ const distributed = distributeIndexOverObjectType(c, getSimplifiedType(c, access.objectType, false), indexType);
328
+ if (distributed !== undefined && distributed !== target)
329
+ this.inferFromTypes(source, distributed);
330
+ }
331
+ }
332
+ }
333
+ if ((source.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 &&
334
+ (target.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 &&
335
+ (referenceTarget(source) === referenceTarget(target) || (isArrayType(c, source) && isArrayType(c, target))) &&
336
+ !(referenceNode(source) !== undefined && referenceNode(target) !== undefined)) {
337
+ this.inferFromReferenceTypeArguments(source, target);
338
+ }
339
+ else if ((source.flags & 2097152 /* TypeFlags.Index */) !== 0 && (target.flags & 2097152 /* TypeFlags.Index */) !== 0) {
340
+ this.inferFromContravariantTypes(source.target, target.target);
341
+ }
342
+ else if ((isLiteralType(source) || (source.flags & 32 /* TypeFlags.String */) !== 0) &&
343
+ (target.flags & 2097152 /* TypeFlags.Index */) !== 0) {
344
+ const empty = createEmptyObjectTypeFromStringLiteral(c, source);
345
+ this.inferFromContravariantTypesWithPriority(empty, target.target, 256 /* InferencePriority.LiteralKeyof */);
346
+ }
347
+ else if ((source.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0 && (target.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0) {
348
+ this.inferFromTypes(source.objectType, target.objectType);
349
+ this.inferFromTypes(source.indexType, target.indexType);
350
+ }
351
+ else if ((source.flags & 8388608 /* TypeFlags.StringMapping */) !== 0 && (target.flags & 8388608 /* TypeFlags.StringMapping */) !== 0) {
352
+ if (source.symbol === target.symbol)
353
+ this.inferFromTypes(source.target, target.target);
354
+ }
355
+ else if ((source.flags & 16777216 /* TypeFlags.Substitution */) !== 0) {
356
+ this.inferFromTypes(source.baseType, target);
357
+ // Substitutes infer at a lower priority.
358
+ this.inferWithPriority(getSubstitutionIntersection(c, source), target, 4 /* InferencePriority.SubstituteSource */);
359
+ }
360
+ else if ((target.flags & 67108864 /* TypeFlags.Conditional */) !== 0) {
361
+ this.invokeOnce(source, target, 0 /* InferenceAction.ConditionalType */);
362
+ }
363
+ else if ((target.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
364
+ this.inferToMultipleTypes(source, typesOf(target), target.flags);
365
+ }
366
+ else if ((source.flags & 134217728 /* TypeFlags.Union */) !== 0) {
367
+ for (const sourceType of typesOf(source))
368
+ this.inferFromTypes(sourceType, target);
369
+ }
370
+ else if ((target.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
371
+ this.inferToTemplateLiteralType(source, target);
372
+ }
373
+ else {
374
+ source = getReducedType(c, source);
375
+ if (isGenericMappedType(c, source) && isGenericMappedType(c, target))
376
+ this.invokeOnce(source, target, 1 /* InferenceAction.GenericMappedTypes */);
377
+ if (!((this.priority & 512 /* InferencePriority.NoConstraints */) !== 0 &&
378
+ (source.flags & (268435456 /* TypeFlags.Intersection */ | 132644864 /* TypeFlags.Instantiable */)) !== 0)) {
379
+ const apparentSource = getApparentType(c, source);
380
+ // The apparent type of an indexed access or conditional may be any type; restart with it.
381
+ if (apparentSource !== source && (apparentSource.flags & (1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */)) === 0) {
382
+ this.inferFromTypes(apparentSource, target);
383
+ return;
384
+ }
385
+ source = apparentSource;
386
+ }
387
+ if ((source.flags & (1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */)) !== 0)
388
+ this.invokeOnce(source, target, 2 /* InferenceAction.ObjectTypes */);
389
+ }
390
+ }
391
+ inferToTypeVariable(inference, source, target) {
392
+ const c = this.c;
393
+ // Non-inferrable sources are markers that must not become candidates.
394
+ if ((source.objectFlags & 262144 /* ObjectFlags.NonInferrableType */) !== 0 || source === c.nonInferrableAnyType)
395
+ return;
396
+ if (!inference.isFixed) {
397
+ const candidate = this.propagationType ?? source;
398
+ if (candidate === c.blockedStringType)
399
+ return;
400
+ if (this.priority < inference.priority) {
401
+ inference.candidates = noCandidates;
402
+ inference.candidateDepths = noCandidateDepths;
403
+ inference.contraCandidates = noCandidates;
404
+ inference.topLevel = true;
405
+ inference.priority = this.priority;
406
+ }
407
+ if (this.priority === inference.priority) {
408
+ // Contravariant inferences only in purely contravariant positions.
409
+ if (this.contravariant && !this.bivariant) {
410
+ if (!inference.contraCandidates.includes(candidate)) {
411
+ if (inference.contraCandidates === noCandidates)
412
+ inference.contraCandidates = [];
413
+ inference.contraCandidates.push(candidate);
414
+ clearCachedInferences(this.inferences);
415
+ }
416
+ }
417
+ else {
418
+ let index = inference.candidates.indexOf(candidate);
419
+ if (index < 0 || inference.candidateDepths[index] < this.depth) {
420
+ if (index >= 0) {
421
+ inference.candidates.splice(index, 1);
422
+ inference.candidateDepths.splice(index, 1);
423
+ }
424
+ if (inference.candidates === noCandidates) {
425
+ inference.candidates = [];
426
+ inference.candidateDepths = [];
427
+ }
428
+ // Keep candidates with the highest depth first.
429
+ index = 0;
430
+ while (index < inference.candidateDepths.length && inference.candidateDepths[index] >= this.depth)
431
+ index++;
432
+ inference.candidates.splice(index, 0, candidate);
433
+ inference.candidateDepths.splice(index, 0, this.depth);
434
+ clearCachedInferences(this.inferences);
435
+ }
436
+ }
437
+ }
438
+ if ((this.priority & 128 /* InferencePriority.ReturnType */) === 0 &&
439
+ (target.flags & 524288 /* TypeFlags.TypeParameter */) !== 0 &&
440
+ inference.topLevel &&
441
+ !isTypeParameterAtTopLevel(c, this.originalTarget, target, 0)) {
442
+ inference.topLevel = false;
443
+ clearCachedInferences(this.inferences);
444
+ }
445
+ }
446
+ this.inferencePriority = Math.min(this.inferencePriority, this.priority);
447
+ }
448
+ /**
449
+ * Infers between the type arguments of two references to one generic type. The variances only pick
450
+ * the direction of each inference, so they are measured only when a target type argument could hold
451
+ * one: measuring them relates marker instantiations of the whole type.
452
+ */
453
+ inferFromReferenceTypeArguments(source, target) {
454
+ const c = this.c;
455
+ const sourceTypes = getTypeArguments(c, source);
456
+ const targetTypes = getTypeArguments(c, target);
457
+ this.inferFromTypeArguments(sourceTypes, targetTypes, someTypeCouldHoldInference(c, targetTypes) ? getVariances(c, referenceTarget(source)) : noVariances);
458
+ }
459
+ inferFromTypeArguments(sourceTypes, targetTypes, variances) {
460
+ this.depth++;
461
+ const count = Math.min(sourceTypes.length, targetTypes.length);
462
+ for (let i = 0; i < count; i++) {
463
+ if (i < variances.length && (variances[i] & 7 /* VarianceFlags.VarianceMask */) === 2 /* VarianceFlags.Contravariant */)
464
+ this.inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
465
+ else
466
+ this.inferFromTypes(sourceTypes[i], targetTypes[i]);
467
+ }
468
+ this.depth--;
469
+ }
470
+ inferWithPriority(source, target, newPriority) {
471
+ const savePriority = this.priority;
472
+ this.priority |= newPriority;
473
+ this.inferFromTypes(source, target);
474
+ this.priority = savePriority;
475
+ }
476
+ inferFromContravariantTypesWithPriority(source, target, newPriority) {
477
+ const savePriority = this.priority;
478
+ this.priority |= newPriority;
479
+ this.inferFromContravariantTypes(source, target);
480
+ this.priority = savePriority;
481
+ }
482
+ inferFromContravariantTypes(source, target) {
483
+ this.contravariant = !this.contravariant;
484
+ this.inferFromTypes(source, target);
485
+ this.contravariant = !this.contravariant;
486
+ }
487
+ inferFromContravariantTypesIfStrictFunctionTypes(source, target) {
488
+ if (this.c.strictFunctionTypes || (this.priority & 1024 /* InferencePriority.AlwaysStrict */) !== 0)
489
+ this.inferFromContravariantTypes(source, target);
490
+ else
491
+ this.inferFromTypes(source, target);
492
+ }
493
+ /** Runs `action` once per source and target pair, stopping at circularities. */
494
+ invokeOnce(source, target, action) {
495
+ const key = visitKey(source, target);
496
+ const status = key >= 0 ? this.visited?.get(key) : this.visitedSpelled?.get(source.id + ',' + target.id);
497
+ if (status !== undefined) {
498
+ this.inferencePriority = Math.min(this.inferencePriority, status);
499
+ return;
500
+ }
501
+ this.setVisited(source, target, key, -1 /* InferencePriority.Circularity */);
502
+ const saveInferencePriority = this.inferencePriority;
503
+ this.inferencePriority = 2048 /* InferencePriority.MaxValue */;
504
+ // Duplicate recursion identities on both sides mean a circularity.
505
+ const saveExpandingFlags = this.expandingFlags;
506
+ const stackDepth = this.stackDepth;
507
+ setStackEntry(this.sourceStack, stackDepth, source);
508
+ setStackEntry(this.targetStack, stackDepth, target);
509
+ this.stackDepth = stackDepth + 1;
510
+ if (isDeeplyNestedType(this.c, source, this.sourceStack, stackDepth + 1, 2))
511
+ this.expandingFlags |= 1 /* ExpandingFlags.Source */;
512
+ if (isDeeplyNestedType(this.c, target, this.targetStack, stackDepth + 1, 2))
513
+ this.expandingFlags |= 2 /* ExpandingFlags.Target */;
514
+ if (this.expandingFlags !== 3 /* ExpandingFlags.Both */) {
515
+ switch (action) {
516
+ case 0 /* InferenceAction.ConditionalType */:
517
+ this.inferToConditionalType(source, target);
518
+ break;
519
+ case 1 /* InferenceAction.GenericMappedTypes */:
520
+ this.inferFromGenericMappedTypes(source, target);
521
+ break;
522
+ case 2 /* InferenceAction.ObjectTypes */:
523
+ this.inferFromObjectTypes(source, target);
524
+ break;
525
+ }
526
+ }
527
+ else
528
+ this.inferencePriority = -1 /* InferencePriority.Circularity */;
529
+ this.stackDepth = stackDepth;
530
+ this.expandingFlags = saveExpandingFlags;
531
+ this.setVisited(source, target, key, this.inferencePriority);
532
+ this.inferencePriority = Math.min(this.inferencePriority, saveInferencePriority);
533
+ }
534
+ setVisited(source, target, key, status) {
535
+ if (key >= 0) {
536
+ let visited = this.visited;
537
+ if (visited === undefined) {
538
+ visited = new Map();
539
+ this.visited = visited;
540
+ }
541
+ visited.set(key, status);
542
+ }
543
+ else {
544
+ let visited = this.visitedSpelled;
545
+ if (visited === undefined) {
546
+ visited = new Map();
547
+ this.visitedSpelled = visited;
548
+ }
549
+ visited.set(source.id + ',' + target.id, status);
550
+ }
551
+ }
552
+ inferFromMatchingTypes(sources, targets, kind) {
553
+ const c = this.c;
554
+ const matchedSources = [];
555
+ const matchedTargets = [];
556
+ for (const t of targets)
557
+ for (const s of sources)
558
+ if (typesMatch(c, s, t, kind)) {
559
+ this.inferFromTypes(s, t);
560
+ if (!matchedSources.includes(s))
561
+ matchedSources.push(s);
562
+ if (!matchedTargets.includes(t))
563
+ matchedTargets.push(t);
564
+ }
565
+ return new MatchedTypes(matchedSources.length !== 0 ? sources.filter((t) => !matchedSources.includes(t)) : sources, matchedTargets.length !== 0 ? targets.filter((t) => !matchedTargets.includes(t)) : targets);
566
+ }
567
+ inferToMultipleTypes(source, targets, targetFlags) {
568
+ let typeVariableCount = 0;
569
+ if ((targetFlags & 134217728 /* TypeFlags.Union */) !== 0) {
570
+ let nakedTypeVariable = undefined;
571
+ const sources = (source.flags & 134217728 /* TypeFlags.Union */) !== 0 ? typesOf(source) : [source];
572
+ const matched = [];
573
+ for (let i = 0; i < sources.length; i++)
574
+ matched.push(false);
575
+ let inferenceCircularity = false;
576
+ // Infer to targets that aren't naked type variables first, tracking which sources produced
577
+ // inferences of the same quality as a naked type variable would get.
578
+ for (const t of targets) {
579
+ if (this.infoFor(t) !== undefined) {
580
+ nakedTypeVariable = t;
581
+ typeVariableCount++;
582
+ }
583
+ else
584
+ for (let i = 0; i < sources.length; i++) {
585
+ const saveInferencePriority = this.inferencePriority;
586
+ this.inferencePriority = 2048 /* InferencePriority.MaxValue */;
587
+ this.inferFromTypes(sources[i], t);
588
+ if (this.inferencePriority === this.priority)
589
+ matched[i] = true;
590
+ inferenceCircularity = inferenceCircularity || this.inferencePriority === -1 /* InferencePriority.Circularity */;
591
+ this.inferencePriority = Math.min(this.inferencePriority, saveInferencePriority);
592
+ }
593
+ }
594
+ if (typeVariableCount === 0) {
595
+ // Infer `A | B` for T from `A | B` to `T & (X | Y)`.
596
+ const intersectionTypeVariable = this.getSingleTypeVariableFromIntersectionTypes(targets);
597
+ if (intersectionTypeVariable !== undefined)
598
+ this.inferWithPriority(source, intersectionTypeVariable, 1 /* InferencePriority.NakedTypeVariable */);
599
+ return;
600
+ }
601
+ // With a single naked type variable and no circularities, infer the unmatched sources to it.
602
+ if (typeVariableCount === 1 && !inferenceCircularity) {
603
+ const unmatched = [];
604
+ for (let i = 0; i < sources.length; i++)
605
+ if (!matched[i])
606
+ unmatched.push(sources[i]);
607
+ if (unmatched.length !== 0) {
608
+ this.inferFromTypes(getUnionType(this.c, unmatched), nakedTypeVariable);
609
+ return;
610
+ }
611
+ }
612
+ }
613
+ else {
614
+ // Inferences from nested naked type variables come first among the candidates.
615
+ for (const t of targets) {
616
+ if (this.infoFor(t) !== undefined)
617
+ typeVariableCount++;
618
+ else
619
+ this.inferFromTypes(source, t);
620
+ }
621
+ }
622
+ // Inferences directly to naked type variables get lower priority.
623
+ if (((targetFlags & 268435456 /* TypeFlags.Intersection */) !== 0 && typeVariableCount === 1) ||
624
+ ((targetFlags & 268435456 /* TypeFlags.Intersection */) === 0 && typeVariableCount > 0))
625
+ for (const t of targets)
626
+ if (this.infoFor(t) !== undefined)
627
+ this.inferWithPriority(source, t, 1 /* InferencePriority.NakedTypeVariable */);
628
+ }
629
+ getSingleTypeVariableFromIntersectionTypes(types) {
630
+ let typeVariable = undefined;
631
+ for (const t of types) {
632
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) === 0)
633
+ return undefined;
634
+ let v;
635
+ for (const u of typesOf(t)) {
636
+ if (this.infoFor(u) !== undefined) {
637
+ v = u;
638
+ break;
639
+ }
640
+ }
641
+ if (v === undefined || (typeVariable !== undefined && v !== typeVariable))
642
+ return undefined;
643
+ typeVariable = v;
644
+ }
645
+ return typeVariable;
646
+ }
647
+ inferToConditionalType(source, target) {
648
+ const c = this.c;
649
+ if ((source.flags & 67108864 /* TypeFlags.Conditional */) !== 0) {
650
+ const s = source;
651
+ this.inferFromTypes(s.checkType, target.checkType);
652
+ this.inferFromTypes(s.extendsType, target.extendsType);
653
+ this.inferFromTypes(getTrueTypeFromConditionalType(c, s), getTrueTypeFromConditionalType(c, target));
654
+ this.inferFromTypes(getFalseTypeFromConditionalType(c, s), getFalseTypeFromConditionalType(c, target));
655
+ }
656
+ else {
657
+ const savePriority = this.priority;
658
+ this.priority |= this.contravariant ? 64 /* InferencePriority.ContravariantConditional */ : 0;
659
+ this.inferToMultipleTypes(source, getBranchTypesFromConditionalType(c, target), target.flags);
660
+ this.priority = savePriority;
661
+ }
662
+ }
663
+ inferToTemplateLiteralType(source, target) {
664
+ const c = this.c;
665
+ const matches = inferTypesFromTemplateLiteralType(c, source, target);
666
+ const types = target.types;
667
+ // When the target has only placeholders and nothing matches, infer `never` for each so that
668
+ // instantiation produces `never` rather than collapsing to the constraint.
669
+ if (matches === undefined && !target.texts.every((s) => s === ''))
670
+ return;
671
+ for (let i = 0; i < types.length; i++) {
672
+ const placeholder = types[i];
673
+ const matchSource = matches !== undefined ? matches[i] : c.neverType;
674
+ if ((matchSource.flags & 1024 /* TypeFlags.StringLiteral */) !== 0 && (placeholder.flags & 34078720 /* TypeFlags.TypeVariable */) !== 0) {
675
+ const info = this.infoFor(placeholder);
676
+ if (info !== undefined) {
677
+ const constraint = getBaseConstraintOfType(c, info.typeParameter);
678
+ if (constraint !== undefined && (constraint.flags & 1 /* TypeFlags.Any */) === 0) {
679
+ const matching = getTemplatePlaceholderInference(c, matchSource, constraint);
680
+ if (matching !== undefined) {
681
+ this.inferFromTypes(matching, placeholder);
682
+ continue;
683
+ }
684
+ }
685
+ }
686
+ }
687
+ this.inferFromTypes(matchSource, placeholder);
688
+ }
689
+ }
690
+ inferFromGenericMappedTypes(source, target) {
691
+ // From `{ [P in S]: X }` to `{ [P in T]: Y }`, infer from S to T and from X to Y.
692
+ const c = this.c;
693
+ this.inferFromTypes(getConstraintTypeFromMappedType(c, source), getConstraintTypeFromMappedType(c, target));
694
+ this.inferFromTypes(getTemplateTypeFromMappedType(c, source), getTemplateTypeFromMappedType(c, target));
695
+ const sourceNameType = getNameTypeFromMappedType(c, source);
696
+ const targetNameType = getNameTypeFromMappedType(c, target);
697
+ if (sourceNameType !== undefined && targetNameType !== undefined)
698
+ this.inferFromTypes(sourceNameType, targetNameType);
699
+ }
700
+ inferFromObjectTypes(source, target) {
701
+ const c = this.c;
702
+ if ((source.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 &&
703
+ (target.objectFlags & 4 /* ObjectFlags.Reference */) !== 0 &&
704
+ (referenceTarget(source) === referenceTarget(target) || (isArrayType(c, source) && isArrayType(c, target)))) {
705
+ this.inferFromReferenceTypeArguments(source, target);
706
+ return;
707
+ }
708
+ if (isGenericMappedType(c, source) && isGenericMappedType(c, target))
709
+ this.inferFromGenericMappedTypes(source, target);
710
+ if ((target.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0 && !hasMappedNameType(target)) {
711
+ const constraintType = getConstraintTypeFromMappedType(c, target);
712
+ if (this.inferToMappedType(source, target, constraintType))
713
+ return;
714
+ }
715
+ // Infer from members only when the types are possibly related.
716
+ if (typesDefinitelyUnrelated(c, source, target))
717
+ return;
718
+ if (isArrayOrTupleType(c, source)) {
719
+ if (isTupleType(target)) {
720
+ this.inferFromTupleTypes(source, target);
721
+ return;
722
+ }
723
+ if (isArrayType(c, target)) {
724
+ this.inferFromIndexTypes(source, target);
725
+ return;
726
+ }
727
+ }
728
+ this.inferFromProperties(source, target);
729
+ this.inferFromSignatures(source, target, 0 /* SignatureKind.Call */);
730
+ this.inferFromSignatures(source, target, 1 /* SignatureKind.Construct */);
731
+ this.inferFromIndexTypes(source, target);
732
+ }
733
+ inferFromTupleTypes(source, target) {
734
+ const c = this.c;
735
+ const sourceArity = getTypeReferenceArity(source);
736
+ const targetArity = getTypeReferenceArity(target);
737
+ const elementTypes = getTypeArguments(c, target);
738
+ const targetTuple = tupleTargetOf(target);
739
+ const elementInfos = targetTuple.elementInfos;
740
+ const sourceTypes = getTypeArguments(c, source);
741
+ // Tuples of the same structure: infer between the element types.
742
+ if (isTupleType(source) && isTupleTypeStructureMatching(source, target)) {
743
+ for (let i = 0; i < targetArity; i++)
744
+ this.inferFromTypes(sourceTypes[i], elementTypes[i]);
745
+ return;
746
+ }
747
+ let startLength = 0;
748
+ let endLength = 0;
749
+ if (isTupleType(source)) {
750
+ const sourceTuple = tupleTargetOf(source);
751
+ startLength = Math.min(sourceTuple.fixedLength, targetTuple.fixedLength);
752
+ if ((targetTuple.combinedFlags & 12 /* ElementFlags.Variable */) !== 0)
753
+ endLength = Math.min(getEndElementCount(sourceTuple, 3 /* ElementFlags.Fixed */), getEndElementCount(targetTuple, 3 /* ElementFlags.Fixed */));
754
+ }
755
+ for (let i = 0; i < startLength; i++)
756
+ this.inferFromTypes(sourceTypes[i], elementTypes[i]);
757
+ if (!isTupleType(source) ||
758
+ (sourceArity - startLength - endLength === 1 &&
759
+ (tupleTargetOf(source).elementInfos[startLength].flags & 4 /* ElementFlags.Rest */) !== 0)) {
760
+ // A single rest element remains in the source; infer from it to every middle target element.
761
+ const restType = sourceTypes[startLength];
762
+ for (let i = startLength; i < targetArity - endLength; i++)
763
+ this.inferFromTypes((elementInfos[i].flags & 8 /* ElementFlags.Variadic */) !== 0 ? createArrayType(c, restType) : restType, elementTypes[i]);
764
+ }
765
+ else {
766
+ const middleLength = targetArity - startLength - endLength;
767
+ if (middleLength === 2) {
768
+ const first = elementInfos[startLength].flags;
769
+ const second = elementInfos[startLength + 1].flags;
770
+ if ((first & second & 8 /* ElementFlags.Variadic */) !== 0) {
771
+ // [...T, ...U]: slice the source by the implied arity of T.
772
+ const targetInfo = this.infoFor(elementTypes[startLength]);
773
+ if (targetInfo !== undefined && targetInfo.impliedArity >= 0) {
774
+ this.inferFromTypes(sliceTupleType(c, source, startLength, endLength + sourceArity - targetInfo.impliedArity), elementTypes[startLength]);
775
+ this.inferFromTypes(sliceTupleType(c, source, startLength + targetInfo.impliedArity, endLength), elementTypes[startLength + 1]);
776
+ }
777
+ }
778
+ else if ((first & 8 /* ElementFlags.Variadic */) !== 0 && (second & 4 /* ElementFlags.Rest */) !== 0) {
779
+ // [...T, ...rest]: use the arity of a fixed-size tuple constraint of T.
780
+ const info = this.infoFor(elementTypes[startLength]);
781
+ if (info !== undefined) {
782
+ const constraint = getBaseConstraintOfType(c, info.typeParameter);
783
+ if (constraint !== undefined &&
784
+ isTupleType(constraint) &&
785
+ (tupleTargetOf(constraint).combinedFlags & 12 /* ElementFlags.Variable */) === 0) {
786
+ const impliedArity = tupleTargetOf(constraint).fixedLength;
787
+ this.inferFromTypes(sliceTupleType(c, source, startLength, sourceArity - (startLength + impliedArity)), elementTypes[startLength]);
788
+ const restType = getElementTypeOfSliceOfTupleType(c, source, startLength + impliedArity, endLength, false, false);
789
+ if (restType !== undefined)
790
+ this.inferFromTypes(restType, elementTypes[startLength + 1]);
791
+ }
792
+ }
793
+ }
794
+ else if ((first & 4 /* ElementFlags.Rest */) !== 0 && (second & 8 /* ElementFlags.Variadic */) !== 0) {
795
+ // [...rest, ...T]: use the arity of a fixed-size tuple constraint of T.
796
+ const info = this.infoFor(elementTypes[startLength + 1]);
797
+ if (info !== undefined) {
798
+ const constraint = getBaseConstraintOfType(c, info.typeParameter);
799
+ if (constraint !== undefined &&
800
+ isTupleType(constraint) &&
801
+ (tupleTargetOf(constraint).combinedFlags & 12 /* ElementFlags.Variable */) === 0) {
802
+ const impliedArity = tupleTargetOf(constraint).fixedLength;
803
+ const endIndex = sourceArity - getEndElementCount(targetTuple, 3 /* ElementFlags.Fixed */);
804
+ const startIndex = endIndex - impliedArity;
805
+ if (startIndex >= startLength) {
806
+ const trailingSlice = createTupleTypeEx(c, sourceTypes.slice(startIndex, endIndex), tupleTargetOf(source).elementInfos.slice(startIndex, endIndex), false);
807
+ const restType = getElementTypeOfSliceOfTupleType(c, source, startLength, endLength + impliedArity, false, false);
808
+ if (restType !== undefined)
809
+ this.inferFromTypes(restType, elementTypes[startLength]);
810
+ this.inferFromTypes(trailingSlice, elementTypes[startLength + 1]);
811
+ }
812
+ }
813
+ }
814
+ }
815
+ }
816
+ else if (middleLength === 1 && (elementInfos[startLength].flags & 8 /* ElementFlags.Variadic */) !== 0) {
817
+ // One variadic middle element: infer the source slice between the fixed parts, speculatively
818
+ // when the target ends in optional elements.
819
+ const priority = (elementInfos[targetArity - 1].flags & 2 /* ElementFlags.Optional */) !== 0
820
+ ? 2 /* InferencePriority.SpeculativeTuple */
821
+ : 0 /* InferencePriority.None */;
822
+ this.inferWithPriority(sliceTupleType(c, source, startLength, endLength), elementTypes[startLength], priority);
823
+ }
824
+ else if (middleLength === 1 && (elementInfos[startLength].flags & 4 /* ElementFlags.Rest */) !== 0) {
825
+ // One rest middle element: infer the union of the middle source elements.
826
+ const restType = getElementTypeOfSliceOfTupleType(c, source, startLength, endLength, false, false);
827
+ if (restType !== undefined)
828
+ this.inferFromTypes(restType, elementTypes[startLength]);
829
+ }
830
+ }
831
+ for (let i = 0; i < endLength; i++)
832
+ this.inferFromTypes(sourceTypes[sourceArity - i - 1], elementTypes[targetArity - i - 1]);
833
+ }
834
+ inferFromProperties(source, target) {
835
+ const c = this.c;
836
+ for (const targetProp of getPropertiesOfObjectType(c, target)) {
837
+ const sourceProp = getPropertyOfType(c, source, targetProp.name);
838
+ if (sourceProp !== undefined)
839
+ this.inferFromTypes(removeMissingType(c, getTypeOfSymbol(c, sourceProp), (sourceProp.flags & 16777216 /* SymbolFlags.Optional */) !== 0), removeMissingType(c, getTypeOfSymbol(c, targetProp), (targetProp.flags & 16777216 /* SymbolFlags.Optional */) !== 0));
840
+ }
841
+ }
842
+ inferFromSignatures(source, target, kind) {
843
+ const c = this.c;
844
+ const sourceSignatures = getSignaturesOfType(c, source, kind);
845
+ const sourceLen = sourceSignatures.length;
846
+ if (sourceLen === 0)
847
+ return;
848
+ // Match signatures from the bottom up; excess targets infer from the first source signature.
849
+ const targetSignatures = getSignaturesOfType(c, target, kind);
850
+ const targetLen = targetSignatures.length;
851
+ for (let i = 0; i < targetLen; i++) {
852
+ const sourceIndex = Math.max(sourceLen - targetLen + i, 0);
853
+ this.inferFromSignature(getBaseSignature(c, sourceSignatures[sourceIndex]), getErasedSignature(c, targetSignatures[i]));
854
+ }
855
+ }
856
+ inferFromSignature(source, target) {
857
+ const c = this.c;
858
+ if ((source.flags & 128 /* SignatureFlags.IsNonInferrable */) === 0) {
859
+ const saveBivariant = this.bivariant;
860
+ const kind = target.declaration !== undefined ? target.declaration.kind : 0 /* SyntaxKind.Unknown */;
861
+ // Once in a bivariant signature, nested inferences stay bivariant.
862
+ this.bivariant =
863
+ this.bivariant ||
864
+ kind === 175 /* SyntaxKind.MethodDeclaration */ ||
865
+ kind === 174 /* SyntaxKind.MethodSignature */ ||
866
+ kind === 177 /* SyntaxKind.Constructor */;
867
+ applyToParameterTypes(c, source, target, (s, t) => this.inferFromContravariantTypesIfStrictFunctionTypes(s, t));
868
+ this.bivariant = saveBivariant;
869
+ }
870
+ applyToReturnTypes(c, source, target, (s, t) => this.inferFromTypes(s, t));
871
+ }
872
+ inferFromIndexTypes(source, target) {
873
+ const c = this.c;
874
+ // Inferences across mapped type index signatures are like inferences to homomorphic variables.
875
+ const priority = (source.objectFlags & target.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0
876
+ ? 8 /* InferencePriority.HomomorphicMappedType */
877
+ : 0 /* InferencePriority.None */;
878
+ const indexInfos = getIndexInfosOfType(c, target);
879
+ if (isObjectTypeWithInferableIndex(c, source))
880
+ for (const targetInfo of indexInfos) {
881
+ const propTypes = [];
882
+ for (const prop of getPropertiesOfType(c, source))
883
+ if (isApplicableIndexType(c, getLiteralTypeFromProperty(c, prop, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, false), targetInfo.keyType)) {
884
+ const propType = getTypeOfSymbol(c, prop);
885
+ propTypes.push((prop.flags & 16777216 /* SymbolFlags.Optional */) !== 0 ? removeMissingOrUndefinedType(c, propType) : propType);
886
+ }
887
+ for (const info of getIndexInfosOfType(c, source))
888
+ if (isApplicableIndexType(c, info.keyType, targetInfo.keyType))
889
+ propTypes.push(info.valueType);
890
+ if (propTypes.length !== 0)
891
+ this.inferWithPriority(getUnionType(c, propTypes), targetInfo.valueType, priority);
892
+ }
893
+ for (const targetInfo of indexInfos) {
894
+ const sourceInfo = getApplicableIndexInfo(c, source, targetInfo.keyType);
895
+ if (sourceInfo !== undefined)
896
+ this.inferWithPriority(sourceInfo.valueType, targetInfo.valueType, priority);
897
+ }
898
+ }
899
+ inferToMappedType(source, target, constraintType) {
900
+ const c = this.c;
901
+ if ((constraintType.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
902
+ let result = false;
903
+ for (const t of typesOf(constraintType))
904
+ result = this.inferToMappedType(source, target, t) || result;
905
+ return result;
906
+ }
907
+ if ((constraintType.flags & 2097152 /* TypeFlags.Index */) !== 0) {
908
+ // From S to a homomorphic `{ [P in keyof T]: X }`: reverse-map S and make a secondary
909
+ // inference to T, so direct inferences to T win over ones to `Partial<T>`.
910
+ const inference = this.infoFor(constraintType.target);
911
+ if (inference !== undefined && !inference.isFixed) {
912
+ const inferredType = inferTypeForHomomorphicMappedType(c, source, target, constraintType);
913
+ if (inferredType !== undefined)
914
+ this.inferWithPriority(inferredType, inference.typeParameter, (source.objectFlags & 262144 /* ObjectFlags.NonInferrableType */) !== 0
915
+ ? 16 /* InferencePriority.PartialHomomorphicMappedType */
916
+ : 8 /* InferencePriority.HomomorphicMappedType */);
917
+ }
918
+ return true;
919
+ }
920
+ if ((constraintType.flags & 524288 /* TypeFlags.TypeParameter */) !== 0) {
921
+ // From S to `{ [P in K]: X }`: infer `keyof S` to K, then to K's constraint like a homomorphic
922
+ // mapped type (for `Pick<T, K>`).
923
+ this.inferWithPriority(getIndexTypeEx(c, source, c.patternForType.has(source) ? 2 /* IndexFlags.NoIndexSignatures */ : 0 /* IndexFlags.None */), constraintType, 32 /* InferencePriority.MappedTypeConstraint */);
924
+ const extendedConstraint = getConstraintOfType(c, constraintType);
925
+ if (extendedConstraint !== undefined && this.inferToMappedType(source, target, extendedConstraint))
926
+ return true;
927
+ // Otherwise infer the union of the source's property types to X.
928
+ const types = [];
929
+ for (const p of getPropertiesOfType(c, source))
930
+ types.push(getTypeOfSymbol(c, p));
931
+ for (const info of getIndexInfosOfType(c, source))
932
+ types.push(info !== c.enumNumberIndexInfo ? info.valueType : c.neverType);
933
+ this.inferFromTypes(getUnionType(c, types), getTemplateTypeFromMappedType(c, target));
934
+ return true;
935
+ }
936
+ return false;
937
+ }
938
+ }
939
+ // ---- Helpers
940
+ function getInferenceInfoForType(inferences, t) {
941
+ if ((t.flags & 34078720 /* TypeFlags.TypeVariable */) !== 0)
942
+ for (const inference of inferences)
943
+ if (t === inference.typeParameter)
944
+ return inference;
945
+ return undefined;
946
+ }
947
+ function distributedTypes(t) {
948
+ return (t.flags & 134217728 /* TypeFlags.Union */) !== 0 ? typesOf(t) : [t];
949
+ }
950
+ function referenceTarget(t) {
951
+ return t.target;
952
+ }
953
+ function referenceNode(t) {
954
+ return t.node;
955
+ }
956
+ const noVariances = [];
957
+ /** Whether inferring to one of `targets` could record a candidate. */
958
+ function someTypeCouldHoldInference(c, targets) {
959
+ for (const t of targets)
960
+ if (couldHoldInference(c, t, 0))
961
+ return true;
962
+ return false;
963
+ }
964
+ /**
965
+ * `couldContainTypeVariables` without two of its conservative answers: a deferred reference without an
966
+ * alias, once its type arguments are resolved, holds type variables only through them, like any other
967
+ * reference; and the static side of a class declared at the top level of a file or module holds none
968
+ * outside its generic signatures, which inference erases.
969
+ */
970
+ function couldHoldInference(c, t, depth) {
971
+ if (!couldContainTypeVariables(c, t))
972
+ return false;
973
+ if (t.alias !== undefined || depth === 4)
974
+ return true;
975
+ if ((t.objectFlags & 4 /* ObjectFlags.Reference */) !== 0) {
976
+ const resolved = t.resolvedTypeArguments ?? sideOf(c, t).resolvedTypeArguments;
977
+ if (resolved === undefined)
978
+ return true;
979
+ for (const a of resolved)
980
+ if (couldHoldInference(c, a, depth + 1))
981
+ return true;
982
+ return false;
983
+ }
984
+ return !isTopLevelClassStaticSide(t);
985
+ }
986
+ function isTopLevelClassStaticSide(t) {
987
+ const symbol = t.symbol;
988
+ if ((t.objectFlags & 16 /* ObjectFlags.Anonymous */) === 0 ||
989
+ symbol === undefined ||
990
+ (symbol.flags & 32 /* SymbolFlags.Class */) === 0 ||
991
+ (symbol.flags &
992
+ (16 /* SymbolFlags.Function */ | 8192 /* SymbolFlags.Method */ | 2048 /* SymbolFlags.TypeLiteral */ | 4096 /* SymbolFlags.ObjectLiteral */)) !==
993
+ 0 ||
994
+ symbol.declarations.length === 0)
995
+ return false;
996
+ for (const d of symbol.declarations) {
997
+ if (d.kind !== 264 /* SyntaxKind.ClassDeclaration */ &&
998
+ d.kind !== 265 /* SyntaxKind.InterfaceDeclaration */ &&
999
+ d.kind !== 268 /* SyntaxKind.ModuleDeclaration */)
1000
+ return false;
1001
+ for (let n = d.parent; n.kind !== 307 /* SyntaxKind.SourceFile */; n = n.parent)
1002
+ if (n.kind !== 269 /* SyntaxKind.ModuleBlock */ && n.kind !== 268 /* SyntaxKind.ModuleDeclaration */)
1003
+ return false;
1004
+ }
1005
+ return true;
1006
+ }
1007
+ function isNonGenericObjectType(c, t) {
1008
+ return (t.flags & 1048576 /* TypeFlags.Object */) !== 0 && !isGenericMappedType(c, t);
1009
+ }
1010
+ function hasMappedNameType(t) {
1011
+ return t.declaration !== undefined && t.declaration.nameType !== undefined;
1012
+ }
1013
+ /** `(T | U)[K]` as `T[K] | U[K]` and `(T & U)[K]` as `T[K] & U[K]`. */
1014
+ function distributeIndexOverObjectType(c, objectType, indexType) {
1015
+ if ((objectType.flags & 134217728 /* TypeFlags.Union */) !== 0 ||
1016
+ ((objectType.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && !shouldDeferIndexType(c, objectType, 0 /* IndexFlags.None */))) {
1017
+ const types = [];
1018
+ for (const t of typesOf(objectType))
1019
+ types.push(getSimplifiedType(c, getIndexedAccessType(c, t, indexType), false));
1020
+ return (objectType.flags & 268435456 /* TypeFlags.Intersection */) !== 0 ? getIntersectionType(c, types) : getUnionType(c, types);
1021
+ }
1022
+ return undefined;
1023
+ }
1024
+ /** An object with a property of type `any` for each string literal in `t`. */
1025
+ function createEmptyObjectTypeFromStringLiteral(c, t) {
1026
+ const members = new Map();
1027
+ for (const u of distributedTypes(t)) {
1028
+ if ((u.flags & 1024 /* TypeFlags.StringLiteral */) === 0)
1029
+ continue;
1030
+ const name = u.stringValue;
1031
+ const literalProp = c.newSymbol(4 /* SymbolFlags.Property */, name);
1032
+ c.symbolTypes.set(getSymbolId(literalProp), c.anyType);
1033
+ if (u.symbol !== undefined) {
1034
+ literalProp.declarations = u.symbol.declarations;
1035
+ literalProp.valueDeclaration = u.symbol.valueDeclaration;
1036
+ }
1037
+ members.set(name, literalProp);
1038
+ }
1039
+ const indexInfos = (t.flags & 32 /* TypeFlags.String */) !== 0 ? [newIndexInfo(c.stringType, c.emptyObjectType, false, undefined)] : [];
1040
+ return createAnonymousType(c, undefined, members, [], [], indexInfos);
1041
+ }
1042
+ export function isTypeParameterAtTopLevel(c, t, tp, depth) {
1043
+ if (t === tp)
1044
+ return true;
1045
+ if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
1046
+ for (const u of typesOf(t))
1047
+ if (isTypeParameterAtTopLevel(c, u, tp, depth))
1048
+ return true;
1049
+ return false;
1050
+ }
1051
+ return (depth < 3 &&
1052
+ (t.flags & 67108864 /* TypeFlags.Conditional */) !== 0 &&
1053
+ (isTypeParameterAtTopLevel(c, getTrueTypeFromConditionalType(c, t), tp, depth + 1) ||
1054
+ isTypeParameterAtTopLevel(c, getFalseTypeFromConditionalType(c, t), tp, depth + 1)));
1055
+ }
1056
+ function isTypeParameterAtTopLevelInReturnType(c, signature, typeParameter) {
1057
+ const typePredicate = getTypePredicateOfSignature(c, signature);
1058
+ if (typePredicate !== undefined)
1059
+ return typePredicate.type !== undefined && isTypeParameterAtTopLevel(c, typePredicate.type, typeParameter, 0);
1060
+ return isTypeParameterAtTopLevel(c, getReturnTypeOfSignature(c, signature), typeParameter, 0);
1061
+ }
1062
+ function isTypeOrBaseIdenticalTo(c, s, t) {
1063
+ if (t === c.missingType)
1064
+ return s === t;
1065
+ return (isTypeIdenticalTo(c, s, t) ||
1066
+ ((t.flags & 32 /* TypeFlags.String */) !== 0 && (s.flags & 1024 /* TypeFlags.StringLiteral */) !== 0) ||
1067
+ ((t.flags & 64 /* TypeFlags.Number */) !== 0 && (s.flags & 2048 /* TypeFlags.NumberLiteral */) !== 0));
1068
+ }
1069
+ function isTypeCloselyMatchedBy(s, t) {
1070
+ if ((s.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
1071
+ (t.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
1072
+ s.symbol !== undefined &&
1073
+ s.symbol === t.symbol)
1074
+ return true;
1075
+ const sa = s.alias;
1076
+ const ta = t.alias;
1077
+ return sa !== undefined && ta !== undefined && sa.typeArguments.length !== 0 && sa.symbol === ta.symbol;
1078
+ }
1079
+ function typesDefinitelyUnrelated(c, source, target) {
1080
+ // Tuples of incompatible arities, or objects that each have a property missing in the other.
1081
+ if (isTupleType(source) && isTupleType(target)) {
1082
+ const s = tupleTargetOf(source);
1083
+ const t = tupleTargetOf(target);
1084
+ return (((t.combinedFlags & 8 /* ElementFlags.Variadic */) === 0 && t.minLength > s.minLength) ||
1085
+ ((t.combinedFlags & 12 /* ElementFlags.Variable */) === 0 &&
1086
+ ((s.combinedFlags & 12 /* ElementFlags.Variable */) !== 0 || t.fixedLength < s.fixedLength)));
1087
+ }
1088
+ return hasUnmatchedProperty(c, source, target, true) && hasUnmatchedProperty(c, target, source, false);
1089
+ }
1090
+ /**
1091
+ * Whether `getUnmatchedProperty(c, source, target, false, matchDiscriminantProperties)` finds a
1092
+ * property, without creating every property of a union or intersection target whose properties
1093
+ * aren't resolved yet. That test reports only required properties: a union property is optional
1094
+ * when it is optional in any constituent, an intersection property when it is optional in every
1095
+ * constituent that has it. So only names required in a constituent (of those
1096
+ * `getPropertiesOfUnionOrIntersectionType` reads) are candidates, and the search stops at the first
1097
+ * unmatched one. A union property is created only when the result can depend on it: when the source
1098
+ * lacks the name, or for the discriminant test when the first constituent's property type is a unit
1099
+ * type or never (otherwise the union of the constituents' types is not a unit type).
1100
+ */
1101
+ function hasUnmatchedProperty(c, source, target, matchDiscriminantProperties) {
1102
+ const t = getReducedApparentType(c, target);
1103
+ if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) === 0 ||
1104
+ sideOf(c, t).resolvedProperties !== undefined)
1105
+ return getUnmatchedProperty(c, source, target, false, matchDiscriminantProperties) !== undefined;
1106
+ const containingType = t;
1107
+ const isUnion = (t.flags & 134217728 /* TypeFlags.Union */) !== 0;
1108
+ const checked = new Set();
1109
+ for (const current of containingType.types) {
1110
+ for (const prop of getPropertiesOfType(c, current)) {
1111
+ const name = prop.name;
1112
+ if ((prop.flags & 16777216 /* SymbolFlags.Optional */) !== 0) {
1113
+ if (isUnion)
1114
+ checked.add(name);
1115
+ continue;
1116
+ }
1117
+ if (checked.has(name))
1118
+ continue;
1119
+ checked.add(name);
1120
+ if (isUnion &&
1121
+ getPropertyOfType(c, source, name) !== undefined &&
1122
+ (!matchDiscriminantProperties || (getTypeOfSymbol(c, prop).flags & (97292 /* TypeFlags.Unit */ | 262144 /* TypeFlags.Never */)) === 0))
1123
+ continue;
1124
+ const combined = getPropertyOfUnionOrIntersectionType(c, containingType, name, !isUnion);
1125
+ if (combined !== undefined && isUnmatchedProperty(c, source, combined, false, matchDiscriminantProperties))
1126
+ return true;
1127
+ }
1128
+ if (isUnion && getIndexInfosOfType(c, current).length === 0)
1129
+ break;
1130
+ }
1131
+ return false;
1132
+ }
1133
+ function isTupleTypeStructureMatching(t1, t2) {
1134
+ if (getTypeReferenceArity(t1) !== getTypeReferenceArity(t2))
1135
+ return false;
1136
+ const infos1 = tupleTargetOf(t1).elementInfos;
1137
+ const infos2 = tupleTargetOf(t2).elementInfos;
1138
+ for (let i = 0; i < infos1.length; i++)
1139
+ if ((infos1[i].flags & 12 /* ElementFlags.Variable */) !== (infos2[i].flags & 12 /* ElementFlags.Variable */))
1140
+ return false;
1141
+ return true;
1142
+ }
1143
+ export function isConstTypeVariable(c, t, depth) {
1144
+ if (depth >= 5 || t === undefined)
1145
+ return false;
1146
+ if ((t.flags & 524288 /* TypeFlags.TypeParameter */) !== 0) {
1147
+ if (t.symbol === undefined)
1148
+ return false;
1149
+ for (const d of t.symbol.declarations)
1150
+ if (hasSyntacticModifier(d, 4096 /* ModifierFlags.Const */))
1151
+ return true;
1152
+ return false;
1153
+ }
1154
+ if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
1155
+ for (const s of typesOf(t))
1156
+ if (isConstTypeVariable(c, s, depth))
1157
+ return true;
1158
+ return false;
1159
+ }
1160
+ if ((t.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0)
1161
+ return isConstTypeVariable(c, t.objectType, depth + 1);
1162
+ if ((t.flags & 67108864 /* TypeFlags.Conditional */) !== 0)
1163
+ return isConstTypeVariable(c, getConstraintOfConditionalType(c, t), depth + 1);
1164
+ if ((t.flags & 16777216 /* TypeFlags.Substitution */) !== 0)
1165
+ return isConstTypeVariable(c, t.baseType, depth);
1166
+ if ((t.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0) {
1167
+ const typeVariable = getHomomorphicTypeVariable(c, t);
1168
+ return typeVariable !== undefined && isConstTypeVariable(c, typeVariable, depth);
1169
+ }
1170
+ if (isGenericTupleType(t)) {
1171
+ const elements = getElementTypes(c, t);
1172
+ const infos = tupleTargetOf(t).elementInfos;
1173
+ for (let i = 0; i < elements.length; i++)
1174
+ if ((infos[i].flags & 8 /* ElementFlags.Variadic */) !== 0 && isConstTypeVariable(c, elements[i], depth))
1175
+ return true;
1176
+ }
1177
+ return false;
1178
+ }
1179
+ export function isMutableArrayLikeType(c, t) {
1180
+ return (isMutableArrayOrTuple(c, t) ||
1181
+ ((t.flags & (1 /* TypeFlags.Any */ | 12 /* TypeFlags.Nullable */)) === 0 && isTypeAssignableTo(c, t, c.anyArrayType)));
1182
+ }
1183
+ function typePredicateKindsMatch(a, b) {
1184
+ return a.kind === b.kind && a.parameterIndex === b.parameterIndex;
1185
+ }
1186
+ /** Calls `callback` with each pair of corresponding parameter types of `source` and `target`. */
1187
+ export function applyToParameterTypes(c, source, target, callback) {
1188
+ const sourceCount = getParameterCount(c, source);
1189
+ const targetCount = getParameterCount(c, target);
1190
+ const sourceRestType = getEffectiveRestType(c, source);
1191
+ const targetRestType = getEffectiveRestType(c, target);
1192
+ const targetNonRestCount = targetRestType !== undefined ? targetCount - 1 : targetCount;
1193
+ const paramCount = sourceRestType !== undefined ? targetNonRestCount : Math.min(sourceCount, targetNonRestCount);
1194
+ const sourceThisType = getThisTypeOfSignature(c, source);
1195
+ if (sourceThisType !== undefined) {
1196
+ const targetThisType = getThisTypeOfSignature(c, target);
1197
+ if (targetThisType !== undefined)
1198
+ callback(sourceThisType, targetThisType);
1199
+ }
1200
+ for (let i = 0; i < paramCount; i++)
1201
+ callback(getTypeAtPosition(c, source, i), getTypeAtPosition(c, target, i));
1202
+ if (targetRestType !== undefined)
1203
+ callback(getRestTypeAtPosition(c, source, paramCount, isConstTypeVariable(c, targetRestType, 0) && !someType(targetRestType, (t) => isMutableArrayLikeType(c, t))), targetRestType);
1204
+ }
1205
+ export function applyToReturnTypes(c, source, target, callback) {
1206
+ const targetTypePredicate = getTypePredicateOfSignature(c, target);
1207
+ if (targetTypePredicate !== undefined) {
1208
+ const sourceTypePredicate = getTypePredicateOfSignature(c, source);
1209
+ if (sourceTypePredicate !== undefined &&
1210
+ typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) &&
1211
+ sourceTypePredicate.type !== undefined &&
1212
+ targetTypePredicate.type !== undefined) {
1213
+ callback(sourceTypePredicate.type, targetTypePredicate.type);
1214
+ return;
1215
+ }
1216
+ }
1217
+ const targetReturnType = getReturnTypeOfSignature(c, target);
1218
+ if (couldContainTypeVariables(c, targetReturnType))
1219
+ callback(getReturnTypeOfSignature(c, source), targetReturnType);
1220
+ }
1221
+ /** `signature` with its type parameters replaced by their base constraints. */
1222
+ export function getBaseSignature(c, signature) {
1223
+ const typeParameters = signature.typeParameters;
1224
+ if (typeParameters.length === 0)
1225
+ return signature;
1226
+ const side = signatureSideOf(c, signature);
1227
+ if (side.baseSignatureCache !== undefined)
1228
+ return side.baseSignatureCache;
1229
+ const constraints = [];
1230
+ for (const tp of typeParameters)
1231
+ constraints.push(getConstraintOfTypeParameter(c, tp) ?? c.unknownType);
1232
+ const baseConstraintMapper = newTypeMapper(typeParameters, constraints);
1233
+ let baseConstraints = [];
1234
+ for (const tp of typeParameters)
1235
+ baseConstraints.push(instantiateType(c, tp, baseConstraintMapper));
1236
+ // Resolve non-circular interdependent constraints, then erase what remains circular.
1237
+ for (let i = 0; i < typeParameters.length - 1; i++)
1238
+ baseConstraints = instantiateTypes(c, baseConstraints, baseConstraintMapper);
1239
+ baseConstraints = instantiateTypes(c, baseConstraints, new ArrayToSingleTypeMapper(typeParameters, c.anyType));
1240
+ const result = instantiateSignatureEx(c, signature, newTypeMapper(typeParameters, baseConstraints), true);
1241
+ side.baseSignatureCache = result;
1242
+ return result;
1243
+ }
1244
+ export function getTypeParametersForMapper(c, signature) {
1245
+ const result = [];
1246
+ for (const tp of signature.typeParameters)
1247
+ result.push(instantiateType(c, tp, tp.mapper));
1248
+ return result;
1249
+ }
1250
+ /** The generic `signature` instantiated in the context of the non-generic `contextualSignature`. */
1251
+ export function instantiateSignatureInContextOf(c, signature, contextualSignature, inferenceContext, compareTypes) {
1252
+ const context = createInferenceContext(c, getTypeParametersForMapper(c, signature), signature, 0 /* InferenceFlags.None */, compareTypes);
1253
+ // Clone so that inferring through a rest type parameter doesn't fix it.
1254
+ const restType = getEffectiveRestType(c, contextualSignature);
1255
+ const mapper = inferenceContext !== undefined
1256
+ ? restType !== undefined && (restType.flags & 524288 /* TypeFlags.TypeParameter */) !== 0
1257
+ ? inferenceContext.nonFixingMapper
1258
+ : inferenceContext.mapper
1259
+ : undefined;
1260
+ const sourceSignature = mapper !== undefined ? instantiateSignature(c, contextualSignature, mapper) : contextualSignature;
1261
+ applyToParameterTypes(c, sourceSignature, signature, (source, target) => inferTypes(c, context.inferences, source, target, 0 /* InferencePriority.None */, false));
1262
+ if (inferenceContext === undefined)
1263
+ applyToReturnTypes(c, contextualSignature, signature, (source, target) => inferTypes(c, context.inferences, source, target, 128 /* InferencePriority.ReturnType */, false));
1264
+ return getSignatureInstantiation(c, signature, getInferredTypes(c, context), contextualSignature.declaration !== undefined &&
1265
+ (contextualSignature.declaration.flags & 65536 /* NodeFlags.JavaScriptFile */) !== 0);
1266
+ }
1267
+ // ---- Template literal inference
1268
+ /**
1269
+ * Whether `source`, a string literal or a template literal type, fails the start and end text test of
1270
+ * `inferFromLiteralPartsToTemplateLiteral` against `target`: its first text does not start with the
1271
+ * target's first text, its last text does not end with the target's last text, or a string literal
1272
+ * is shorter than both. `inferTypesFromTemplateLiteralType` returns undefined then.
1273
+ */
1274
+ export function failsTemplateLiteralTextTest(source, target) {
1275
+ const targetTexts = target.texts;
1276
+ const targetStartText = targetTexts[0];
1277
+ const targetEndText = targetTexts[targetTexts.length - 1];
1278
+ if ((source.flags & 1024 /* TypeFlags.StringLiteral */) !== 0) {
1279
+ const text = source.stringValue;
1280
+ return (text.length < targetStartText.length + targetEndText.length ||
1281
+ !text.startsWith(targetStartText) ||
1282
+ !text.endsWith(targetEndText));
1283
+ }
1284
+ const sourceTexts = source.texts;
1285
+ return !sourceTexts[0].startsWith(targetStartText) || !sourceTexts[sourceTexts.length - 1].endsWith(targetEndText);
1286
+ }
1287
+ /**
1288
+ * Whether `source` is a string literal, or a template literal type whose placeholders are `string`,
1289
+ * `number` or `bigint`, that fails the start and end text test against `target`. It is then not
1290
+ * assignable to `target`, and relating the two has no other effect: such a template has no type
1291
+ * variables for a variance marker mapper to find.
1292
+ */
1293
+ export function isUnmatchedTemplateLiteralKey(source, target) {
1294
+ if ((source.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
1295
+ for (const t of source.types)
1296
+ if ((t.flags & (32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */ | 128 /* TypeFlags.BigInt */)) === 0)
1297
+ return false;
1298
+ }
1299
+ else if ((source.flags & 1024 /* TypeFlags.StringLiteral */) === 0)
1300
+ return false;
1301
+ return failsTemplateLiteralTextTest(source, target);
1302
+ }
1303
+ /** The types matched by `target`'s placeholders in `source`, or undefined if it doesn't match. */
1304
+ export function inferTypesFromTemplateLiteralType(c, source, target) {
1305
+ if ((source.flags & 1024 /* TypeFlags.StringLiteral */) !== 0) {
1306
+ // Most string literals fail the start and end text test; apply it before building the
1307
+ // one-element source texts.
1308
+ if (failsTemplateLiteralTextTest(source, target))
1309
+ return undefined;
1310
+ return inferFromLiteralPartsToTemplateLiteral(c, [source.stringValue], emptyTypes, target);
1311
+ }
1312
+ if ((source.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
1313
+ const s = source;
1314
+ if (arraysEqual(s.texts, target.texts)) {
1315
+ const result = [];
1316
+ for (let i = 0; i < s.types.length; i++) {
1317
+ const t = s.types[i];
1318
+ result.push(isTypeAssignableTo(c, getBaseConstraintOrType(c, t), getBaseConstraintOrType(c, target.types[i]))
1319
+ ? t
1320
+ : getStringLikeTypeForType(c, t));
1321
+ }
1322
+ return result;
1323
+ }
1324
+ return inferFromLiteralPartsToTemplateLiteral(c, s.texts, s.types, target);
1325
+ }
1326
+ return undefined;
1327
+ }
1328
+ function arraysEqual(a, b) {
1329
+ if (a.length !== b.length)
1330
+ return false;
1331
+ for (let i = 0; i < a.length; i++)
1332
+ if (a[i] !== b[i])
1333
+ return false;
1334
+ return true;
1335
+ }
1336
+ function getStringLikeTypeForType(c, t) {
1337
+ return (t.flags & (1 /* TypeFlags.Any */ | 12583968 /* TypeFlags.StringLike */)) !== 0 ? t : getTemplateLiteralType(c, ['', ''], [t]);
1338
+ }
1339
+ /** Matches the text and type parts of a source literal against a target template literal. */
1340
+ class TemplateMatcher {
1341
+ c;
1342
+ sourceTexts;
1343
+ sourceTypes;
1344
+ lastSourceIndex;
1345
+ remainingEndText;
1346
+ seg = 0;
1347
+ pos;
1348
+ matches = [];
1349
+ constructor(c, sourceTexts, sourceTypes, remainingEndText, pos) {
1350
+ this.c = c;
1351
+ this.sourceTexts = sourceTexts;
1352
+ this.sourceTypes = sourceTypes;
1353
+ this.lastSourceIndex = sourceTexts.length - 1;
1354
+ this.remainingEndText = remainingEndText;
1355
+ this.pos = pos;
1356
+ }
1357
+ sourceText(index) {
1358
+ return index < this.lastSourceIndex ? this.sourceTexts[index] : this.remainingEndText;
1359
+ }
1360
+ addMatch(s, p) {
1361
+ let matchType;
1362
+ if (s === this.seg)
1363
+ matchType = getStringLiteralType(this.c, this.sourceText(s).substring(this.pos, p));
1364
+ else {
1365
+ const texts = [this.sourceTexts[this.seg].substring(this.pos)];
1366
+ for (let i = this.seg + 1; i < s; i++)
1367
+ texts.push(this.sourceTexts[i]);
1368
+ texts.push(this.sourceText(s).substring(0, p));
1369
+ matchType = getTemplateLiteralType(this.c, texts, this.sourceTypes.slice(this.seg, s));
1370
+ }
1371
+ this.matches.push(matchType);
1372
+ this.seg = s;
1373
+ this.pos = p;
1374
+ }
1375
+ }
1376
+ function inferFromLiteralPartsToTemplateLiteral(c, sourceTexts, sourceTypes, target) {
1377
+ const lastSourceIndex = sourceTexts.length - 1;
1378
+ const sourceStartText = sourceTexts[0];
1379
+ const sourceEndText = sourceTexts[lastSourceIndex];
1380
+ const targetTexts = target.texts;
1381
+ const lastTargetIndex = targetTexts.length - 1;
1382
+ const targetStartText = targetTexts[0];
1383
+ const targetEndText = targetTexts[lastTargetIndex];
1384
+ if ((lastSourceIndex === 0 && sourceStartText.length < targetStartText.length + targetEndText.length) ||
1385
+ !sourceStartText.startsWith(targetStartText) ||
1386
+ !sourceEndText.endsWith(targetEndText))
1387
+ return undefined;
1388
+ const m = new TemplateMatcher(c, sourceTexts, sourceTypes, sourceEndText.substring(0, sourceEndText.length - targetEndText.length), targetStartText.length);
1389
+ for (let i = 1; i < lastTargetIndex; i++) {
1390
+ const delim = targetTexts[i];
1391
+ if (delim.length > 0) {
1392
+ let s = m.seg;
1393
+ let p = m.pos;
1394
+ for (;;) {
1395
+ const d = m.sourceText(s).indexOf(delim, p);
1396
+ if (d >= 0) {
1397
+ p = d;
1398
+ break;
1399
+ }
1400
+ s++;
1401
+ if (s === sourceTexts.length)
1402
+ return undefined;
1403
+ p = 0;
1404
+ }
1405
+ m.addMatch(s, p);
1406
+ m.pos += delim.length;
1407
+ }
1408
+ else if (m.pos < m.sourceText(m.seg).length) {
1409
+ // Consume one code point.
1410
+ const code = m.sourceText(m.seg).codePointAt(m.pos);
1411
+ m.addMatch(m.seg, m.pos + (code > 0xffff ? 2 : 1));
1412
+ }
1413
+ else if (m.seg < lastSourceIndex)
1414
+ m.addMatch(m.seg + 1, 0);
1415
+ else
1416
+ return undefined;
1417
+ }
1418
+ m.addMatch(lastSourceIndex, m.sourceText(lastSourceIndex).length);
1419
+ return m.matches;
1420
+ }
1421
+ /** For a string literal inferred to a placeholder constrained to primitives, the literal type the constraint prefers. */
1422
+ function getTemplatePlaceholderInference(c, source, constraint) {
1423
+ let allTypeFlags = 0 /* TypeFlags.None */;
1424
+ for (const t of distributedTypes(constraint))
1425
+ allTypeFlags |= t.flags;
1426
+ // A constraint including `string` needs no more specific type.
1427
+ if ((allTypeFlags & 32 /* TypeFlags.String */) !== 0)
1428
+ return undefined;
1429
+ const str = source.stringValue;
1430
+ if ((allTypeFlags & 67648 /* TypeFlags.NumberLike */) !== 0 && !isValidNumberString(str, true))
1431
+ allTypeFlags &= ~67648 /* TypeFlags.NumberLike */;
1432
+ if ((allTypeFlags & 4224 /* TypeFlags.BigIntLike */) !== 0 && !isValidBigIntString(str, true))
1433
+ allTypeFlags &= ~4224 /* TypeFlags.BigIntLike */;
1434
+ let matching = c.neverType;
1435
+ for (const t of distributedTypes(constraint))
1436
+ matching = choosePlaceholderType(c, matching, t, source, str, allTypeFlags);
1437
+ return (matching.flags & 262144 /* TypeFlags.Never */) === 0 ? matching : undefined;
1438
+ }
1439
+ function choosePlaceholderType(c, left, right, source, str, allTypeFlags) {
1440
+ if ((right.flags & allTypeFlags) === 0)
1441
+ return left;
1442
+ if ((left.flags & 32 /* TypeFlags.String */) !== 0)
1443
+ return left;
1444
+ if ((right.flags & 32 /* TypeFlags.String */) !== 0)
1445
+ return source;
1446
+ if ((left.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0)
1447
+ return left;
1448
+ if ((right.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0 &&
1449
+ isTypeMatchedByTemplateLiteralType(c, source, right))
1450
+ return source;
1451
+ if ((left.flags & 8388608 /* TypeFlags.StringMapping */) !== 0)
1452
+ return left;
1453
+ if ((right.flags & 8388608 /* TypeFlags.StringMapping */) !== 0 && str === applyStringMapping(right.symbol, str))
1454
+ return source;
1455
+ if ((left.flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
1456
+ return left;
1457
+ if ((right.flags & 1024 /* TypeFlags.StringLiteral */) !== 0 && right.stringValue === str)
1458
+ return right;
1459
+ if ((left.flags & 64 /* TypeFlags.Number */) !== 0)
1460
+ return left;
1461
+ if ((right.flags & 64 /* TypeFlags.Number */) !== 0)
1462
+ return getNumberLiteralType(c, Number(str));
1463
+ if ((left.flags & 65536 /* TypeFlags.Enum */) !== 0)
1464
+ return left;
1465
+ if ((right.flags & 65536 /* TypeFlags.Enum */) !== 0)
1466
+ return getNumberLiteralType(c, Number(str));
1467
+ if ((left.flags & 2048 /* TypeFlags.NumberLiteral */) !== 0)
1468
+ return left;
1469
+ if ((right.flags & 2048 /* TypeFlags.NumberLiteral */) !== 0 && right.numberValue === Number(str))
1470
+ return right;
1471
+ if ((left.flags & 128 /* TypeFlags.BigInt */) !== 0)
1472
+ return left;
1473
+ if ((right.flags & 128 /* TypeFlags.BigInt */) !== 0)
1474
+ return getBigIntLiteralType(c, parsePseudoBigInt(str));
1475
+ if ((left.flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0)
1476
+ return left;
1477
+ if ((right.flags & 4096 /* TypeFlags.BigIntLiteral */) !== 0 && right.stringValue === str)
1478
+ return right;
1479
+ if ((left.flags & 256 /* TypeFlags.Boolean */) !== 0)
1480
+ return left;
1481
+ if ((right.flags & 256 /* TypeFlags.Boolean */) !== 0)
1482
+ return str === 'true' ? c.trueType : str === 'false' ? c.falseType : c.booleanType;
1483
+ if ((left.flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0)
1484
+ return left;
1485
+ if ((right.flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0 && right.stringValue === str)
1486
+ return right;
1487
+ if ((left.flags & 4 /* TypeFlags.Undefined */) !== 0)
1488
+ return left;
1489
+ if ((right.flags & 4 /* TypeFlags.Undefined */) !== 0 && right.intrinsicName === str)
1490
+ return right;
1491
+ if ((left.flags & 8 /* TypeFlags.Null */) !== 0)
1492
+ return left;
1493
+ if ((right.flags & 8 /* TypeFlags.Null */) !== 0 && right.intrinsicName === str)
1494
+ return right;
1495
+ return left;
1496
+ }
1497
+ export function isValidNumberString(s, roundTripOnly) {
1498
+ if (s === '')
1499
+ return false;
1500
+ const n = Number(s);
1501
+ return isFinite(n) && (!roundTripOnly || String(n) === s);
1502
+ }
1503
+ /** Whether `s` scans as a bigint literal without separators; round-trippable ones print as written. */
1504
+ export function isValidBigIntString(s, roundTripOnly) {
1505
+ if (roundTripOnly)
1506
+ return /^-?(0|[1-9][0-9]*)$/.test(s) && s !== '-0';
1507
+ return /^-?(0|[1-9][0-9]*|0[xX][0-9a-fA-F]+|0[bB][01]+|0[oO][0-7]+)$/.test(s);
1508
+ }
1509
+ // ---- Reverse mapped types
1510
+ /** Infers a source type for the homomorphic mapped type `{ [P in keyof T]: X }`, cached. */
1511
+ export function inferTypeForHomomorphicMappedType(c, source, target, constraint) {
1512
+ const key = source.id + ',' + target.id + ',' + constraint.id;
1513
+ const caches = c.inferenceCaches;
1514
+ if (caches.reverseHomomorphicMapped.has(key))
1515
+ return caches.reverseHomomorphicMapped.get(key);
1516
+ const t = createReverseMappedType(c, source, target, constraint);
1517
+ caches.reverseHomomorphicMapped.set(key, t);
1518
+ return t;
1519
+ }
1520
+ function createReverseMappedType(c, source, target, constraint) {
1521
+ // Reverse mappable: a string index signature, or properties of a partially inferable type.
1522
+ if (!(getIndexInfoOfType(c, source, c.stringType) !== undefined ||
1523
+ (getPropertiesOfType(c, source).length !== 0 && isPartiallyInferableType(c, source))))
1524
+ return undefined;
1525
+ // Arrays and tuples map to arrays and tuples of reverse-mapped elements.
1526
+ if (isArrayType(c, source)) {
1527
+ const elementType = inferReverseMappedType(c, getTypeArguments(c, source)[0], target, constraint);
1528
+ if (elementType === undefined)
1529
+ return undefined;
1530
+ return createArrayTypeEx(c, elementType, isReadonlyArrayType(c, source));
1531
+ }
1532
+ if (isTupleType(source)) {
1533
+ const elementTypes = [];
1534
+ for (const t of getElementTypes(c, source)) {
1535
+ const e = inferReverseMappedType(c, t, target, constraint);
1536
+ if (e === undefined)
1537
+ return undefined;
1538
+ elementTypes.push(e);
1539
+ }
1540
+ let elementInfos = tupleTargetOf(source).elementInfos;
1541
+ if ((getMappedTypeModifiers(target) & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0)
1542
+ elementInfos = elementInfos.map((info) => (info.flags & 2 /* ElementFlags.Optional */) !== 0
1543
+ ? new TupleElementInfo(1 /* ElementFlags.Required */, info.labeledDeclaration)
1544
+ : info);
1545
+ return createTupleTypeEx(c, elementTypes, elementInfos, tupleTargetOf(source).readonly);
1546
+ }
1547
+ // Other object types map to an object whose properties are reverse-mapped lazily.
1548
+ const reversed = newObjectType(c, 1024 /* ObjectFlags.ReverseMapped */ | 16 /* ObjectFlags.Anonymous */, undefined);
1549
+ reversed.source = source;
1550
+ reversed.mappedType = target;
1551
+ reversed.constraintType = constraint;
1552
+ return reversed;
1553
+ }
1554
+ /** Not marked non-inferable, or an object literal or tuple with some inferable part. */
1555
+ function isPartiallyInferableType(c, t) {
1556
+ if ((t.objectFlags & 262144 /* ObjectFlags.NonInferrableType */) === 0)
1557
+ return true;
1558
+ if (isObjectLiteralType(t))
1559
+ for (const p of getPropertiesOfType(c, t))
1560
+ if (isPartiallyInferableType(c, getTypeOfSymbol(c, p)))
1561
+ return true;
1562
+ if (isTupleType(t))
1563
+ for (const e of getElementTypes(c, t))
1564
+ if (isPartiallyInferableType(c, e))
1565
+ return true;
1566
+ return false;
1567
+ }
1568
+ export function inferReverseMappedType(c, source, target, constraint) {
1569
+ const key = source.id + ',' + target.id + ',' + constraint.id;
1570
+ const caches = c.inferenceCaches;
1571
+ if (caches.reverseMapped.has(key))
1572
+ return caches.reverseMapped.get(key) ?? c.unknownType;
1573
+ const stackDepth = caches.reverseStackDepth;
1574
+ setStackEntry(caches.reverseSourceStack, stackDepth, source);
1575
+ setStackEntry(caches.reverseTargetStack, stackDepth, target);
1576
+ caches.reverseStackDepth = stackDepth + 1;
1577
+ const saveExpandingFlags = caches.reverseExpandingFlags;
1578
+ if (isDeeplyNestedType(c, source, caches.reverseSourceStack, stackDepth + 1, 2))
1579
+ caches.reverseExpandingFlags |= 1 /* ExpandingFlags.Source */;
1580
+ if (isDeeplyNestedType(c, target, caches.reverseTargetStack, stackDepth + 1, 2))
1581
+ caches.reverseExpandingFlags |= 2 /* ExpandingFlags.Target */;
1582
+ let t = undefined;
1583
+ if (caches.reverseExpandingFlags !== 3 /* ExpandingFlags.Both */) {
1584
+ const typeParameter = getIndexedAccessType(c, constraint.target, getTypeParameterFromMappedType(c, target));
1585
+ const templateType = getTemplateTypeFromMappedType(c, target);
1586
+ const inference = new InferenceInfo(typeParameter);
1587
+ inferTypes(c, [inference], source, templateType, 0 /* InferencePriority.None */, false);
1588
+ t = getWidenedType(c, getTypeFromInference(c, inference) ?? c.unknownType);
1589
+ }
1590
+ caches.reverseStackDepth = stackDepth;
1591
+ caches.reverseExpandingFlags = saveExpandingFlags;
1592
+ caches.reverseMapped.set(key, t);
1593
+ return t;
1594
+ }
1595
+ export function resolveReverseMappedTypeMembers(c, t) {
1596
+ const source = t.source;
1597
+ const mappedType = t.mappedType;
1598
+ const constraintType = t.constraintType;
1599
+ const indexInfo = getIndexInfoOfType(c, source, c.stringType);
1600
+ const modifiers = getMappedTypeModifiers(mappedType);
1601
+ const readonlyMask = (modifiers & 1 /* MappedTypeModifiers.IncludeReadonly */) === 0;
1602
+ const optionalMask = (modifiers & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0 ? 0 : 16777216 /* SymbolFlags.Optional */;
1603
+ const indexInfos = indexInfo !== undefined
1604
+ ? [
1605
+ newIndexInfo(c.stringType, inferReverseMappedType(c, indexInfo.valueType, mappedType, constraintType) ?? c.unknownType, readonlyMask && indexInfo.isReadonly, undefined),
1606
+ ]
1607
+ : [];
1608
+ const members = new Map();
1609
+ const limitedConstraint = getLimitedConstraint(c, t);
1610
+ for (const prop of getPropertiesOfType(c, source)) {
1611
+ // With an intersection constraint, skip properties the filtering literals exclude.
1612
+ if (limitedConstraint !== undefined) {
1613
+ const propertyNameType = getLiteralTypeFromProperty(c, prop, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, false);
1614
+ if (!isTypeAssignableTo(c, propertyNameType, limitedConstraint))
1615
+ continue;
1616
+ }
1617
+ const checkFlags = 8192 /* CheckFlags.ReverseMapped */ | (readonlyMask && isReadonlySymbol(c, prop) ? 8 /* CheckFlags.Readonly */ : 0);
1618
+ const inferredProp = c.newSymbolWithCheckFlags(4 /* SymbolFlags.Property */ | (prop.flags & optionalMask), prop.name, checkFlags);
1619
+ inferredProp.declarations = prop.declarations;
1620
+ const links = c.symbolLinks(inferredProp);
1621
+ const id = inferredProp.id;
1622
+ c.symbolNameTypes.set(id, c.symbolNameTypes.get(getSymbolId(prop)));
1623
+ links.reverseMappedPropertyType = getTypeOfSymbol(c, prop);
1624
+ const constraintTarget = constraintType.target;
1625
+ if ((constraintTarget.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0 &&
1626
+ (constraintTarget.objectType.flags & 524288 /* TypeFlags.TypeParameter */) !== 0 &&
1627
+ (constraintTarget.indexType.flags & 524288 /* TypeFlags.TypeParameter */) !== 0) {
1628
+ // `{[K in keyof T[K_1]]: T[K_1]}` reverse-maps like `{[K in keyof T]: T}`.
1629
+ const newTypeParam = constraintTarget.objectType;
1630
+ links.reverseMappedMappedType = replaceIndexedAccess(c, mappedType, constraintTarget, newTypeParam);
1631
+ links.reverseMappedConstraintType = getIndexType(c, newTypeParam);
1632
+ }
1633
+ else {
1634
+ links.reverseMappedMappedType = mappedType;
1635
+ links.reverseMappedConstraintType = constraintType;
1636
+ }
1637
+ members.set(prop.name, inferredProp);
1638
+ }
1639
+ setStructuredTypeMembers(c, t, members, [], [], indexInfos);
1640
+ }
1641
+ export function getTypeOfReverseMappedSymbol(c, symbol) {
1642
+ const links = c.symbolLinks(symbol);
1643
+ const id = symbol.id;
1644
+ let type = c.symbolTypes.get(id);
1645
+ if (type === undefined) {
1646
+ type =
1647
+ inferReverseMappedType(c, links.reverseMappedPropertyType, links.reverseMappedMappedType, links.reverseMappedConstraintType) ?? c.unknownType;
1648
+ c.symbolTypes.set(id, type);
1649
+ }
1650
+ return type;
1651
+ }
1652
+ /** The filtering literals of an intersection constraint, e.g. `"a" | "b"` in `keyof U & ("a" | "b")`. */
1653
+ function getLimitedConstraint(c, t) {
1654
+ const constraint = getConstraintTypeFromMappedType(c, t.mappedType);
1655
+ if ((constraint.flags & 402653184 /* TypeFlags.UnionOrIntersection */) === 0)
1656
+ return undefined;
1657
+ const origin = (constraint.flags & 134217728 /* TypeFlags.Union */) !== 0 ? constraint.origin : constraint;
1658
+ if (origin === undefined || (origin.flags & 268435456 /* TypeFlags.Intersection */) === 0)
1659
+ return undefined;
1660
+ const limited = getIntersectionType(c, typesOf(origin).filter((u) => u !== t.constraintType));
1661
+ return limited !== c.neverType ? limited : undefined;
1662
+ }
1663
+ function replaceIndexedAccess(c, instantiable, t, replacement) {
1664
+ // Map the index to 0 and the object to `[replacement]`, making the access `replacement`.
1665
+ return instantiateType(c, instantiable, newTypeMapper([t.indexType, t.objectType], [getNumberLiteralType(c, 0), createTupleType(c, [replacement])]));
1666
+ }
1667
+ export function instantiateReverseMappedType(c, t, mapper) {
1668
+ const innerMappedType = instantiateType(c, t.mappedType, mapper);
1669
+ if ((innerMappedType.objectFlags & 32 /* ObjectFlags.Mapped */) === 0)
1670
+ return t;
1671
+ const innerIndexType = instantiateType(c, t.constraintType, mapper);
1672
+ if ((innerIndexType.flags & 2097152 /* TypeFlags.Index */) === 0)
1673
+ return t;
1674
+ const inferred = inferTypeForHomomorphicMappedType(c, instantiateType(c, t.source, mapper), innerMappedType, innerIndexType);
1675
+ return inferred === undefined ? t : inferred;
1676
+ }
1677
+ // ---- Inferred types
1678
+ /** Infers early from intra-expression sites (context-sensitive elements of object and array literals). */
1679
+ export function inferFromIntraExpressionSites(c, context) {
1680
+ const sites = context.intraExpressionInferenceSites;
1681
+ context.intraExpressionInferenceSites = [];
1682
+ for (const site of sites) {
1683
+ const contextualType = getContextualTypeForInference(c, site.node);
1684
+ if (contextualType !== undefined)
1685
+ inferTypes(c, context.inferences, site.type, contextualType, 0 /* InferencePriority.None */, false);
1686
+ }
1687
+ }
1688
+ export function getInferredType(c, context, index) {
1689
+ const inference = context.inferences[index];
1690
+ if (inference.inferredType !== undefined)
1691
+ return inference.inferredType;
1692
+ if (inference.typeParameter === c.errorType)
1693
+ return inference.typeParameter;
1694
+ let inferredType = undefined;
1695
+ let fallbackType = undefined;
1696
+ const signature = context.signature;
1697
+ if (signature !== undefined) {
1698
+ const covariant = inference.candidates.length !== 0 ? getCovariantInference(c, inference, signature) : undefined;
1699
+ const contravariant = inference.contraCandidates.length !== 0 ? getContravariantInference(c, inference) : undefined;
1700
+ if (covariant !== undefined || contravariant !== undefined) {
1701
+ // Prefer the covariant inference when it isn't never or any, is assignable to some
1702
+ // contravariant candidate, and no type parameter constrained to this one conflicts.
1703
+ const preferCovariantType = covariant !== undefined &&
1704
+ (contravariant === undefined ||
1705
+ ((covariant.flags & (262144 /* TypeFlags.Never */ | 1 /* TypeFlags.Any */)) === 0 &&
1706
+ someTypeAssignableFrom(c, covariant, inference.contraCandidates) &&
1707
+ noConflictingConstrainedInference(c, context, inference, covariant)));
1708
+ if (preferCovariantType) {
1709
+ inferredType = covariant;
1710
+ fallbackType = contravariant;
1711
+ }
1712
+ else {
1713
+ inferredType = contravariant;
1714
+ fallbackType = covariant;
1715
+ }
1716
+ }
1717
+ else if ((context.flags & 1 /* InferenceFlags.NoDefault */) !== 0) {
1718
+ // silentNeverType signals no inferences.
1719
+ inferredType = c.silentNeverType;
1720
+ }
1721
+ else {
1722
+ // No inferences: the default, with forward references to later type parameters erased.
1723
+ const defaultType = getDefaultFromTypeParameter(c, inference.typeParameter);
1724
+ if (defaultType !== undefined)
1725
+ inferredType = instantiateType(c, defaultType, new MergedTypeMapper(newBackreferenceMapper(c, context, index), context.nonFixingMapper));
1726
+ }
1727
+ }
1728
+ else
1729
+ inferredType = getTypeFromInference(c, inference);
1730
+ inference.inferredType =
1731
+ inferredType ?? ((context.flags & 2 /* InferenceFlags.AnyDefault */) !== 0 ? c.anyType : c.unknownType);
1732
+ const constraint = getConstraintOfTypeParameter(c, inference.typeParameter);
1733
+ if (constraint !== undefined) {
1734
+ const instantiatedConstraint = instantiateType(c, constraint, context.nonFixingMapper);
1735
+ if (inferredType !== undefined) {
1736
+ const constraintWithThis = getTypeWithThisArgument(c, instantiatedConstraint, inferredType, false);
1737
+ if (context.compareTypes.compare(inferredType, constraintWithThis, false) === 0 /* Ternary.False */) {
1738
+ // A pure return type inference may still succeed without the constituents the constraint rejects.
1739
+ let filtered = undefined;
1740
+ if (inference.priority === 128 /* InferencePriority.ReturnType */)
1741
+ filtered = mapType(c, inferredType, (t) => context.compareTypes.compare(t, constraintWithThis, false) !== 0 /* Ternary.False */ ? t : c.neverType);
1742
+ inferredType = filtered !== undefined && (filtered.flags & 262144 /* TypeFlags.Never */) === 0 ? filtered : undefined;
1743
+ }
1744
+ }
1745
+ if (inferredType === undefined)
1746
+ inferredType =
1747
+ fallbackType !== undefined &&
1748
+ context.compareTypes.compare(fallbackType, getTypeWithThisArgument(c, instantiatedConstraint, fallbackType, false), false) !== 0 /* Ternary.False */
1749
+ ? fallbackType
1750
+ : instantiatedConstraint;
1751
+ inference.inferredType = inferredType;
1752
+ }
1753
+ clearActiveMapperCaches(c);
1754
+ return inference.inferredType;
1755
+ }
1756
+ function clearActiveMapperCaches(c) {
1757
+ for (const cache of c.activeMapperCaches)
1758
+ if (cache !== undefined)
1759
+ cache.clear();
1760
+ for (const cache of c.activeMapperAliasCaches)
1761
+ if (cache !== undefined)
1762
+ cache.clear();
1763
+ }
1764
+ /** Type ids below this bound pack in pairs into one exact number. */
1765
+ const VisitKeyIdLimit = 1 << 26;
1766
+ /** invokeOnce's key for a pair of types: both ids in one number, or -1 when an id is too large. */
1767
+ function visitKey(source, target) {
1768
+ const s = source.id;
1769
+ const t = target.id;
1770
+ return s < VisitKeyIdLimit && t < VisitKeyIdLimit ? s * VisitKeyIdLimit + t : -1;
1771
+ }
1772
+ /** Maps forward references to later type parameters to `unknown`, for instantiating defaults. */
1773
+ function newBackreferenceMapper(c, context, index) {
1774
+ return new ArrayToSingleTypeMapper(context.inferences.slice(index).map((i) => i.typeParameter), c.unknownType);
1775
+ }
1776
+ export function getInferredTypes(c, context) {
1777
+ const result = [];
1778
+ for (let i = 0; i < context.inferences.length; i++)
1779
+ result.push(getInferredType(c, context, i));
1780
+ return result;
1781
+ }
1782
+ function getCovariantInference(c, inference, signature) {
1783
+ // Object and array literal candidates become one widened, normalized type.
1784
+ const candidates = unionObjectAndArrayLiteralCandidates(c, inference.candidates);
1785
+ // Widen literals when all inferences were to top-level occurrences, the type parameter has no
1786
+ // primitive constraint, and it was fixed or doesn't occur at top level in the return type.
1787
+ const primitiveConstraint = hasPrimitiveConstraint(c, inference.typeParameter) || isConstTypeVariable(c, inference.typeParameter, 0);
1788
+ const widenLiteralTypes = !primitiveConstraint &&
1789
+ inference.topLevel &&
1790
+ (inference.isFixed || !isTypeParameterAtTopLevelInReturnType(c, signature, inference.typeParameter));
1791
+ let baseCandidates = candidates;
1792
+ if (primitiveConstraint || widenLiteralTypes) {
1793
+ baseCandidates = [];
1794
+ for (const t of candidates)
1795
+ baseCandidates.push(primitiveConstraint ? getRegularTypeOfLiteralType(c, t) : getWidenedLiteralType(c, t));
1796
+ }
1797
+ // Positions implying a combined result infer a union, others a common supertype.
1798
+ const unwidenedType = (inference.priority & 416 /* InferencePriority.PriorityImpliesCombination */) !== 0
1799
+ ? getUnionTypeEx(c, baseCandidates, 2 /* UnionReduction.Subtype */, undefined, undefined)
1800
+ : getCommonSupertype(c, baseCandidates);
1801
+ return getWidenedType(c, unwidenedType);
1802
+ }
1803
+ function getContravariantInference(c, inference) {
1804
+ return (inference.priority & 416 /* InferencePriority.PriorityImpliesCombination */) !== 0
1805
+ ? getIntersectionType(c, inference.contraCandidates)
1806
+ : getCommonSubtype(c, inference.contraCandidates);
1807
+ }
1808
+ function someTypeAssignableFrom(c, source, targets) {
1809
+ for (const t of targets)
1810
+ if (isTypeAssignableTo(c, source, t))
1811
+ return true;
1812
+ return false;
1813
+ }
1814
+ /** No other inference constrained to `inference`'s type parameter has a candidate not assignable to `covariant`. */
1815
+ function noConflictingConstrainedInference(c, context, inference, covariant) {
1816
+ for (const other of context.inferences) {
1817
+ if (other !== inference && getConstraintOfTypeParameter(c, other.typeParameter) !== inference.typeParameter)
1818
+ continue;
1819
+ for (const t of other.candidates)
1820
+ if (!isTypeAssignableTo(c, t, covariant))
1821
+ return false;
1822
+ }
1823
+ return true;
1824
+ }
1825
+ function everyNonGenericObjectType(c, types) {
1826
+ for (const t of types)
1827
+ if (!isNonGenericObjectType(c, t))
1828
+ return false;
1829
+ return true;
1830
+ }
1831
+ function isObjectOrArrayLiteralType(t) {
1832
+ return (t.objectFlags & (128 /* ObjectFlags.ObjectLiteral */ | 16384 /* ObjectFlags.ArrayLiteral */)) !== 0;
1833
+ }
1834
+ function unionObjectAndArrayLiteralCandidates(c, candidates) {
1835
+ if (candidates.length > 1) {
1836
+ const objectLiterals = [];
1837
+ const result = [];
1838
+ for (const t of candidates) {
1839
+ if (isObjectOrArrayLiteralType(t))
1840
+ objectLiterals.push(t);
1841
+ else
1842
+ result.push(t);
1843
+ }
1844
+ if (objectLiterals.length !== 0) {
1845
+ result.push(getUnionTypeEx(c, objectLiterals, 2 /* UnionReduction.Subtype */, undefined, undefined));
1846
+ return result;
1847
+ }
1848
+ }
1849
+ return candidates;
1850
+ }
1851
+ function hasPrimitiveConstraint(c, t) {
1852
+ let constraint = getConstraintOfTypeParameter(c, t);
1853
+ if (constraint === undefined)
1854
+ return false;
1855
+ if ((constraint.flags & 67108864 /* TypeFlags.Conditional */) !== 0)
1856
+ constraint = getConstraintFromConditionalType(c, constraint);
1857
+ return maybeTypeOfKind(constraint, 12713980 /* TypeFlags.Primitive */ | 2097152 /* TypeFlags.Index */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */);
1858
+ }
1859
+ export function getTypeFromInference(c, inference) {
1860
+ if (inference.candidates.length !== 0)
1861
+ return getUnionTypeEx(c, inference.candidates, 2 /* UnionReduction.Subtype */, undefined, undefined);
1862
+ if (inference.contraCandidates.length !== 0)
1863
+ return getIntersectionType(c, inference.contraCandidates);
1864
+ return undefined;
1865
+ }
1866
+ export function getCommonSupertype(c, types) {
1867
+ if (types.length === 1)
1868
+ return types[0];
1869
+ // Remove nullable types, find the supertype, and add them back.
1870
+ let primaryTypes = types;
1871
+ if (c.strictNullChecks) {
1872
+ primaryTypes = [];
1873
+ for (const t of types)
1874
+ primaryTypes.push(filterType(c, t, (u) => (u.flags & 12 /* TypeFlags.Nullable */) === 0));
1875
+ }
1876
+ const supertype = literalTypesWithSameBaseType(c, primaryTypes)
1877
+ ? getUnionType(c, primaryTypes)
1878
+ : getSingleCommonSupertype(c, primaryTypes);
1879
+ let same = true;
1880
+ for (let i = 0; i < types.length; i++)
1881
+ if (primaryTypes[i] !== types[i])
1882
+ same = false;
1883
+ if (same)
1884
+ return supertype;
1885
+ return getNullableType(c, supertype, getCombinedTypeFlags(types) & 12 /* TypeFlags.Nullable */);
1886
+ }
1887
+ function getSingleCommonSupertype(c, types) {
1888
+ // The leftmost type no type to its right is a strict supertype of, if it's a strict supertype of
1889
+ // all others; otherwise the same with regular supertypes.
1890
+ let candidate = undefined;
1891
+ for (const t of types)
1892
+ if (candidate === undefined || isTypeStrictSubtypeOf(c, candidate, t))
1893
+ candidate = t;
1894
+ let strictSupertype = true;
1895
+ for (const t of types) {
1896
+ if (t !== candidate && !isTypeStrictSubtypeOf(c, t, candidate)) {
1897
+ strictSupertype = false;
1898
+ break;
1899
+ }
1900
+ }
1901
+ if (strictSupertype)
1902
+ return candidate;
1903
+ candidate = undefined;
1904
+ for (const t of types)
1905
+ if (candidate === undefined || isTypeSubtypeOf(c, candidate, t))
1906
+ candidate = t;
1907
+ return candidate;
1908
+ }
1909
+ /** The leftmost type no type to its right is a subtype of. */
1910
+ export function getCommonSubtype(c, types) {
1911
+ let subtype = undefined;
1912
+ for (const t of types)
1913
+ if (subtype === undefined || isTypeSubtypeOf(c, t, subtype))
1914
+ subtype = t;
1915
+ return subtype;
1916
+ }
1917
+ function getCombinedTypeFlags(types) {
1918
+ let flags = 0 /* TypeFlags.None */;
1919
+ for (const t of types)
1920
+ flags |= (t.flags & 134217728 /* TypeFlags.Union */) !== 0 ? getCombinedTypeFlags(typesOf(t)) : t.flags;
1921
+ return flags;
1922
+ }
1923
+ function literalTypesWithSameBaseType(c, types) {
1924
+ let commonBaseType = undefined;
1925
+ for (const t of types) {
1926
+ if ((t.flags & 262144 /* TypeFlags.Never */) !== 0)
1927
+ continue;
1928
+ const baseType = getBaseTypeOfLiteralType(c, t);
1929
+ if (commonBaseType === undefined)
1930
+ commonBaseType = baseType;
1931
+ if (baseType === t || baseType !== commonBaseType)
1932
+ return false;
1933
+ }
1934
+ return true;
1935
+ }
1936
+ // ---- Conditional types
1937
+ /** A mapper from a conditional type's `infer` type parameters to their inferences. */
1938
+ export function inferTypesForConditional(c, root, checkType, extendsType, mapper, checkTypeDeferred) {
1939
+ // The constraints of `infer` type parameters may refer to other roots or to what `mapper` maps,
1940
+ // so the non-fixing mapper combines with `mapper`.
1941
+ const context = createInferenceContext(c, root.inferTypeParameters, undefined, 0 /* InferenceFlags.None */, undefined);
1942
+ if (mapper !== undefined)
1943
+ context.nonFixingMapper = combineTypeMappers(c, context.nonFixingMapper, mapper);
1944
+ // Inferences from constraints could eagerly resolve what should defer; contravariance is always strict.
1945
+ if (!checkTypeDeferred)
1946
+ inferTypes(c, context.inferences, checkType, extendsType, 512 /* InferencePriority.NoConstraints */ | 1024 /* InferencePriority.AlwaysStrict */, false);
1947
+ return context.mapper;
1948
+ }
1949
+ /** Caches and stacks for inference, contextual typing and call resolution. */
1950
+ export class InferenceCaches {
1951
+ /**
1952
+ * Contextual types pushed for nodes being checked, innermost last. The first `contextualDepth`
1953
+ * entries are live; popping keeps the slots after them for reuse.
1954
+ */
1955
+ contextualNodes = [];
1956
+ contextualTypes = [];
1957
+ contextualIsCache = [];
1958
+ contextualDepth = 0;
1959
+ /** Inference contexts pushed for arguments being checked, innermost last; `inferenceDepth` are live. */
1960
+ inferenceNodes = [];
1961
+ inferenceContexts = [];
1962
+ inferenceDepth = 0;
1963
+ optionalCallSignatures = new Map();
1964
+ discriminatedContextualTypes = new Map();
1965
+ /** `discriminatedContextualTypes` by node and type id packed into one number, when they fit. */
1966
+ discriminatedContextualTypesById = new Map();
1967
+ /** For union contextual types, what each constituent has for a missing optional property (discriminateTypeByObjectLiteral). */
1968
+ missingPropertyStates = new Map();
1969
+ contextFreeTypes = new Map();
1970
+ reverseMapped = new Map();
1971
+ reverseHomomorphicMapped = new Map();
1972
+ /** inferReverseMappedType's recursion stacks; the first `reverseStackDepth` entries of each are live. */
1973
+ reverseSourceStack = [];
1974
+ reverseTargetStack = [];
1975
+ reverseStackDepth = 0;
1976
+ reverseExpandingFlags = 0 /* ExpandingFlags.None */;
1977
+ }
1978
+ // ---- Constraints of `infer` type parameters
1979
+ /**
1980
+ * Maps the type parameters of a type reference to its type arguments, resolving them (and filling
1981
+ * in defaults) only when a type parameter is mapped.
1982
+ */
1983
+ class TypeArgumentMapper extends TypeMapper {
1984
+ c;
1985
+ typeArgumentNodes;
1986
+ typeParameters;
1987
+ filled = undefined;
1988
+ constructor(c, typeArgumentNodes, typeParameters) {
1989
+ super();
1990
+ this.c = c;
1991
+ this.typeArgumentNodes = typeArgumentNodes;
1992
+ this.typeParameters = typeParameters;
1993
+ }
1994
+ map(t) {
1995
+ const index = this.typeParameters.indexOf(t);
1996
+ if (index < 0)
1997
+ return t;
1998
+ if (index < this.typeArgumentNodes.length)
1999
+ return getTypeFromTypeNode(this.c, this.typeArgumentNodes[index]);
2000
+ if (this.filled === undefined) {
2001
+ const typeArguments = [];
2002
+ for (const n of this.typeArgumentNodes)
2003
+ typeArguments.push(getTypeFromTypeNode(this.c, n));
2004
+ this.filled = fillMissingTypeArguments(this.c, typeArguments, this.typeParameters, getMinTypeArgumentCount(this.c, this.typeParameters), false);
2005
+ }
2006
+ return this.filled[index];
2007
+ }
2008
+ }
2009
+ /**
2010
+ * The constraint an `infer T` position implies, e.g. `unknown[]` in a rest position; with
2011
+ * `omitTypeReferences`, `Foo<infer T>` implies none.
2012
+ */
2013
+ export function getInferredTypeParameterConstraint(c, t, omitTypeReferences = false) {
2014
+ const inferences = [];
2015
+ const symbol = t.symbol;
2016
+ if (symbol !== undefined)
2017
+ for (const declaration of symbol.declarations) {
2018
+ if (declaration.parent === undefined || declaration.parent.kind !== 196 /* SyntaxKind.InferType */)
2019
+ continue;
2020
+ let child = declaration.parent;
2021
+ let parent = child.parent;
2022
+ while (parent !== undefined && parent.kind === 197 /* SyntaxKind.ParenthesizedType */) {
2023
+ child = parent;
2024
+ parent = child.parent;
2025
+ }
2026
+ if (parent === undefined)
2027
+ continue;
2028
+ switch (parent.kind) {
2029
+ case 184 /* SyntaxKind.TypeReference */: {
2030
+ if (omitTypeReferences)
2031
+ break;
2032
+ // `Foo<infer T>` takes the constraint of Foo's corresponding type parameter.
2033
+ const typeArgumentNodes = parent.typeArguments;
2034
+ const typeParameters = getTypeParametersForTypeReference(c, parent);
2035
+ if (typeArgumentNodes === undefined || typeParameters === undefined)
2036
+ break;
2037
+ const index = typeArgumentNodes.nodes.indexOf(child);
2038
+ if (index < 0 || index >= typeParameters.length)
2039
+ break;
2040
+ const declaredConstraint = getConstraintOfTypeParameter(c, typeParameters[index]);
2041
+ if (declaredConstraint === undefined)
2042
+ break;
2043
+ // Constraints referencing the type parameter itself are discarded.
2044
+ const constraint = instantiateType(c, declaredConstraint, new TypeArgumentMapper(c, typeArgumentNodes.nodes, typeParameters));
2045
+ if (constraint !== t)
2046
+ inferences.push(constraint);
2047
+ break;
2048
+ }
2049
+ case 170 /* SyntaxKind.Parameter */:
2050
+ if (parent.dotDotDotToken !== undefined)
2051
+ inferences.push(createArrayType(c, c.unknownType));
2052
+ break;
2053
+ case 192 /* SyntaxKind.RestType */:
2054
+ inferences.push(createArrayType(c, c.unknownType));
2055
+ break;
2056
+ case 203 /* SyntaxKind.NamedTupleMember */:
2057
+ if (parent.dotDotDotToken !== undefined)
2058
+ inferences.push(createArrayType(c, c.unknownType));
2059
+ break;
2060
+ case 205 /* SyntaxKind.TemplateLiteralTypeSpan */:
2061
+ inferences.push(c.stringType);
2062
+ break;
2063
+ case 169 /* SyntaxKind.TypeParameter */:
2064
+ if (parent.parent !== undefined && parent.parent.kind === 201 /* SyntaxKind.MappedType */)
2065
+ inferences.push(c.stringNumberSymbolType);
2066
+ break;
2067
+ case 201 /* SyntaxKind.MappedType */: {
2068
+ // `{ [P in K]: X } extends { [Q in L]: infer U }` constrains U to X with P replaced by K.
2069
+ const mapped = parent;
2070
+ const conditional = parent.parent;
2071
+ if (mapped.type !== undefined &&
2072
+ mapped.type === declaration.parent &&
2073
+ conditional !== undefined &&
2074
+ conditional.kind === 195 /* SyntaxKind.ConditionalType */ &&
2075
+ conditional.extendsType === parent &&
2076
+ conditional.checkType.kind === 201 /* SyntaxKind.MappedType */ &&
2077
+ conditional.checkType.type !== undefined) {
2078
+ const checkMappedType = conditional.checkType;
2079
+ const nodeType = getTypeFromTypeNode(c, checkMappedType.type);
2080
+ const checkMappedTypeParameter = checkMappedType.typeParameter;
2081
+ const mapper = new SimpleTypeMapper(getDeclaredTypeOfTypeParameter(c, getSymbolOfDeclaration(c, checkMappedTypeParameter)), checkMappedTypeParameter.constraint !== undefined
2082
+ ? getTypeFromTypeNode(c, checkMappedTypeParameter.constraint)
2083
+ : c.stringNumberSymbolType);
2084
+ inferences.push(instantiateType(c, nodeType, mapper));
2085
+ }
2086
+ break;
2087
+ }
2088
+ }
2089
+ }
2090
+ return inferences.length !== 0 ? getIntersectionType(c, inferences) : undefined;
2091
+ }
2092
+ function getTypeParametersForTypeReference(c, node) {
2093
+ if (isErrorType(c, getTypeFromTypeNode(c, node)))
2094
+ return undefined;
2095
+ const symbol = c.resolvedSymbols.get(node.id);
2096
+ if (symbol === undefined || symbol === c.unknownSymbol)
2097
+ return undefined;
2098
+ if ((symbol.flags & (32 /* SymbolFlags.Class */ | 64 /* SymbolFlags.Interface */)) !== 0)
2099
+ return getDeclaredTypeOfClassOrInterface(c, getMergedSymbol(c, symbol)).localTypeParameters();
2100
+ if ((symbol.flags & 524288 /* SymbolFlags.TypeAlias */) !== 0) {
2101
+ getDeclaredTypeOfSymbol(c, symbol);
2102
+ return c.symbolLinks(symbol).typeParameters;
2103
+ }
2104
+ return undefined;
2105
+ }