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,2797 @@
1
+ // Control flow analysis: the type of a reference at a position, walking the binder's flow graph
2
+ // backwards from the reference through assignments, conditions, calls and labels.
3
+ import { sideOf } from './side-state.js';
4
+ import { getName, getExpression, getFlowNode, getInitializer, getText, NodeVisitor, getArguments, getDotDotDotToken, getParameters, getElements, getType, getStatements, } from './ast.js';
5
+ import { cachedTypeKey, cachedTypePairKey, } from './checker-types.js';
6
+ import { getSymbolId } from './symbol.js';
7
+ import { getNodeId } from './ast.js';
8
+ import { getAdjustedTypeWithFacts, getNonNullableType, getTypeFacts, getTypeWithFacts, hasTypeFacts, } from './checker-flow.js';
9
+ import { everyType, filterType, getBaseTypeOfLiteralType, getFreshTypeOfLiteralType, getIntersectionType, getRegularTypeOfLiteralType, getUnionType, getUnionTypeEx, getWidenedLiteralType, isEmptyAnonymousObjectType, isFreshLiteralType, isLiteralType, isPatternLiteralType, isUnitType, mapType, removeType, someType, typesOf, containsMissingType, getNumberLiteralType, createArrayType, isTupleType, newObjectType, } from './checker-type-factory.js';
10
+ import { areTypesComparable, isDiscriminantProperty, isTypeAssignableTo, isTypeDerivedFrom, isTypeRelatedTo, isTypeStrictSubtypeOf, isTypeSubsetOf, isTypeSubtypeOf, } from './relations.js';
11
+ import { checkExpression, checkExpressionCached, checkExpressionEx, checkNonNullExpression, checkNonNullType, getNarrowableTypeForReference, someTypeIsGenericWithUnionConstraint, getResolvedSymbol, getTypeOfExpression, isThisInTypeQuery, isTupleLikeType, } from './checker-expressions.js';
12
+ import { isBlockScopedNameDeclaredBeforeUse } from './checker-declaration-order.js';
13
+ import { getContextualSignature, getInferenceContext, isContextSensitiveFunctionOrObjectLiteralMethod, } from './checker-contextual.js';
14
+ import { instantiateType } from './instantiation.js';
15
+ import { checkIteratedTypeOrElementType } from './checker-iteration.js';
16
+ import { checkSuperExpression } from './checker-super.js';
17
+ import { getSymbolNameForPrivateIdentifier } from './binder.js';
18
+ import { isTypeAssignableToKindEx } from './checker-type-operators.js';
19
+ import { getRegularTypeOfObjectLiteral } from './checker-literals.js';
20
+ import { checkRightHandSideOfForOf } from './checker-statements.js';
21
+ import { getApparentType, getApplicableIndexInfoForName, getBaseConstraintOfType, getPropertiesOfType, getPropertyOfType, getReducedApparentType, getSignaturesOfType, getTypeOfPropertyOfType, mapToBaseConstraintOrType, } from './checker-members.js';
22
+ import { getAssignmentTarget, isAssignmentTarget, isPushOrUnshiftIdentifier, getCombinedModifierFlags, hasStaticModifier, getCombinedNodeFlags, getImmediatelyInvokedFunctionExpression, getRootDeclaration, getThisContainer, isAssignmentExpression, isClassLike, isEntityNameExpression, isObjectLiteralMethod, isExpressionOfOptionalChainRoot, isGlobalSourceFile, isLogicalOrCoalescingAssignmentOperator, isOptionalChain, isStatic, skipParentheses, getRangeOfTokenAtPosition, getSourceFileOfNode, } from './binder-utilities.js';
23
+ import { getDeclaredTypeOfSymbol, isConstructorType } from './checker-declared-types.js';
24
+ import { getErasedSignature, getReturnTypeFromAnnotation, getReturnTypeOfSignature, getSignatureFromDeclaration, getTypePredicateOfSignature, } from './checker-signatures.js';
25
+ import { getExportSymbolOfValueSymbolIfExported, getSymbolOfDeclaration, resolveEntityName, resolveSymbol, getGlobalTypeAliasSymbol, } from './checker-symbols.js';
26
+ import { getOptionalType, getTypeOfSymbol, isReadonlySymbol } from './checker-symbol-types.js';
27
+ import { getPropertyNameFromType, isTypeUsableAsPropertyName, maybeTypeOfKind } from './checker-utilities.js';
28
+ import { getTypeFromTypeNode, getTypeAliasInstantiation } from './checker-type-nodes.js';
29
+ import { isFunctionLike, isInJSFile } from './ast-utilities.js';
30
+ import { Diagnostic } from './diagnostics.js';
31
+ import { getOptionalExpressionType, getResolvedSignature } from './checker-calls.js';
32
+ import { getSymbolHasInstanceMethodOfObjectType } from './checker-operators.js';
33
+ import { getSymbolForPrivateIdentifierExpression } from './checker-property-access.js';
34
+ import { getIndexedAccessType, getLiteralTypeFromPropertyName, getTupleElementTypeOutOfStartCount, } from './checker-type-operators.js';
35
+ import { getBindingElementTypeFromParentType, getNonUndefinedType, getTypeForBindingElementParent, } from './checker-destructuring.js';
36
+ /**
37
+ * The number of shared nodes a walk caches before it indexes them. A walk through many consecutive
38
+ * conditions on one reference caches a node for each, and searching them all for each node made
39
+ * such walks cubic in the number of conditions. A walk's own entries stay in the cache until it
40
+ * ends, so a walk with at least this many is indexed.
41
+ */
42
+ const sharedFlowIndexThreshold = 128;
43
+ /** Indexes the entries of walk `f` in the shared flow cache by their first occurrence. */
44
+ function indexSharedFlows(c, f, walkDepth) {
45
+ while (c.sharedFlowIndexes.length <= walkDepth)
46
+ c.sharedFlowIndexes.push(new Map());
47
+ const index = c.sharedFlowIndexes[walkDepth];
48
+ index.clear();
49
+ for (let i = f.sharedFlowStart; i < c.sharedFlowCount; i++) {
50
+ const node = c.sharedFlowNodes[i];
51
+ if (!index.has(node))
52
+ index.set(node, i);
53
+ }
54
+ }
55
+ /** The state of one `getFlowTypeOfReference` walk. `incomplete` describes the last returned type. */
56
+ export class FlowState {
57
+ reference;
58
+ declaredType;
59
+ initialType;
60
+ flowContainer;
61
+ refKey = undefined;
62
+ depth = 0;
63
+ sharedFlowStart = 0;
64
+ incomplete = false;
65
+ /** The flow nodes visited, and the deepest nesting of the walk. */
66
+ steps = 0;
67
+ maxDepth = 0;
68
+ /** Whether this walk may be kept and end on a kept one (`FlowWalk`), and whether it looked one up. */
69
+ keepsWalk = false;
70
+ lookedUp = false;
71
+ /** The reference's last long walk, which this walk may end on. */
72
+ walk = undefined;
73
+ /** The node where this walk would be kept from: see `flowWalkAnchorSteps`. */
74
+ anchor = undefined;
75
+ /** Whether the result depends on where the reference is, not only on its key. */
76
+ positional = false;
77
+ constructor(reference, declaredType, initialType, flowContainer) {
78
+ this.reference = reference;
79
+ this.declaredType = declaredType;
80
+ this.initialType = initialType;
81
+ this.flowContainer = flowContainer;
82
+ }
83
+ /** Prepares a free flow state for another walk. */
84
+ reset(reference, declaredType, initialType, flowContainer) {
85
+ this.reference = reference;
86
+ this.declaredType = declaredType;
87
+ this.initialType = initialType;
88
+ this.flowContainer = flowContainer;
89
+ this.refKey = undefined;
90
+ this.depth = 0;
91
+ this.sharedFlowStart = 0;
92
+ this.incomplete = false;
93
+ this.steps = 0;
94
+ this.maxDepth = 0;
95
+ this.keepsWalk = false;
96
+ this.lookedUp = false;
97
+ this.walk = undefined;
98
+ this.anchor = undefined;
99
+ this.positional = false;
100
+ }
101
+ }
102
+ /**
103
+ * A node of a reference's last long walk, and the walk's result. Without loops or reduce labels in
104
+ * effect, the type of a reference at a flow node doesn't change, so a later walk that reaches `start`
105
+ * ends there. In a long list of top-level declarations, each reference otherwise walks back over
106
+ * every one before it.
107
+ */
108
+ export class FlowWalk {
109
+ start;
110
+ type;
111
+ /** The deepest nesting of a walk from `start`, which reaches the depth limit the same way. */
112
+ depth;
113
+ constructor(start, type, depth) {
114
+ this.start = start;
115
+ this.type = type;
116
+ this.depth = depth;
117
+ }
118
+ }
119
+ /** Walks shorter than this aren't kept. */
120
+ const minFlowWalkSteps = 32;
121
+ /**
122
+ * The step of a frame of a walk at which the walk looks up the reference's kept walk; in the first
123
+ * frame, also the node it would be kept from. Every node a frame passes before the last one leaves
124
+ * the type unchanged, so the type there is the walk's result. Shorter walks don't compute the
125
+ * reference's key. A later walk that passes this walk's start reaches the node after this many steps
126
+ * or more, once it has looked.
127
+ */
128
+ const flowWalkAnchorSteps = 16;
129
+ /**
130
+ * Whether `node` is an identifier, `this`, or property and element accesses on one. Walks of such
131
+ * references with the same key (`getFlowReferenceKey`) go the same way; with parentheses or a
132
+ * non-null assertion in between, a reference has the key of the one without, but not its walk.
133
+ */
134
+ function isDottedReference(node) {
135
+ let n = node;
136
+ for (;;) {
137
+ switch (n.kind) {
138
+ case 79 /* SyntaxKind.Identifier */:
139
+ return !isThisInTypeQuery(n);
140
+ case 109 /* SyntaxKind.ThisKeyword */:
141
+ return true;
142
+ case 212 /* SyntaxKind.PropertyAccessExpression */:
143
+ case 213 /* SyntaxKind.ElementAccessExpression */:
144
+ n = getExpression(n);
145
+ break;
146
+ default:
147
+ return false;
148
+ }
149
+ }
150
+ }
151
+ /**
152
+ * Whether an element access in `node` names a property with a character that keys use as a
153
+ * separator (`.`, `@`, `#`). Its key can then be another reference's, as `o["a.b"]` has the key of
154
+ * `o.a.b`, so its walks aren't kept.
155
+ */
156
+ function hasKeyCharacterInName(c, node) {
157
+ let n = node;
158
+ while (n.kind === 212 /* SyntaxKind.PropertyAccessExpression */ || n.kind === 213 /* SyntaxKind.ElementAccessExpression */) {
159
+ if (n.kind === 213 /* SyntaxKind.ElementAccessExpression */) {
160
+ const name = getAccessedPropertyName(c, n);
161
+ if (name !== undefined && /[.@#]/.test(name))
162
+ return true;
163
+ }
164
+ n = getExpression(n);
165
+ }
166
+ return false;
167
+ }
168
+ /** A loop junction being analyzed for a reference, with the types seen on its paths so far. */
169
+ export class FlowLoopInfo {
170
+ flow;
171
+ refKey;
172
+ types;
173
+ constructor(flow, refKey, types) {
174
+ this.flow = flow;
175
+ this.refKey = refKey;
176
+ this.types = types;
177
+ }
178
+ }
179
+ /** The type of `reference` at its flow position, narrowed from `declaredType` starting at `initialType`. */
180
+ export function getFlowTypeOfReference(c, reference, declaredType, initialType, flowContainer) {
181
+ return getFlowTypeOfReferenceAt(c, reference, declaredType, initialType, flowContainer, getFlowNode(reference));
182
+ }
183
+ /** `getFlowTypeOfReference` at `flowNode`, for pseudo-references such as binding patterns. */
184
+ export function getFlowTypeOfReferenceAt(c, reference, declaredType, initialType, flowContainer, flowNode) {
185
+ if (c.flowAnalysisDisabled)
186
+ return c.errorType;
187
+ if (flowNode === undefined)
188
+ return declaredType;
189
+ let f;
190
+ const stateDepth = c.flowStateDepth;
191
+ if (stateDepth < c.flowStates.length) {
192
+ f = c.flowStates[stateDepth];
193
+ f.reset(reference, declaredType, initialType, flowContainer);
194
+ }
195
+ else {
196
+ f = new FlowState(reference, declaredType, initialType, flowContainer);
197
+ c.flowStates.push(f);
198
+ }
199
+ c.flowStateDepth = stateDepth + 1;
200
+ f.sharedFlowStart = c.sharedFlowCount;
201
+ c.flowInvocationCount++;
202
+ f.keepsWalk =
203
+ c.flowLoopStack.length === 0 &&
204
+ c.flowReduceLabels.length === 0 &&
205
+ c.inlineLevel === 0 &&
206
+ isDottedReference(reference);
207
+ const cycles = c.resolutionCycleCount;
208
+ const evolvedType = getTypeAtFlowNode(c, f, flowNode);
209
+ const anchor = f.anchor;
210
+ if (anchor !== undefined &&
211
+ f.steps >= minFlowWalkSteps &&
212
+ !f.incomplete &&
213
+ !f.positional &&
214
+ !c.flowAnalysisDisabled &&
215
+ c.resolutionCycleCount === cycles) {
216
+ const walk = f.walk;
217
+ if (walk !== undefined) {
218
+ walk.start = anchor;
219
+ walk.type = evolvedType;
220
+ walk.depth = f.maxDepth;
221
+ }
222
+ else
223
+ c.flowWalks.set(f.refKey, new FlowWalk(anchor, evolvedType, f.maxDepth));
224
+ }
225
+ c.sharedFlowCount = f.sharedFlowStart;
226
+ c.flowStateDepth = stateDepth;
227
+ // `x` in `x.length`, `x.push(v)`, `x.unshift(v)` or `x[n] = v` stays `any[]` so empty arrays may be
228
+ // operated on and new element types inferred without implicit any or mismatch errors.
229
+ const resultType = (evolvedType.objectFlags & 256 /* ObjectFlags.EvolvingArray */) !== 0 && isEvolvingArrayOperationTarget(c, reference)
230
+ ? c.autoArrayType
231
+ : finalizeEvolvingArrayType(c, evolvedType);
232
+ if (resultType === c.unreachableNeverType ||
233
+ (reference.parent !== undefined &&
234
+ reference.parent.kind === 236 /* SyntaxKind.NonNullExpression */ &&
235
+ (resultType.flags & 262144 /* TypeFlags.Never */) === 0 &&
236
+ (getTypeWithFacts(c, resultType, 2097152 /* TypeFacts.NEUndefinedOrNull */).flags & 262144 /* TypeFlags.Never */) !== 0))
237
+ return declaredType;
238
+ return resultType;
239
+ }
240
+ function newFlowType(c, f, t, incomplete) {
241
+ f.incomplete = incomplete;
242
+ return incomplete && (t.flags & 262144 /* TypeFlags.Never */) !== 0 ? c.silentNeverType : t;
243
+ }
244
+ function complete(f, t) {
245
+ f.incomplete = false;
246
+ return t;
247
+ }
248
+ export function getTypeAtFlowNode(c, f, start) {
249
+ if (f.depth === 2000) {
250
+ c.flowAnalysisDisabled = true;
251
+ reportFlowControlError(c, f.reference);
252
+ return complete(f, c.errorType);
253
+ }
254
+ f.depth++;
255
+ if (f.depth > f.maxDepth)
256
+ f.maxDepth = f.depth;
257
+ let flow = start;
258
+ let sharedFlow = undefined;
259
+ let frameSteps = 0;
260
+ for (;;) {
261
+ f.steps++;
262
+ if (++frameSteps === flowWalkAnchorSteps && f.keepsWalk && !f.lookedUp) {
263
+ f.lookedUp = true;
264
+ if (f.refKey === undefined)
265
+ f.refKey = getFlowReferenceKey(c, f);
266
+ if (f.refKey !== '' && !hasKeyCharacterInName(c, f.reference)) {
267
+ f.walk = c.flowWalks.get(f.refKey);
268
+ // Only the first frame's nodes have the walk's result as their type.
269
+ if (f.depth === 1)
270
+ f.anchor = flow;
271
+ }
272
+ }
273
+ const walk = f.walk;
274
+ const flags = flow.flags;
275
+ if ((flags & 4096 /* FlowFlags.Shared */) !== 0) {
276
+ // Shared nodes are cached for the duration of one walk.
277
+ if (c.sharedFlowCount - f.sharedFlowStart >= sharedFlowIndexThreshold) {
278
+ const i = c.sharedFlowIndexes[c.flowStateDepth - 1].get(flow);
279
+ if (i !== undefined) {
280
+ f.depth--;
281
+ f.incomplete = c.sharedFlowIncomplete[i];
282
+ return c.sharedFlowTypes[i];
283
+ }
284
+ }
285
+ else
286
+ for (let i = f.sharedFlowStart; i < c.sharedFlowCount; i++) {
287
+ if (c.sharedFlowNodes[i] === flow) {
288
+ f.depth--;
289
+ f.incomplete = c.sharedFlowIncomplete[i];
290
+ return c.sharedFlowTypes[i];
291
+ }
292
+ }
293
+ sharedFlow = flow;
294
+ }
295
+ let t;
296
+ if (walk !== undefined &&
297
+ flow === walk.start &&
298
+ c.flowLoopStack.length === 0 &&
299
+ c.flowReduceLabels.length === 0 &&
300
+ c.inlineLevel === 0 &&
301
+ f.depth + walk.depth - 1 <= 2000) {
302
+ if (f.depth + walk.depth - 1 > f.maxDepth)
303
+ f.maxDepth = f.depth + walk.depth - 1;
304
+ t = complete(f, walk.type);
305
+ }
306
+ else if ((flags & 16 /* FlowFlags.Assignment */) !== 0) {
307
+ const assigned = getTypeAtFlowAssignment(c, f, flow);
308
+ if (assigned === undefined) {
309
+ flow = flow.antecedent;
310
+ continue;
311
+ }
312
+ t = assigned;
313
+ }
314
+ else if ((flags & 512 /* FlowFlags.Call */) !== 0) {
315
+ const narrowed = getTypeAtFlowCall(c, f, flow);
316
+ if (narrowed === undefined) {
317
+ flow = flow.antecedent;
318
+ continue;
319
+ }
320
+ t = narrowed;
321
+ }
322
+ else if ((flags & 96 /* FlowFlags.Condition */) !== 0)
323
+ t = getTypeAtFlowCondition(c, f, flow);
324
+ else if ((flags & 128 /* FlowFlags.SwitchClause */) !== 0)
325
+ t = getTypeAtSwitchClause(c, f, flow);
326
+ else if ((flags & 4 /* FlowFlags.BranchLabel */) !== 0) {
327
+ const antecedents = getBranchLabelAntecedents(c, flow);
328
+ if (antecedents.length === 1) {
329
+ flow = antecedents[0];
330
+ continue;
331
+ }
332
+ t = getTypeAtFlowBranchLabel(c, f, antecedents);
333
+ }
334
+ else if ((flags & 8 /* FlowFlags.LoopLabel */) !== 0) {
335
+ const antecedents = flow.antecedents;
336
+ if (antecedents.length === 1) {
337
+ flow = antecedents[0];
338
+ continue;
339
+ }
340
+ t = getTypeAtFlowLoopLabel(c, f, flow);
341
+ }
342
+ else if ((flags & 256 /* FlowFlags.ArrayMutation */) !== 0) {
343
+ const mutated = getTypeAtFlowArrayMutation(c, f, flow);
344
+ if (mutated === undefined) {
345
+ flow = flow.antecedent;
346
+ continue;
347
+ }
348
+ t = mutated;
349
+ }
350
+ else if ((flags & 1024 /* FlowFlags.ReduceLabel */) !== 0) {
351
+ c.flowReduceLabels.push(flow);
352
+ t = getTypeAtFlowNode(c, f, flow.antecedent);
353
+ c.flowReduceLabels.pop();
354
+ }
355
+ else if ((flags & 2 /* FlowFlags.Start */) !== 0) {
356
+ // Continue with the containing function's flow when the reference is captured.
357
+ const container = flow.node;
358
+ if (container !== undefined &&
359
+ container !== f.flowContainer &&
360
+ f.reference.kind !== 212 /* SyntaxKind.PropertyAccessExpression */ &&
361
+ f.reference.kind !== 213 /* SyntaxKind.ElementAccessExpression */ &&
362
+ !(f.reference.kind === 109 /* SyntaxKind.ThisKeyword */ && container.kind !== 220 /* SyntaxKind.ArrowFunction */)) {
363
+ const outer = getFlowNode(container);
364
+ if (outer !== undefined) {
365
+ flow = outer;
366
+ continue;
367
+ }
368
+ }
369
+ t = complete(f, f.initialType);
370
+ }
371
+ else {
372
+ // Unreachable code; errors are reported by the binder.
373
+ t = complete(f, convertAutoToAny(c, f.declaredType));
374
+ }
375
+ if (sharedFlow !== undefined) {
376
+ const count = c.sharedFlowCount;
377
+ if (count < c.sharedFlowNodes.length) {
378
+ c.sharedFlowNodes[count] = sharedFlow;
379
+ c.sharedFlowTypes[count] = t;
380
+ c.sharedFlowIncomplete[count] = f.incomplete;
381
+ }
382
+ else {
383
+ c.sharedFlowNodes.push(sharedFlow);
384
+ c.sharedFlowTypes.push(t);
385
+ c.sharedFlowIncomplete.push(f.incomplete);
386
+ }
387
+ c.sharedFlowCount = count + 1;
388
+ const own = count + 1 - f.sharedFlowStart;
389
+ if (own > sharedFlowIndexThreshold) {
390
+ const index = c.sharedFlowIndexes[c.flowStateDepth - 1];
391
+ if (!index.has(sharedFlow))
392
+ index.set(sharedFlow, count);
393
+ }
394
+ else if (own === sharedFlowIndexThreshold)
395
+ indexSharedFlows(c, f, c.flowStateDepth - 1);
396
+ }
397
+ f.depth--;
398
+ return t;
399
+ }
400
+ }
401
+ export function convertAutoToAny(c, t) {
402
+ if (t === c.autoType)
403
+ return c.anyType;
404
+ if (t === c.autoArrayType)
405
+ return c.anyArrayType;
406
+ return t;
407
+ }
408
+ /** A branch label's antecedents, as limited by the innermost reduce label being passed that targets it. */
409
+ export function getBranchLabelAntecedents(c, flow) {
410
+ const reduceLabels = c.flowReduceLabels;
411
+ for (let i = reduceLabels.length - 1; i >= 0; i--)
412
+ if (reduceLabels[i].target === flow)
413
+ return reduceLabels[i].antecedents;
414
+ return flow.antecedents;
415
+ }
416
+ // ---- Assignments and calls
417
+ function getTypeAtFlowAssignment(c, f, flow) {
418
+ const node = flow.node;
419
+ if (isMatchingReference(c, f.reference, node)) {
420
+ if (!isReachableFlowNode(c, flow))
421
+ return complete(f, c.unreachableNeverType);
422
+ if (getAssignmentTargetKind(node) === 2 /* AssignmentKind.Compound */) {
423
+ const flowType = getTypeAtFlowNode(c, f, flow.antecedent);
424
+ return newFlowType(c, f, getBaseTypeOfLiteralType(c, flowType), f.incomplete);
425
+ }
426
+ if (f.declaredType === c.autoType || f.declaredType === c.autoArrayType) {
427
+ if (isEmptyArrayAssignment(node))
428
+ return complete(f, getEvolvingArrayType(c, c.neverType));
429
+ const assignedType = getWidenedLiteralType(c, getInitialOrAssignedType(c, f, flow));
430
+ return complete(f, isTypeAssignableTo(c, assignedType, f.declaredType) ? assignedType : c.anyArrayType);
431
+ }
432
+ let t = f.declaredType;
433
+ if (isInCompoundLikeAssignment(node))
434
+ t = getBaseTypeOfLiteralType(c, t);
435
+ if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0)
436
+ return complete(f, getAssignmentReducedType(c, t, getInitialOrAssignedType(c, f, flow)));
437
+ return complete(f, t);
438
+ }
439
+ // An assignment to a prefix of a dotted name resets it to the declared type.
440
+ if (containsMatchingReference(c, f.reference, node)) {
441
+ if (!isReachableFlowNode(c, flow))
442
+ return complete(f, c.unreachableNeverType);
443
+ // The dotted name may be an expando property of a function expression; its flow continues
444
+ // back past the function's declaration.
445
+ if (node.kind === 261 /* SyntaxKind.VariableDeclaration */) {
446
+ const blockScope = getCombinedNodeFlags(node) & 7 /* NodeFlags.BlockScoped */;
447
+ const initializer = getInitializer(node);
448
+ if ((isInJSFile(node) ||
449
+ blockScope === 2 /* NodeFlags.Const */ ||
450
+ blockScope === 4 /* NodeFlags.Using */ ||
451
+ blockScope === 6 /* NodeFlags.AwaitUsing */) &&
452
+ initializer !== undefined &&
453
+ (initializer.kind === 219 /* SyntaxKind.FunctionExpression */ || initializer.kind === 220 /* SyntaxKind.ArrowFunction */))
454
+ return getTypeAtFlowNode(c, f, flow.antecedent);
455
+ }
456
+ return complete(f, f.declaredType);
457
+ }
458
+ // `for (const _ in ref)` acts as a non-null check of `ref`.
459
+ if (node.kind === 261 /* SyntaxKind.VariableDeclaration */ && node.parent.parent.kind === 250 /* SyntaxKind.ForInStatement */) {
460
+ const statementExpression = getExpression(node.parent.parent);
461
+ if (isMatchingReference(c, f.reference, statementExpression) ||
462
+ optionalChainContainsReference(c, statementExpression, f.reference)) {
463
+ const antecedentType = getTypeAtFlowNode(c, f, flow.antecedent);
464
+ const finalType = finalizeEvolvingArrayType(c, antecedentType);
465
+ return complete(f, hasTypeFacts(c, finalType, 50331648 /* TypeFacts.IsUndefinedOrNull */) ? getNonNullableType(c, finalType) : finalType);
466
+ }
467
+ }
468
+ return undefined;
469
+ }
470
+ function isEmptyArrayAssignment(node) {
471
+ if (node.kind === 261 /* SyntaxKind.VariableDeclaration */) {
472
+ const initializer = getInitializer(node);
473
+ return initializer !== undefined && isEmptyArrayLiteral(initializer);
474
+ }
475
+ const parent = node.parent;
476
+ return (node.kind !== 209 /* SyntaxKind.BindingElement */ &&
477
+ parent.kind === 227 /* SyntaxKind.BinaryExpression */ &&
478
+ isEmptyArrayLiteral(parent.right));
479
+ }
480
+ function isEmptyArrayLiteral(node) {
481
+ return node.kind === 210 /* SyntaxKind.ArrayLiteralExpression */ && getElements(node).nodes.length === 0;
482
+ }
483
+ function getInitialOrAssignedType(c, f, flow) {
484
+ const node = flow.node;
485
+ const t = node.kind === 261 /* SyntaxKind.VariableDeclaration */ || node.kind === 209 /* SyntaxKind.BindingElement */
486
+ ? getInitialType(c, node)
487
+ : getAssignedType(c, node);
488
+ // getNarrowableTypeForReference looks at the reference's position for these.
489
+ if (someTypeIsGenericWithUnionConstraint(c, t))
490
+ f.positional = true;
491
+ return getNarrowableTypeForReference(c, t, f.reference);
492
+ }
493
+ function getInitialType(c, node) {
494
+ if (node.kind === 261 /* SyntaxKind.VariableDeclaration */) {
495
+ const initializer = getInitializer(node);
496
+ if (initializer !== undefined)
497
+ return getTypeOfInitializer(c, initializer);
498
+ const statement = node.parent.parent;
499
+ if (statement.kind === 250 /* SyntaxKind.ForInStatement */)
500
+ return c.stringType;
501
+ if (statement.kind === 251 /* SyntaxKind.ForOfStatement */)
502
+ return checkRightHandSideOfForOf(c, statement);
503
+ return c.errorType;
504
+ }
505
+ return getInitialTypeOfBindingElement(c, node);
506
+ }
507
+ /** The type of an initializer: the type a variable was inferred from, else computed without caching. */
508
+ export function getTypeOfInitializer(c, node) {
509
+ return c.resolvedTypes.get(node.id) ?? getTypeOfExpression(c, node);
510
+ }
511
+ function getInitialTypeOfBindingElement(c, node) {
512
+ const pattern = node.parent;
513
+ const parentType = getInitialType(c, pattern.parent);
514
+ let t;
515
+ if (pattern.kind === 207 /* SyntaxKind.ObjectBindingPattern */)
516
+ t = getTypeOfDestructuredProperty(c, parentType, (node.propertyName ?? node.name));
517
+ else if (node.dotDotDotToken === undefined)
518
+ t = getTypeOfDestructuredArrayElement(c, parentType, getElements(pattern).nodes.indexOf(node));
519
+ else
520
+ t = getTypeOfDestructuredSpreadExpression(c, parentType);
521
+ return getTypeWithDefault(c, t, node.initializer);
522
+ }
523
+ function getAssignedType(c, node) {
524
+ const parent = node.parent;
525
+ switch (parent.kind) {
526
+ case 250 /* SyntaxKind.ForInStatement */:
527
+ return c.stringType;
528
+ case 251 /* SyntaxKind.ForOfStatement */:
529
+ return checkRightHandSideOfForOf(c, parent);
530
+ case 227 /* SyntaxKind.BinaryExpression */:
531
+ return getAssignedTypeOfBinaryExpression(c, parent);
532
+ case 221 /* SyntaxKind.DeleteExpression */:
533
+ return c.undefinedType;
534
+ case 210 /* SyntaxKind.ArrayLiteralExpression */:
535
+ return getTypeOfDestructuredArrayElement(c, getAssignedType(c, parent), getElements(parent).nodes.indexOf(node));
536
+ case 231 /* SyntaxKind.SpreadElement */:
537
+ return getTypeOfDestructuredSpreadExpression(c, getAssignedType(c, parent.parent));
538
+ case 303 /* SyntaxKind.PropertyAssignment */:
539
+ return getAssignedTypeOfPropertyAssignment(c, parent);
540
+ case 304 /* SyntaxKind.ShorthandPropertyAssignment */:
541
+ return getTypeWithDefault(c, getAssignedTypeOfPropertyAssignment(c, parent), parent.objectAssignmentInitializer);
542
+ }
543
+ return c.errorType;
544
+ }
545
+ function getAssignedTypeOfBinaryExpression(c, node) {
546
+ const parent = node.parent;
547
+ const isDestructuringDefaultAssignment = (parent.kind === 210 /* SyntaxKind.ArrayLiteralExpression */ && isDestructuringAssignmentTarget(parent)) ||
548
+ (parent.kind === 303 /* SyntaxKind.PropertyAssignment */ && isDestructuringAssignmentTarget(parent.parent));
549
+ if (isDestructuringDefaultAssignment)
550
+ return getTypeWithDefault(c, getAssignedType(c, node), node.right);
551
+ return getTypeOfExpression(c, node.right);
552
+ }
553
+ function isDestructuringAssignmentTarget(parent) {
554
+ const grandparent = parent.parent;
555
+ return ((grandparent.kind === 227 /* SyntaxKind.BinaryExpression */ && grandparent.left === parent) ||
556
+ (grandparent.kind === 251 /* SyntaxKind.ForOfStatement */ && grandparent.initializer === parent));
557
+ }
558
+ function getAssignedTypeOfPropertyAssignment(c, node) {
559
+ return getTypeOfDestructuredProperty(c, getAssignedType(c, node.parent), getName(node));
560
+ }
561
+ function getTypeOfDestructuredArrayElement(c, t, index) {
562
+ if (everyType(t, (u) => isTupleLikeType(c, u))) {
563
+ const elementType = getTupleElementType(c, t, index);
564
+ if (elementType !== undefined)
565
+ return elementType;
566
+ }
567
+ return includeUndefinedInIndexSignature(c, checkIteratedTypeOrElementType(c, 65 /* IterationUse.Destructuring */, t, c.undefinedType, undefined));
568
+ }
569
+ function getTupleElementType(c, t, index) {
570
+ const propType = getTypeOfPropertyOfType(c, t, String(index));
571
+ if (propType !== undefined)
572
+ return propType;
573
+ if (everyType(t, isTupleType))
574
+ return getTupleElementTypeOutOfStartCount(c, t, index, c.noUncheckedIndexedAccess ? c.undefinedType : undefined);
575
+ return undefined;
576
+ }
577
+ function includeUndefinedInIndexSignature(c, t) {
578
+ return c.noUncheckedIndexedAccess ? getUnionType(c, [t, c.missingType]) : t;
579
+ }
580
+ function getTypeOfDestructuredSpreadExpression(c, t) {
581
+ return createArrayType(c, checkIteratedTypeOrElementType(c, 65 /* IterationUse.Destructuring */, t, c.undefinedType, undefined));
582
+ }
583
+ function getTypeOfDestructuredProperty(c, t, name) {
584
+ const nameType = getLiteralTypeFromPropertyName(c, name);
585
+ if (nameType === undefined || !isTypeUsableAsPropertyName(nameType))
586
+ return c.errorType;
587
+ const text = getPropertyNameFromType(nameType);
588
+ const propType = getTypeOfPropertyOfType(c, t, text);
589
+ if (propType !== undefined)
590
+ return propType;
591
+ const indexInfo = getApplicableIndexInfoForName(c, t, text);
592
+ if (indexInfo !== undefined)
593
+ return includeUndefinedInIndexSignature(c, indexInfo.valueType);
594
+ return c.errorType;
595
+ }
596
+ function getTypeWithDefault(c, t, defaultExpression) {
597
+ return defaultExpression !== undefined
598
+ ? getUnionType(c, [getNonUndefinedType(c, t), getTypeOfExpression(c, defaultExpression)])
599
+ : t;
600
+ }
601
+ /** The constituents of `declaredType` that some constituent of `assignedType` is assignable to. */
602
+ export function getAssignmentReducedType(c, declaredType, assignedType) {
603
+ if (declaredType === assignedType)
604
+ return declaredType;
605
+ if ((assignedType.flags & 262144 /* TypeFlags.Never */) !== 0)
606
+ return assignedType;
607
+ const pair = cachedTypePairKey(declaredType.id, assignedType.id, 0);
608
+ const key = pair < 0 ? 'assignmentReduced' + declaredType.id + ',' + assignedType.id : '';
609
+ let result = pair < 0 ? c.cachedTypes.get(key) : c.cachedTypesByPair.get(pair);
610
+ if (result === undefined) {
611
+ const filteredType = filterType(c, declaredType, (t) => typeMaybeAssignableTo(c, assignedType, t));
612
+ let reducedType = filteredType;
613
+ if ((assignedType.flags & 8192 /* TypeFlags.BooleanLiteral */) !== 0 && isFreshLiteralType(assignedType))
614
+ reducedType = mapType(c, filteredType, (t) => getFreshTypeOfLiteralType(c, t));
615
+ result = isTypeAssignableTo(c, assignedType, reducedType) ? reducedType : declaredType;
616
+ if (pair < 0)
617
+ c.cachedTypes.set(key, result);
618
+ else
619
+ c.cachedTypesByPair.set(pair, result);
620
+ }
621
+ return result;
622
+ }
623
+ function typeMaybeAssignableTo(c, source, target) {
624
+ if ((source.flags & 134217728 /* TypeFlags.Union */) === 0)
625
+ return isTypeAssignableTo(c, source, target);
626
+ for (const t of typesOf(source))
627
+ if (isTypeAssignableTo(c, t, target))
628
+ return true;
629
+ return false;
630
+ }
631
+ function getTypeAtFlowCall(c, f, flow) {
632
+ const call = flow.node;
633
+ const signature = getEffectsSignature(c, call);
634
+ if (signature === undefined)
635
+ return undefined;
636
+ const predicate = getTypePredicateOfSignature(c, signature);
637
+ if (predicate !== undefined &&
638
+ (predicate.kind === 2 /* TypePredicateKind.AssertsThis */ || predicate.kind === 3 /* TypePredicateKind.AssertsIdentifier */)) {
639
+ const flowType = getTypeAtFlowNode(c, f, flow.antecedent);
640
+ const incomplete = f.incomplete;
641
+ const t = finalizeEvolvingArrayType(c, flowType);
642
+ let narrowedType = t;
643
+ const args = getArguments(call).nodes;
644
+ if (predicate.type !== undefined)
645
+ narrowedType = narrowTypeByTypePredicate(c, f, t, predicate, call, true);
646
+ else if (predicate.kind === 3 /* TypePredicateKind.AssertsIdentifier */ &&
647
+ predicate.parameterIndex >= 0 &&
648
+ predicate.parameterIndex < args.length)
649
+ narrowedType = narrowTypeByAssertion(c, f, t, args[predicate.parameterIndex]);
650
+ if (narrowedType === t)
651
+ return newFlowType(c, f, flowType, incomplete);
652
+ return newFlowType(c, f, narrowedType, incomplete);
653
+ }
654
+ if ((getReturnTypeOfSignature(c, signature).flags & 262144 /* TypeFlags.Never */) !== 0)
655
+ return complete(f, c.unreachableNeverType);
656
+ return undefined;
657
+ }
658
+ function narrowTypeByAssertion(c, f, t, expr) {
659
+ const node = skipParentheses(expr);
660
+ if (node.kind === 96 /* SyntaxKind.FalseKeyword */)
661
+ return c.unreachableNeverType;
662
+ if (node.kind === 227 /* SyntaxKind.BinaryExpression */) {
663
+ const binary = node;
664
+ if (binary.operatorToken.kind === 55 /* SyntaxKind.AmpersandAmpersandToken */)
665
+ return narrowTypeByAssertion(c, f, narrowTypeByAssertion(c, f, t, binary.left), binary.right);
666
+ if (binary.operatorToken.kind === 56 /* SyntaxKind.BarBarToken */)
667
+ return getUnionType(c, [
668
+ narrowTypeByAssertion(c, f, t, binary.left),
669
+ narrowTypeByAssertion(c, f, t, binary.right),
670
+ ]);
671
+ }
672
+ return narrowType(c, f, t, node, true);
673
+ }
674
+ /** The signature of a call whose effects (type predicates, `never` returns) apply to control flow. */
675
+ export function getEffectsSignature(c, node) {
676
+ const links = c.nodeLinks(node);
677
+ let signature = links.effectsSignature;
678
+ if (signature === undefined) {
679
+ // Assertion calls resolve their target with getTypeOfDottedName to avoid circularities.
680
+ let funcType = undefined;
681
+ if (node.kind === 227 /* SyntaxKind.BinaryExpression */)
682
+ funcType = getSymbolHasInstanceMethodOfObjectType(c, checkNonNullExpression(c, node.right));
683
+ else {
684
+ const callee = getExpression(node);
685
+ // Assertions resolve their target without inference, to avoid circularities in flow analysis.
686
+ if (node.parent.kind === 245 /* SyntaxKind.ExpressionStatement */)
687
+ funcType = getTypeOfDottedName(c, callee);
688
+ else if (callee.kind !== 107 /* SyntaxKind.SuperKeyword */)
689
+ funcType = isOptionalChain(node)
690
+ ? checkNonNullType(c, getOptionalExpressionType(c, checkExpression(c, callee), callee), callee)
691
+ : checkNonNullExpression(c, callee);
692
+ }
693
+ const signatures = getSignaturesOfType(c, funcType !== undefined ? getApparentType(c, funcType) : c.unknownType, 0 /* SignatureKind.Call */);
694
+ let candidate = undefined;
695
+ if (signatures.length === 1 && signatures[0].typeParameters.length === 0)
696
+ candidate = signatures[0];
697
+ // Generic or overloaded predicates and assertions take effect through the resolved signature.
698
+ else if (someSignatureHasTypePredicateOrNeverReturnType(c, signatures))
699
+ candidate = getResolvedSignature(c, node, 0 /* CheckMode.Normal */);
700
+ signature =
701
+ candidate !== undefined && hasTypePredicateOrNeverReturnType(c, candidate) ? candidate : c.unknownSignature;
702
+ links.effectsSignature = signature;
703
+ }
704
+ return signature === c.unknownSignature ? undefined : signature;
705
+ }
706
+ function someSignatureHasTypePredicateOrNeverReturnType(c, signatures) {
707
+ for (const sig of signatures)
708
+ if (hasTypePredicateOrNeverReturnType(c, sig))
709
+ return true;
710
+ return false;
711
+ }
712
+ function hasTypePredicateOrNeverReturnType(c, sig) {
713
+ if (getTypePredicateOfSignature(c, sig) !== undefined)
714
+ return true;
715
+ if (sig.declaration === undefined)
716
+ return false;
717
+ const returnType = getReturnTypeFromAnnotation(c, sig.declaration);
718
+ return returnType !== undefined && (returnType.flags & 262144 /* TypeFlags.Never */) !== 0;
719
+ }
720
+ /** The declared type of a dotted name, without inference, for assertion calls. */
721
+ function getTypeOfDottedName(c, node) {
722
+ if ((node.flags & 16777216 /* NodeFlags.InWithStatement */) !== 0)
723
+ return undefined;
724
+ switch (node.kind) {
725
+ case 79 /* SyntaxKind.Identifier */:
726
+ return getExplicitTypeOfSymbol(c, getExportSymbolOfValueSymbolIfExported(c, getResolvedSymbol(c, node)));
727
+ case 109 /* SyntaxKind.ThisKeyword */:
728
+ return getExplicitThisType(c, node);
729
+ case 107 /* SyntaxKind.SuperKeyword */:
730
+ return checkSuperExpression(c, node);
731
+ case 212 /* SyntaxKind.PropertyAccessExpression */: {
732
+ const t = getTypeOfDottedName(c, getExpression(node));
733
+ if (t === undefined)
734
+ return undefined;
735
+ const name = node.name;
736
+ let prop;
737
+ if (name.kind === 80 /* SyntaxKind.PrivateIdentifier */)
738
+ prop =
739
+ t.symbol !== undefined
740
+ ? getPropertyOfType(c, t, getSymbolNameForPrivateIdentifier(t.symbol, getText(name)))
741
+ : undefined;
742
+ else
743
+ prop = getPropertyOfType(c, t, getText(name));
744
+ return prop !== undefined ? getExplicitTypeOfSymbol(c, prop) : undefined;
745
+ }
746
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
747
+ return getTypeOfDottedName(c, getExpression(node));
748
+ }
749
+ return undefined;
750
+ }
751
+ function getExplicitTypeOfSymbol(c, original) {
752
+ const symbol = resolveSymbol(c, original) ?? original;
753
+ if ((symbol.flags & (16 /* SymbolFlags.Function */ | 8192 /* SymbolFlags.Method */ | 32 /* SymbolFlags.Class */ | 512 /* SymbolFlags.ValueModule */)) !== 0)
754
+ return getTypeOfSymbol(c, symbol);
755
+ if ((symbol.flags & (3 /* SymbolFlags.Variable */ | 4 /* SymbolFlags.Property */)) !== 0) {
756
+ if ((symbol.checkFlags & 262144 /* CheckFlags.Mapped */) !== 0) {
757
+ const origin = c.symbolSyntheticOrigins.get(getSymbolId(symbol));
758
+ if (origin !== undefined && getExplicitTypeOfSymbol(c, origin) !== undefined)
759
+ return getTypeOfSymbol(c, symbol);
760
+ }
761
+ const declaration = symbol.valueDeclaration;
762
+ if (declaration !== undefined) {
763
+ if (isDeclarationWithExplicitTypeAnnotation(declaration))
764
+ return getTypeOfSymbol(c, symbol);
765
+ // `for (const x of xs)` where `xs` has an explicit type.
766
+ if (declaration.kind === 261 /* SyntaxKind.VariableDeclaration */ &&
767
+ declaration.parent.parent.kind === 251 /* SyntaxKind.ForOfStatement */) {
768
+ const statement = declaration.parent.parent;
769
+ const expressionType = getTypeOfDottedName(c, statement.expression);
770
+ if (expressionType !== undefined)
771
+ return checkIteratedTypeOrElementType(c, statement.awaitModifier !== undefined ? 15 /* IterationUse.ForAwaitOf */ : 13 /* IterationUse.ForOf */, expressionType, c.undefinedType, undefined);
772
+ }
773
+ }
774
+ }
775
+ return undefined;
776
+ }
777
+ function isDeclarationWithExplicitTypeAnnotation(node) {
778
+ switch (node.kind) {
779
+ case 261 /* SyntaxKind.VariableDeclaration */:
780
+ case 173 /* SyntaxKind.PropertyDeclaration */:
781
+ case 172 /* SyntaxKind.PropertySignature */:
782
+ case 170 /* SyntaxKind.Parameter */:
783
+ return getType(node) !== undefined;
784
+ case 227 /* SyntaxKind.BinaryExpression */: {
785
+ // An expando property assigned a function with a return type annotation.
786
+ const right = node.right;
787
+ return isFunctionLike(right) && getType(right) !== undefined;
788
+ }
789
+ }
790
+ return false;
791
+ }
792
+ function getExplicitThisType(c, node) {
793
+ const container = getThisContainer(node, false, false);
794
+ if (isFunctionLike(container)) {
795
+ const signature = getSignatureFromDeclaration(c, container);
796
+ if (signature.thisParameter !== undefined)
797
+ return getExplicitTypeOfSymbol(c, signature.thisParameter);
798
+ }
799
+ const parent = container.parent;
800
+ if (parent !== undefined && isClassLike(parent)) {
801
+ const symbol = getSymbolOfDeclaration(c, parent);
802
+ if (symbol === undefined)
803
+ return undefined;
804
+ return isStatic(container)
805
+ ? getTypeOfSymbol(c, symbol)
806
+ : getDeclaredTypeOfSymbol(c, symbol).thisType;
807
+ }
808
+ return undefined;
809
+ }
810
+ // ---- Conditions
811
+ function getTypeAtFlowCondition(c, f, flow) {
812
+ const flowType = getTypeAtFlowNode(c, f, flow.antecedent);
813
+ const incomplete = f.incomplete;
814
+ if ((flowType.flags & 262144 /* TypeFlags.Never */) !== 0)
815
+ return newFlowType(c, f, flowType, incomplete);
816
+ const assumeTrue = (flow.flags & 32 /* FlowFlags.TrueCondition */) !== 0;
817
+ const nonEvolvingType = finalizeEvolvingArrayType(c, flowType);
818
+ const narrowedType = narrowType(c, f, nonEvolvingType, flow.node, assumeTrue);
819
+ if (narrowedType === nonEvolvingType) {
820
+ f.incomplete = incomplete;
821
+ return flowType;
822
+ }
823
+ return newFlowType(c, f, narrowedType, incomplete);
824
+ }
825
+ function getTypeAtFlowBranchLabel(c, f, antecedents) {
826
+ const types = [];
827
+ let subtypeReduction = false;
828
+ let seenIncomplete = false;
829
+ let bypassFlow = undefined;
830
+ for (const antecedent of antecedents) {
831
+ if (bypassFlow === undefined &&
832
+ (antecedent.flags & 128 /* FlowFlags.SwitchClause */) !== 0 &&
833
+ antecedent.isEmpty()) {
834
+ // The bypass branch of a possibly exhaustive switch.
835
+ bypassFlow = antecedent;
836
+ continue;
837
+ }
838
+ const flowType = getTypeAtFlowNode(c, f, antecedent);
839
+ const incomplete = f.incomplete;
840
+ if (flowType === f.declaredType && f.declaredType === f.initialType)
841
+ return complete(f, flowType);
842
+ if (!types.includes(flowType))
843
+ types.push(flowType);
844
+ if (!isTypeSubsetOf(c, flowType, f.initialType))
845
+ subtypeReduction = true;
846
+ if (incomplete)
847
+ seenIncomplete = true;
848
+ }
849
+ if (bypassFlow !== undefined) {
850
+ const flowType = getTypeAtFlowNode(c, f, bypassFlow);
851
+ const incomplete = f.incomplete;
852
+ if ((flowType.flags & 262144 /* TypeFlags.Never */) === 0 &&
853
+ !types.includes(flowType) &&
854
+ !isExhaustiveSwitchStatement(c, bypassFlow.switchStatement)) {
855
+ if (flowType === f.declaredType && f.declaredType === f.initialType)
856
+ return complete(f, flowType);
857
+ types.push(flowType);
858
+ if (!isTypeSubsetOf(c, flowType, f.initialType))
859
+ subtypeReduction = true;
860
+ if (incomplete)
861
+ seenIncomplete = true;
862
+ }
863
+ }
864
+ return newFlowType(c, f, getUnionOfFlowTypes(c, f, types, subtypeReduction ? 2 /* UnionReduction.Subtype */ : 1 /* UnionReduction.Literal */), seenIncomplete);
865
+ }
866
+ /**
867
+ * The union of the types at a junction: an evolving array when every path has one, otherwise the
868
+ * union with evolving arrays finalized.
869
+ */
870
+ function getUnionOfFlowTypes(c, f, types, reduction) {
871
+ if (isEvolvingArrayTypeList(types)) {
872
+ const elementTypes = [];
873
+ for (const t of types)
874
+ elementTypes.push(getElementTypeOfEvolvingArrayType(c, t));
875
+ return getEvolvingArrayType(c, getUnionType(c, elementTypes));
876
+ }
877
+ // Finalizing changes only evolving arrays; without one, the list itself is unioned (getUnionTypeEx
878
+ // does not keep or change it).
879
+ let finalized = types;
880
+ for (const t of types)
881
+ if ((t.objectFlags & 256 /* ObjectFlags.EvolvingArray */) !== 0) {
882
+ finalized = [];
883
+ for (const u of types)
884
+ finalized.push(finalizeEvolvingArrayType(c, u));
885
+ break;
886
+ }
887
+ const result = recombineUnknownType(c, getUnionTypeEx(c, finalized, reduction, undefined, undefined));
888
+ const declared = f.declaredType;
889
+ if (result !== declared && (result.flags & declared.flags & 134217728 /* TypeFlags.Union */) !== 0) {
890
+ const a = result.types;
891
+ const b = declared.types;
892
+ if (a.length === b.length) {
893
+ let same = true;
894
+ for (let i = 0; i < a.length; i++)
895
+ if (a[i] !== b[i]) {
896
+ same = false;
897
+ break;
898
+ }
899
+ if (same)
900
+ return declared;
901
+ }
902
+ }
903
+ return result;
904
+ }
905
+ export function recombineUnknownType(c, t) {
906
+ return t === c.unknownUnionType ? c.unknownType : t;
907
+ }
908
+ function getTypeAtFlowLoopLabel(c, f, flow) {
909
+ if (f.refKey === undefined)
910
+ f.refKey = getFlowReferenceKey(c, f);
911
+ if (f.refKey === '')
912
+ return complete(f, f.declaredType);
913
+ const key = f.refKey;
914
+ let cache = c.flowLoopCaches.get(flow);
915
+ if (cache === undefined) {
916
+ cache = new Map();
917
+ c.flowLoopCaches.set(flow, cache);
918
+ }
919
+ const cached = cache.get(key);
920
+ if (cached !== undefined)
921
+ return complete(f, cached);
922
+ // A junction already being analyzed for this reference yields the types seen so far, incomplete.
923
+ for (const loop of c.flowLoopStack) {
924
+ if (loop.flow === flow && loop.refKey === key && loop.types.length !== 0)
925
+ return newFlowType(c, f, getUnionOfFlowTypes(c, f, loop.types, 1 /* UnionReduction.Literal */), true);
926
+ }
927
+ const antecedentTypes = [];
928
+ let subtypeReduction = false;
929
+ let firstIncomplete = false;
930
+ let first = true;
931
+ for (const antecedent of flow.antecedents) {
932
+ let flowType;
933
+ if (first) {
934
+ // The first antecedent is the path into the loop.
935
+ flowType = getTypeAtFlowNode(c, f, antecedent);
936
+ firstIncomplete = f.incomplete;
937
+ first = false;
938
+ }
939
+ else {
940
+ c.flowLoopStack.push(new FlowLoopInfo(flow, key, antecedentTypes));
941
+ const saveFlowTypeCache = c.flowTypeCache;
942
+ c.flowTypeCache = undefined;
943
+ flowType = getTypeAtFlowNode(c, f, antecedent);
944
+ c.flowTypeCache = saveFlowTypeCache;
945
+ c.flowLoopStack.pop();
946
+ const restarted = cache.get(key);
947
+ if (restarted !== undefined)
948
+ return complete(f, restarted);
949
+ }
950
+ if (!antecedentTypes.includes(flowType))
951
+ antecedentTypes.push(flowType);
952
+ if (!isTypeSubsetOf(c, flowType, f.initialType))
953
+ subtypeReduction = true;
954
+ if (flowType === f.declaredType)
955
+ break;
956
+ }
957
+ const result = getUnionOfFlowTypes(c, f, antecedentTypes, subtypeReduction ? 2 /* UnionReduction.Subtype */ : 1 /* UnionReduction.Literal */);
958
+ if (firstIncomplete)
959
+ return newFlowType(c, f, result, true);
960
+ cache.set(key, result);
961
+ return complete(f, result);
962
+ }
963
+ /** A key identifying a dotted-name reference and its declared/initial types; '' when not a dotted name. */
964
+ function getFlowReferenceKey(c, f) {
965
+ return writeFlowCacheKey(c, f.reference, f.declaredType, f.initialType, f.flowContainer) ?? '';
966
+ }
967
+ function writeFlowCacheKey(c, node, declaredType, initialType, flowContainer) {
968
+ switch (node.kind) {
969
+ case 79 /* SyntaxKind.Identifier */:
970
+ case 109 /* SyntaxKind.ThisKeyword */: {
971
+ let key = '';
972
+ if (node.kind === 79 /* SyntaxKind.Identifier */ && !isThisInTypeQuery(node)) {
973
+ const symbol = getResolvedSymbol(c, node);
974
+ if (symbol === c.unknownSymbol)
975
+ return undefined;
976
+ key = String(getSymbolId(symbol));
977
+ }
978
+ key += ':' + declaredType.id;
979
+ if (initialType !== declaredType)
980
+ key += '=' + initialType.id;
981
+ if (flowContainer !== undefined)
982
+ key += '@' + getNodeId(flowContainer);
983
+ return key;
984
+ }
985
+ case 236 /* SyntaxKind.NonNullExpression */:
986
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
987
+ return writeFlowCacheKey(c, getExpression(node), declaredType, initialType, flowContainer);
988
+ case 167 /* SyntaxKind.QualifiedName */: {
989
+ const left = writeFlowCacheKey(c, node.left, declaredType, initialType, flowContainer);
990
+ return left === undefined ? undefined : left + '.' + node.right.text;
991
+ }
992
+ case 212 /* SyntaxKind.PropertyAccessExpression */:
993
+ case 213 /* SyntaxKind.ElementAccessExpression */: {
994
+ const name = getAccessedPropertyName(c, node);
995
+ if (name !== undefined) {
996
+ const left = writeFlowCacheKey(c, getExpression(node), declaredType, initialType, flowContainer);
997
+ return left === undefined ? undefined : left + '.' + name;
998
+ }
999
+ // `obj[key]` for a key that isn't reassigned.
1000
+ if (node.kind !== 213 /* SyntaxKind.ElementAccessExpression */)
1001
+ return undefined;
1002
+ const argument = node.argumentExpression;
1003
+ if (argument.kind !== 79 /* SyntaxKind.Identifier */)
1004
+ return undefined;
1005
+ const symbol = getResolvedSymbol(c, argument);
1006
+ if (!isUnreassignedVariable(c, symbol))
1007
+ return undefined;
1008
+ const left = writeFlowCacheKey(c, getExpression(node), declaredType, initialType, flowContainer);
1009
+ return left === undefined ? undefined : left + '.@' + getSymbolId(symbol);
1010
+ }
1011
+ // Pseudo-references of getNarrowedTypeOfSymbol.
1012
+ case 207 /* SyntaxKind.ObjectBindingPattern */:
1013
+ case 208 /* SyntaxKind.ArrayBindingPattern */:
1014
+ case 263 /* SyntaxKind.FunctionDeclaration */:
1015
+ case 219 /* SyntaxKind.FunctionExpression */:
1016
+ case 220 /* SyntaxKind.ArrowFunction */:
1017
+ case 175 /* SyntaxKind.MethodDeclaration */:
1018
+ return '#' + getNodeId(node) + '#' + declaredType.id;
1019
+ }
1020
+ return undefined;
1021
+ }
1022
+ // ---- Narrowing by expressions
1023
+ function narrowType(c, f, t, expr, assumeTrue) {
1024
+ // `a?.b` implies `a` is not nullish; so does the left of `??`.
1025
+ const parent = expr.parent;
1026
+ if (isExpressionOfOptionalChainRoot(expr) ||
1027
+ (parent !== undefined &&
1028
+ parent.kind === 227 /* SyntaxKind.BinaryExpression */ &&
1029
+ (parent.operatorToken.kind === 60 /* SyntaxKind.QuestionQuestionToken */ ||
1030
+ parent.operatorToken.kind === 77 /* SyntaxKind.QuestionQuestionEqualsToken */) &&
1031
+ parent.left === expr))
1032
+ return narrowTypeByOptionality(c, f, t, expr, assumeTrue);
1033
+ switch (expr.kind) {
1034
+ case 79 /* SyntaxKind.Identifier */:
1035
+ // Narrowing through aliased conditions: `const ok = x !== undefined; if (ok) ...`.
1036
+ if (!isMatchingReference(c, f.reference, expr) && c.inlineLevel < 5) {
1037
+ const symbol = getResolvedSymbol(c, expr);
1038
+ if (isConstantVariable(c, symbol)) {
1039
+ const declaration = symbol.valueDeclaration;
1040
+ if (declaration !== undefined &&
1041
+ declaration.kind === 261 /* SyntaxKind.VariableDeclaration */ &&
1042
+ getType(declaration) === undefined) {
1043
+ const initializer = getInitializer(declaration);
1044
+ // For an access, isConstantReference reads the property symbol resolved where the
1045
+ // reference is, which narrowing of its object changes.
1046
+ if (initializer !== undefined && isAccessExpression(f.reference))
1047
+ f.positional = true;
1048
+ if (initializer !== undefined && isConstantReference(c, f.reference)) {
1049
+ c.inlineLevel++;
1050
+ const result = narrowType(c, f, t, initializer, assumeTrue);
1051
+ c.inlineLevel--;
1052
+ return result;
1053
+ }
1054
+ }
1055
+ }
1056
+ }
1057
+ return narrowTypeByTruthiness(c, f, t, expr, assumeTrue);
1058
+ case 109 /* SyntaxKind.ThisKeyword */:
1059
+ case 107 /* SyntaxKind.SuperKeyword */:
1060
+ case 212 /* SyntaxKind.PropertyAccessExpression */:
1061
+ case 213 /* SyntaxKind.ElementAccessExpression */:
1062
+ return narrowTypeByTruthiness(c, f, t, expr, assumeTrue);
1063
+ case 214 /* SyntaxKind.CallExpression */:
1064
+ return narrowTypeByCallExpression(c, f, t, expr, assumeTrue);
1065
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
1066
+ case 236 /* SyntaxKind.NonNullExpression */:
1067
+ case 239 /* SyntaxKind.SatisfiesExpression */:
1068
+ return narrowType(c, f, t, getExpression(expr), assumeTrue);
1069
+ case 227 /* SyntaxKind.BinaryExpression */:
1070
+ return narrowTypeByBinaryExpression(c, f, t, expr, assumeTrue);
1071
+ case 225 /* SyntaxKind.PrefixUnaryExpression */:
1072
+ if (expr.operator === 53 /* SyntaxKind.ExclamationToken */)
1073
+ return narrowType(c, f, t, expr.operand, !assumeTrue);
1074
+ break;
1075
+ }
1076
+ return t;
1077
+ }
1078
+ function narrowTypeByOptionality(c, f, t, expr, assumePresent) {
1079
+ const facts = assumePresent ? 2097152 /* TypeFacts.NEUndefinedOrNull */ : 262144 /* TypeFacts.EQUndefinedOrNull */;
1080
+ if (isMatchingReference(c, f.reference, expr))
1081
+ return getAdjustedTypeWithFacts(c, t, facts);
1082
+ const access = getDiscriminantPropertyAccess(c, f, expr, t);
1083
+ if (access !== undefined)
1084
+ return narrowTypeByDiscriminant(c, t, access, (u) => getTypeWithFacts(c, u, facts));
1085
+ return t;
1086
+ }
1087
+ function narrowTypeByTruthiness(c, f, t, expr, assumeTrue) {
1088
+ const facts = assumeTrue ? 4194304 /* TypeFacts.Truthy */ : 8388608 /* TypeFacts.Falsy */;
1089
+ if (isMatchingReference(c, f.reference, expr))
1090
+ return getAdjustedTypeWithFacts(c, t, facts);
1091
+ if (c.strictNullChecks && assumeTrue && optionalChainContainsReference(c, expr, f.reference))
1092
+ t = getAdjustedTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */);
1093
+ const access = getDiscriminantPropertyAccess(c, f, expr, t);
1094
+ if (access !== undefined)
1095
+ return narrowTypeByDiscriminant(c, t, access, (u) => getTypeWithFacts(c, u, facts));
1096
+ return t;
1097
+ }
1098
+ function narrowTypeByCallExpression(c, f, t, call, assumeTrue) {
1099
+ if (hasMatchingArgument(c, call, f.reference)) {
1100
+ const signature = assumeTrue || !isCallChain(call) ? getEffectsSignature(c, call) : undefined;
1101
+ const predicate = signature !== undefined ? getTypePredicateOfSignature(c, signature) : undefined;
1102
+ if (predicate !== undefined &&
1103
+ (predicate.kind === 0 /* TypePredicateKind.This */ || predicate.kind === 1 /* TypePredicateKind.Identifier */))
1104
+ return narrowTypeByTypePredicate(c, f, t, predicate, call, assumeTrue);
1105
+ }
1106
+ // `obj.hasOwnProperty("p")` narrows an optional `obj.p` under exactOptionalPropertyTypes.
1107
+ if (containsMissingType(c, t) && isAccessExpression(f.reference)) {
1108
+ const callee = getExpression(call);
1109
+ if (callee.kind === 212 /* SyntaxKind.PropertyAccessExpression */) {
1110
+ const name = callee.name;
1111
+ const args = getArguments(call).nodes;
1112
+ if (isMatchingReference(c, getExpression(f.reference), getReferenceCandidate(getExpression(callee))) &&
1113
+ name.kind === 79 /* SyntaxKind.Identifier */ &&
1114
+ getText(name) === 'hasOwnProperty' &&
1115
+ args.length === 1 &&
1116
+ isStringLiteralLike(args[0]) &&
1117
+ getAccessedPropertyName(c, f.reference) === getText(args[0]))
1118
+ return getTypeWithFacts(c, t, assumeTrue ? 524288 /* TypeFacts.NEUndefined */ : 65536 /* TypeFacts.EQUndefined */);
1119
+ }
1120
+ }
1121
+ return t;
1122
+ }
1123
+ function narrowTypeByTypePredicate(c, f, t, predicate, call, assumeTrue) {
1124
+ const predicateType = predicate.type;
1125
+ if (predicateType === undefined)
1126
+ return t;
1127
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0 &&
1128
+ (predicateType === c.globalObjectType || predicateType === c.globalFunctionType))
1129
+ return t;
1130
+ const argument = getTypePredicateArgument(predicate, call);
1131
+ if (argument === undefined)
1132
+ return t;
1133
+ if (isMatchingReference(c, f.reference, argument))
1134
+ return getNarrowedType(c, t, predicateType, assumeTrue, false);
1135
+ if (c.strictNullChecks &&
1136
+ optionalChainContainsReference(c, argument, f.reference) &&
1137
+ ((assumeTrue && !hasTypeFacts(c, predicateType, 65536 /* TypeFacts.EQUndefined */)) ||
1138
+ (!assumeTrue && everyType(predicateType, isNullableType))))
1139
+ t = getAdjustedTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */);
1140
+ const access = getDiscriminantPropertyAccess(c, f, argument, t);
1141
+ if (access !== undefined)
1142
+ return narrowTypeByDiscriminant(c, t, access, (u) => getNarrowedType(c, u, predicateType, assumeTrue, false));
1143
+ return t;
1144
+ }
1145
+ function isNullableType(t) {
1146
+ return (t.flags & 12 /* TypeFlags.Nullable */) !== 0;
1147
+ }
1148
+ function getTypePredicateArgument(predicate, call) {
1149
+ if (predicate.kind === 1 /* TypePredicateKind.Identifier */ || predicate.kind === 3 /* TypePredicateKind.AssertsIdentifier */) {
1150
+ const args = getArguments(call).nodes;
1151
+ return predicate.parameterIndex >= 0 && predicate.parameterIndex < args.length
1152
+ ? args[predicate.parameterIndex]
1153
+ : undefined;
1154
+ }
1155
+ const invoked = skipParentheses(getExpression(call));
1156
+ return invoked.kind === 212 /* SyntaxKind.PropertyAccessExpression */ || invoked.kind === 213 /* SyntaxKind.ElementAccessExpression */
1157
+ ? skipParentheses(getExpression(invoked))
1158
+ : undefined;
1159
+ }
1160
+ function narrowTypeByBinaryExpression(c, f, t, expr, assumeTrue) {
1161
+ const operator = expr.operatorToken.kind;
1162
+ switch (operator) {
1163
+ case 63 /* SyntaxKind.EqualsToken */:
1164
+ case 75 /* SyntaxKind.BarBarEqualsToken */:
1165
+ case 76 /* SyntaxKind.AmpersandAmpersandEqualsToken */:
1166
+ case 77 /* SyntaxKind.QuestionQuestionEqualsToken */:
1167
+ return narrowTypeByTruthiness(c, f, narrowType(c, f, t, expr.right, assumeTrue), expr.left, assumeTrue);
1168
+ case 34 /* SyntaxKind.EqualsEqualsToken */:
1169
+ case 35 /* SyntaxKind.ExclamationEqualsToken */:
1170
+ case 36 /* SyntaxKind.EqualsEqualsEqualsToken */:
1171
+ case 37 /* SyntaxKind.ExclamationEqualsEqualsToken */: {
1172
+ const left = getReferenceCandidate(expr.left);
1173
+ const right = getReferenceCandidate(expr.right);
1174
+ if (left.kind === 222 /* SyntaxKind.TypeOfExpression */ && isStringLiteralLike(right))
1175
+ return narrowTypeByTypeof(c, f, t, left, operator, right, assumeTrue);
1176
+ if (right.kind === 222 /* SyntaxKind.TypeOfExpression */ && isStringLiteralLike(left))
1177
+ return narrowTypeByTypeof(c, f, t, right, operator, left, assumeTrue);
1178
+ if (isMatchingReference(c, f.reference, left))
1179
+ return narrowTypeByEquality(c, t, operator, right, assumeTrue);
1180
+ if (isMatchingReference(c, f.reference, right))
1181
+ return narrowTypeByEquality(c, t, operator, left, assumeTrue);
1182
+ if (c.strictNullChecks) {
1183
+ if (optionalChainContainsReference(c, left, f.reference))
1184
+ t = narrowTypeByOptionalChainContainment(c, t, operator, right, assumeTrue);
1185
+ else if (optionalChainContainsReference(c, right, f.reference))
1186
+ t = narrowTypeByOptionalChainContainment(c, t, operator, left, assumeTrue);
1187
+ }
1188
+ const leftAccess = getDiscriminantPropertyAccess(c, f, left, t);
1189
+ if (leftAccess !== undefined)
1190
+ return narrowTypeByDiscriminantProperty(c, t, leftAccess, operator, right, assumeTrue);
1191
+ const rightAccess = getDiscriminantPropertyAccess(c, f, right, t);
1192
+ if (rightAccess !== undefined)
1193
+ return narrowTypeByDiscriminantProperty(c, t, rightAccess, operator, left, assumeTrue);
1194
+ if (isMatchingConstructorReference(c, f, left))
1195
+ return narrowTypeByConstructor(c, t, operator, right, assumeTrue);
1196
+ if (isMatchingConstructorReference(c, f, right))
1197
+ return narrowTypeByConstructor(c, t, operator, left, assumeTrue);
1198
+ if (isBooleanLiteral(right) && !isAccessExpression(left))
1199
+ return narrowTypeByBooleanComparison(c, f, t, left, right, operator, assumeTrue);
1200
+ if (isBooleanLiteral(left) && !isAccessExpression(right))
1201
+ return narrowTypeByBooleanComparison(c, f, t, right, left, operator, assumeTrue);
1202
+ break;
1203
+ }
1204
+ case 103 /* SyntaxKind.InstanceOfKeyword */:
1205
+ return narrowTypeByInstanceof(c, f, t, expr, assumeTrue);
1206
+ case 102 /* SyntaxKind.InKeyword */: {
1207
+ if (expr.left.kind === 80 /* SyntaxKind.PrivateIdentifier */)
1208
+ return narrowTypeByPrivateIdentifierInInExpression(c, f, t, expr, assumeTrue);
1209
+ const target = getReferenceCandidate(expr.right);
1210
+ if (containsMissingType(c, t) &&
1211
+ isAccessExpression(f.reference) &&
1212
+ isMatchingReference(c, getExpression(f.reference), target)) {
1213
+ const leftType = getTypeOfExpression(c, expr.left);
1214
+ if (isTypeUsableAsPropertyName(leftType) &&
1215
+ getAccessedPropertyName(c, f.reference) === getPropertyNameFromType(leftType))
1216
+ return getTypeWithFacts(c, t, assumeTrue ? 524288 /* TypeFacts.NEUndefined */ : 65536 /* TypeFacts.EQUndefined */);
1217
+ }
1218
+ if (isMatchingReference(c, f.reference, target)) {
1219
+ const leftType = getTypeOfExpression(c, expr.left);
1220
+ if (isTypeUsableAsPropertyName(leftType))
1221
+ return narrowTypeByInKeyword(c, t, leftType, assumeTrue);
1222
+ }
1223
+ break;
1224
+ }
1225
+ case 27 /* SyntaxKind.CommaToken */:
1226
+ return narrowType(c, f, t, expr.right, assumeTrue);
1227
+ case 55 /* SyntaxKind.AmpersandAmpersandToken */:
1228
+ if (assumeTrue)
1229
+ return narrowType(c, f, narrowType(c, f, t, expr.left, true), expr.right, true);
1230
+ return getUnionType(c, [narrowType(c, f, t, expr.left, false), narrowType(c, f, t, expr.right, false)]);
1231
+ case 56 /* SyntaxKind.BarBarToken */:
1232
+ if (assumeTrue)
1233
+ return getUnionType(c, [narrowType(c, f, t, expr.left, true), narrowType(c, f, t, expr.right, true)]);
1234
+ return narrowType(c, f, narrowType(c, f, t, expr.left, false), expr.right, false);
1235
+ }
1236
+ return t;
1237
+ }
1238
+ function narrowTypeByPrivateIdentifierInInExpression(c, f, t, expr, assumeTrue) {
1239
+ if (!isMatchingReference(c, f.reference, getReferenceCandidate(expr.right)))
1240
+ return t;
1241
+ const symbol = getSymbolForPrivateIdentifierExpression(c, expr.left);
1242
+ if (symbol === undefined)
1243
+ return t;
1244
+ const classSymbol = symbol.parent;
1245
+ const targetType = hasStaticModifier(symbol.valueDeclaration)
1246
+ ? getTypeOfSymbol(c, classSymbol)
1247
+ : getDeclaredTypeOfSymbol(c, classSymbol);
1248
+ return getNarrowedType(c, t, targetType, assumeTrue, true);
1249
+ }
1250
+ function isBooleanLiteral(node) {
1251
+ return node.kind === 111 /* SyntaxKind.TrueKeyword */ || node.kind === 96 /* SyntaxKind.FalseKeyword */;
1252
+ }
1253
+ function isAccessExpression(node) {
1254
+ return node.kind === 212 /* SyntaxKind.PropertyAccessExpression */ || node.kind === 213 /* SyntaxKind.ElementAccessExpression */;
1255
+ }
1256
+ function isStringLiteralLike(node) {
1257
+ return node.kind === 10 /* SyntaxKind.StringLiteral */ || node.kind === 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */;
1258
+ }
1259
+ function narrowTypeByBooleanComparison(c, f, t, expr, boolValue, operator, assumeTrue) {
1260
+ const isEquality = operator !== 37 /* SyntaxKind.ExclamationEqualsEqualsToken */ && operator !== 35 /* SyntaxKind.ExclamationEqualsToken */;
1261
+ return narrowType(c, f, t, expr, (assumeTrue !== (boolValue.kind === 111 /* SyntaxKind.TrueKeyword */)) !== isEquality);
1262
+ }
1263
+ function narrowTypeByEquality(c, t, operator, value, assumeTrueIn) {
1264
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
1265
+ return t;
1266
+ let assumeTrue = assumeTrueIn;
1267
+ if (operator === 35 /* SyntaxKind.ExclamationEqualsToken */ || operator === 37 /* SyntaxKind.ExclamationEqualsEqualsToken */)
1268
+ assumeTrue = !assumeTrue;
1269
+ const valueType = getTypeOfExpression(c, value);
1270
+ const doubleEquals = operator === 34 /* SyntaxKind.EqualsEqualsToken */ || operator === 35 /* SyntaxKind.ExclamationEqualsToken */;
1271
+ if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
1272
+ return narrowTypeByEqualityWorker(c, t, valueType, doubleEquals, assumeTrue);
1273
+ // The result depends only on the types and the operator, and each flow walk through a condition
1274
+ // narrows the same union again: cache it.
1275
+ const bits = 8 | (doubleEquals ? 2 : 0) | (assumeTrue ? 1 : 0);
1276
+ const pair = cachedTypePairKey(t.id, valueType.id, bits);
1277
+ const key = pair < 0 ? 'narrowedByEquality' + t.id + ',' + valueType.id + ',' + bits : '';
1278
+ let result = pair < 0 ? c.cachedTypes.get(key) : c.cachedTypesByPair.get(pair);
1279
+ if (result === undefined) {
1280
+ result = narrowTypeByEqualityWorker(c, t, valueType, doubleEquals, assumeTrue);
1281
+ if (pair < 0)
1282
+ c.cachedTypes.set(key, result);
1283
+ else
1284
+ c.cachedTypesByPair.set(pair, result);
1285
+ }
1286
+ return result;
1287
+ }
1288
+ function narrowTypeByEqualityWorker(c, t, valueType, doubleEquals, assumeTrue) {
1289
+ if ((valueType.flags & 12 /* TypeFlags.Nullable */) !== 0) {
1290
+ if (!c.strictNullChecks)
1291
+ return t;
1292
+ let facts;
1293
+ if (doubleEquals)
1294
+ facts = assumeTrue ? 262144 /* TypeFacts.EQUndefinedOrNull */ : 2097152 /* TypeFacts.NEUndefinedOrNull */;
1295
+ else if ((valueType.flags & 8 /* TypeFlags.Null */) !== 0)
1296
+ facts = assumeTrue ? 131072 /* TypeFacts.EQNull */ : 1048576 /* TypeFacts.NENull */;
1297
+ else
1298
+ facts = assumeTrue ? 65536 /* TypeFacts.EQUndefined */ : 524288 /* TypeFacts.NEUndefined */;
1299
+ return getAdjustedTypeWithFacts(c, t, facts);
1300
+ }
1301
+ if (assumeTrue) {
1302
+ if (!doubleEquals &&
1303
+ ((t.flags & 2 /* TypeFlags.Unknown */) !== 0 || someType(t, (u) => isEmptyAnonymousObjectType(c, u)))) {
1304
+ if ((valueType.flags & (12713980 /* TypeFlags.Primitive */ | 131072 /* TypeFlags.NonPrimitive */)) !== 0 ||
1305
+ isEmptyAnonymousObjectType(c, valueType))
1306
+ return valueType;
1307
+ if ((valueType.flags & 1048576 /* TypeFlags.Object */) !== 0)
1308
+ return c.nonPrimitiveType;
1309
+ }
1310
+ const filtered = filterType(c, t, (u) => areTypesComparable(c, u, valueType) || (doubleEquals && isCoercibleUnderDoubleEquals(u, valueType)));
1311
+ return replacePrimitivesWithLiterals(c, filtered, valueType);
1312
+ }
1313
+ if (isUnitType(valueType))
1314
+ return filterType(c, t, (u) => !(isUnitLikeType(u) && areTypesComparable(c, u, valueType)));
1315
+ return t;
1316
+ }
1317
+ function isCoercibleUnderDoubleEquals(source, target) {
1318
+ return ((source.flags & (64 /* TypeFlags.Number */ | 32 /* TypeFlags.String */ | 8192 /* TypeFlags.BooleanLiteral */)) !== 0 &&
1319
+ (target.flags & (64 /* TypeFlags.Number */ | 32 /* TypeFlags.String */ | 256 /* TypeFlags.Boolean */)) !== 0);
1320
+ }
1321
+ function isUnitLikeType(t) {
1322
+ return extractUnitType(t) !== undefined;
1323
+ }
1324
+ /** `t`, or the first unit type of intersection `t`; undefined when `t` isn't unit-like. */
1325
+ function extractUnitType(t) {
1326
+ if ((t.flags & 268435456 /* TypeFlags.Intersection */) === 0)
1327
+ return isUnitType(t) ? t : undefined;
1328
+ for (const u of typesOf(t))
1329
+ if (isUnitType(u))
1330
+ return u;
1331
+ return undefined;
1332
+ }
1333
+ function replacePrimitivesWithLiterals(c, typeWithPrimitives, typeWithLiterals) {
1334
+ if (maybeTypeOfKind(typeWithPrimitives, 32 /* TypeFlags.String */ | 4194304 /* TypeFlags.TemplateLiteral */ | 64 /* TypeFlags.Number */ | 128 /* TypeFlags.BigInt */) &&
1335
+ maybeTypeOfKind(typeWithLiterals, 1024 /* TypeFlags.StringLiteral */ |
1336
+ 4194304 /* TypeFlags.TemplateLiteral */ |
1337
+ 8388608 /* TypeFlags.StringMapping */ |
1338
+ 2048 /* TypeFlags.NumberLiteral */ |
1339
+ 4096 /* TypeFlags.BigIntLiteral */)) {
1340
+ return mapType(c, typeWithPrimitives, (t) => {
1341
+ if ((t.flags & 32 /* TypeFlags.String */) !== 0)
1342
+ return extractTypesOfKind(c, typeWithLiterals, 32 /* TypeFlags.String */ | 1024 /* TypeFlags.StringLiteral */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */);
1343
+ if (isPatternLiteralType(c, t) &&
1344
+ !maybeTypeOfKind(typeWithLiterals, 32 /* TypeFlags.String */ | 4194304 /* TypeFlags.TemplateLiteral */ | 8388608 /* TypeFlags.StringMapping */))
1345
+ return extractTypesOfKind(c, typeWithLiterals, 1024 /* TypeFlags.StringLiteral */);
1346
+ if ((t.flags & 64 /* TypeFlags.Number */) !== 0)
1347
+ return extractTypesOfKind(c, typeWithLiterals, 64 /* TypeFlags.Number */ | 2048 /* TypeFlags.NumberLiteral */);
1348
+ if ((t.flags & 128 /* TypeFlags.BigInt */) !== 0)
1349
+ return extractTypesOfKind(c, typeWithLiterals, 128 /* TypeFlags.BigInt */ | 4096 /* TypeFlags.BigIntLiteral */);
1350
+ return t;
1351
+ });
1352
+ }
1353
+ return typeWithPrimitives;
1354
+ }
1355
+ function extractTypesOfKind(c, t, kind) {
1356
+ return filterType(c, t, (u) => (u.flags & kind) !== 0);
1357
+ }
1358
+ function narrowTypeByTypeof(c, f, t, typeOfExpr, operator, literal, assumeTrueIn) {
1359
+ let assumeTrue = assumeTrueIn;
1360
+ if (operator === 35 /* SyntaxKind.ExclamationEqualsToken */ || operator === 37 /* SyntaxKind.ExclamationEqualsEqualsToken */)
1361
+ assumeTrue = !assumeTrue;
1362
+ const target = getReferenceCandidate(getExpression(typeOfExpr));
1363
+ const text = getText(literal);
1364
+ if (!isMatchingReference(c, f.reference, target)) {
1365
+ if (c.strictNullChecks &&
1366
+ optionalChainContainsReference(c, target, f.reference) &&
1367
+ assumeTrue === (text !== 'undefined'))
1368
+ t = getAdjustedTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */);
1369
+ const access = getDiscriminantPropertyAccess(c, f, target, t);
1370
+ if (access !== undefined)
1371
+ return narrowTypeByDiscriminant(c, t, access, (u) => narrowTypeByLiteralExpression(c, u, text, assumeTrue));
1372
+ return t;
1373
+ }
1374
+ return narrowTypeByLiteralExpression(c, t, text, assumeTrue);
1375
+ }
1376
+ function typeofNEFacts(text) {
1377
+ switch (text) {
1378
+ case 'string':
1379
+ return 256 /* TypeFacts.TypeofNEString */;
1380
+ case 'number':
1381
+ return 512 /* TypeFacts.TypeofNENumber */;
1382
+ case 'bigint':
1383
+ return 1024 /* TypeFacts.TypeofNEBigInt */;
1384
+ case 'boolean':
1385
+ return 2048 /* TypeFacts.TypeofNEBoolean */;
1386
+ case 'symbol':
1387
+ return 4096 /* TypeFacts.TypeofNESymbol */;
1388
+ case 'undefined':
1389
+ return 524288 /* TypeFacts.NEUndefined */;
1390
+ case 'object':
1391
+ return 8192 /* TypeFacts.TypeofNEObject */;
1392
+ case 'function':
1393
+ return 16384 /* TypeFacts.TypeofNEFunction */;
1394
+ }
1395
+ return 32768 /* TypeFacts.TypeofNEHostObject */;
1396
+ }
1397
+ function narrowTypeByLiteralExpression(c, t, text, assumeTrue) {
1398
+ return assumeTrue ? narrowTypeByTypeName(c, t, text) : getAdjustedTypeWithFacts(c, t, typeofNEFacts(text));
1399
+ }
1400
+ function narrowTypeByTypeName(c, t, typeName) {
1401
+ switch (typeName) {
1402
+ case 'string':
1403
+ return narrowTypeByTypeFacts(c, t, c.stringType, 1 /* TypeFacts.TypeofEQString */);
1404
+ case 'number':
1405
+ return narrowTypeByTypeFacts(c, t, c.numberType, 2 /* TypeFacts.TypeofEQNumber */);
1406
+ case 'bigint':
1407
+ return narrowTypeByTypeFacts(c, t, c.bigintType, 4 /* TypeFacts.TypeofEQBigInt */);
1408
+ case 'boolean':
1409
+ return narrowTypeByTypeFacts(c, t, c.booleanType, 8 /* TypeFacts.TypeofEQBoolean */);
1410
+ case 'symbol':
1411
+ return narrowTypeByTypeFacts(c, t, c.esSymbolType, 16 /* TypeFacts.TypeofEQSymbol */);
1412
+ case 'object':
1413
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
1414
+ return t;
1415
+ return getUnionType(c, [
1416
+ narrowTypeByTypeFacts(c, t, c.nonPrimitiveType, 32 /* TypeFacts.TypeofEQObject */),
1417
+ narrowTypeByTypeFacts(c, t, c.nullType, 131072 /* TypeFacts.EQNull */),
1418
+ ]);
1419
+ case 'function':
1420
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
1421
+ return t;
1422
+ return narrowTypeByTypeFacts(c, t, c.globalFunctionType, 64 /* TypeFacts.TypeofEQFunction */);
1423
+ case 'undefined':
1424
+ return narrowTypeByTypeFacts(c, t, c.undefinedType, 65536 /* TypeFacts.EQUndefined */);
1425
+ }
1426
+ return narrowTypeByTypeFacts(c, t, c.nonPrimitiveType, 128 /* TypeFacts.TypeofEQHostObject */);
1427
+ }
1428
+ function narrowTypeByTypeFacts(c, t, impliedType, facts) {
1429
+ return mapType(c, t, (u) => {
1430
+ if (isTypeRelatedTo(c, u, impliedType, c.strictSubtypeRelation))
1431
+ return hasTypeFacts(c, u, facts) ? u : c.neverType;
1432
+ if (isTypeSubtypeOf(c, impliedType, u))
1433
+ return impliedType;
1434
+ if (hasTypeFacts(c, u, facts))
1435
+ return getIntersectionType(c, [u, impliedType]);
1436
+ return c.neverType;
1437
+ });
1438
+ }
1439
+ function narrowTypeByOptionalChainContainment(c, t, operator, value, assumeTrue) {
1440
+ const equalsOperator = operator === 34 /* SyntaxKind.EqualsEqualsToken */ || operator === 36 /* SyntaxKind.EqualsEqualsEqualsToken */;
1441
+ const nullableFlags = operator === 34 /* SyntaxKind.EqualsEqualsToken */ || operator === 35 /* SyntaxKind.ExclamationEqualsToken */
1442
+ ? 12 /* TypeFlags.Nullable */
1443
+ : 4 /* TypeFlags.Undefined */;
1444
+ const valueType = getTypeOfExpression(c, value);
1445
+ const removeNullable = (equalsOperator !== assumeTrue && everyType(valueType, (u) => (u.flags & nullableFlags) !== 0)) ||
1446
+ (equalsOperator === assumeTrue &&
1447
+ everyType(valueType, (u) => (u.flags & (3 /* TypeFlags.AnyOrUnknown */ | nullableFlags)) === 0));
1448
+ return removeNullable ? getAdjustedTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */) : t;
1449
+ }
1450
+ function narrowTypeByInstanceof(c, f, t, expr, assumeTrue) {
1451
+ const left = getReferenceCandidate(expr.left);
1452
+ if (!isMatchingReference(c, f.reference, left)) {
1453
+ if (assumeTrue && c.strictNullChecks && optionalChainContainsReference(c, left, f.reference))
1454
+ return getAdjustedTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */);
1455
+ return t;
1456
+ }
1457
+ const rightType = getTypeOfExpression(c, expr.right);
1458
+ if (!isTypeDerivedFrom(c, rightType, c.globalObjectType))
1459
+ return t;
1460
+ // A `[Symbol.hasInstance]` method with a type predicate narrows like a type guard call.
1461
+ const signature = getEffectsSignature(c, expr);
1462
+ const predicate = signature !== undefined ? getTypePredicateOfSignature(c, signature) : undefined;
1463
+ if (predicate !== undefined && predicate.kind === 1 /* TypePredicateKind.Identifier */ && predicate.parameterIndex === 0)
1464
+ return getNarrowedType(c, t, predicate.type, assumeTrue, true);
1465
+ if (!isTypeDerivedFrom(c, rightType, c.globalFunctionType))
1466
+ return t;
1467
+ const instanceType = mapType(c, rightType, (u) => getInstanceType(c, u));
1468
+ if (((t.flags & 1 /* TypeFlags.Any */) !== 0 &&
1469
+ (instanceType === c.globalObjectType || instanceType === c.globalFunctionType)) ||
1470
+ (!assumeTrue && !((instanceType.flags & 1048576 /* TypeFlags.Object */) !== 0 && !isEmptyAnonymousObjectType(c, instanceType))))
1471
+ return t;
1472
+ return getNarrowedType(c, t, instanceType, assumeTrue, true);
1473
+ }
1474
+ function getInstanceType(c, constructorType) {
1475
+ const prototypeType = getTypeOfPropertyOfType(c, constructorType, 'prototype');
1476
+ if (prototypeType !== undefined && (prototypeType.flags & 1 /* TypeFlags.Any */) === 0)
1477
+ return prototypeType;
1478
+ const signatures = getSignaturesOfType(c, constructorType, 1 /* SignatureKind.Construct */);
1479
+ if (signatures.length === 0)
1480
+ return c.emptyObjectType;
1481
+ const types = [];
1482
+ for (const s of signatures)
1483
+ types.push(getReturnTypeOfSignature(c, getErasedSignature(c, s)));
1484
+ return getUnionType(c, types);
1485
+ }
1486
+ function narrowTypeByInKeyword(c, t, nameType, assumeTrue) {
1487
+ const name = getPropertyNameFromType(nameType);
1488
+ if (someType(t, (u) => isTypePresencePossible(c, u, name, true)))
1489
+ return filterType(c, t, (u) => isTypePresencePossible(c, u, name, assumeTrue));
1490
+ // `"x" in t` for an unknown property x narrows t to `t & Record<"x", unknown>`.
1491
+ if (assumeTrue) {
1492
+ const record = getGlobalTypeAliasSymbol(c, 'Record', 2, true);
1493
+ if (record !== undefined)
1494
+ return getIntersectionType(c, [t, getTypeAliasInstantiation(c, record, [nameType, c.unknownType], undefined)]);
1495
+ }
1496
+ return t;
1497
+ }
1498
+ function isTypePresencePossible(c, t, propName, assumeTrue) {
1499
+ const prop = getPropertyOfType(c, t, propName);
1500
+ if (prop !== undefined)
1501
+ return (prop.flags & 16777216 /* SymbolFlags.Optional */) !== 0 || (prop.checkFlags & 48 /* CheckFlags.Partial */) !== 0 || assumeTrue;
1502
+ return getApplicableIndexInfoForName(c, t, propName) !== undefined || !assumeTrue;
1503
+ }
1504
+ /** `t` narrowed to `candidate` (or with it removed when `assumeTrue` is false). */
1505
+ export function getNarrowedType(c, t, candidate, assumeTrue, checkDerived) {
1506
+ if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
1507
+ return getNarrowedTypeWorker(c, t, candidate, assumeTrue, checkDerived);
1508
+ const pair = cachedTypePairKey(t.id, candidate.id, 4 | (assumeTrue ? 2 : 0) | (checkDerived ? 1 : 0));
1509
+ const key = pair < 0 ? 'narrowed' + t.id + ',' + candidate.id + (assumeTrue ? 't' : 'f') + (checkDerived ? 'd' : '') : '';
1510
+ let result = pair < 0 ? c.cachedTypes.get(key) : c.cachedTypesByPair.get(pair);
1511
+ if (result === undefined) {
1512
+ result = getNarrowedTypeWorker(c, t, candidate, assumeTrue, checkDerived);
1513
+ if (pair < 0)
1514
+ c.cachedTypes.set(key, result);
1515
+ else
1516
+ c.cachedTypesByPair.set(pair, result);
1517
+ }
1518
+ return result;
1519
+ }
1520
+ function getNarrowedTypeWorker(c, typeIn, candidate, assumeTrue, checkDerived) {
1521
+ let t = typeIn;
1522
+ if (!assumeTrue) {
1523
+ if (t === candidate)
1524
+ return c.neverType;
1525
+ if (checkDerived)
1526
+ return filterType(c, t, (u) => !isTypeDerivedFrom(c, u, candidate));
1527
+ if ((t.flags & 2 /* TypeFlags.Unknown */) !== 0)
1528
+ t = c.unknownUnionType;
1529
+ const trueType = getNarrowedType(c, t, candidate, true, false);
1530
+ return recombineUnknownType(c, filterType(c, t, (u) => !isTypeSubsetOf(c, u, trueType)));
1531
+ }
1532
+ if ((t.flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0)
1533
+ return candidate;
1534
+ if (t === candidate)
1535
+ return candidate;
1536
+ const keyPropertyName = (t.flags & 134217728 /* TypeFlags.Union */) !== 0 ? getKeyPropertyName(c, t) : '';
1537
+ const narrowedType = mapType(c, candidate, (n) => {
1538
+ let matching = t;
1539
+ if (keyPropertyName !== '') {
1540
+ const discriminant = getTypeOfPropertyOfType(c, n, keyPropertyName);
1541
+ if (discriminant !== undefined) {
1542
+ const constituent = getConstituentTypeForKeyType(c, t, discriminant);
1543
+ if (constituent !== undefined)
1544
+ matching = constituent;
1545
+ }
1546
+ }
1547
+ const directlyRelated = mapType(c, matching, (u) => {
1548
+ if (checkDerived) {
1549
+ if (isTypeDerivedFrom(c, u, n))
1550
+ return u;
1551
+ if (isTypeDerivedFrom(c, n, u))
1552
+ return n;
1553
+ return c.neverType;
1554
+ }
1555
+ if (isTypeStrictSubtypeOf(c, u, n))
1556
+ return u;
1557
+ if (isTypeStrictSubtypeOf(c, n, u))
1558
+ return n;
1559
+ if (isTypeSubtypeOf(c, u, n))
1560
+ return u;
1561
+ if (isTypeSubtypeOf(c, n, u))
1562
+ return n;
1563
+ return c.neverType;
1564
+ });
1565
+ if ((directlyRelated.flags & 262144 /* TypeFlags.Never */) === 0)
1566
+ return directlyRelated;
1567
+ return mapType(c, t, (u) => {
1568
+ if (maybeTypeOfKind(u, 132644864 /* TypeFlags.Instantiable */)) {
1569
+ const constraint = getBaseConstraintOfType(c, u);
1570
+ if (constraint === undefined ||
1571
+ (checkDerived ? isTypeDerivedFrom(c, n, constraint) : isTypeSubtypeOf(c, n, constraint)))
1572
+ return getIntersectionType(c, [u, n]);
1573
+ }
1574
+ return c.neverType;
1575
+ });
1576
+ });
1577
+ if ((narrowedType.flags & 262144 /* TypeFlags.Never */) === 0)
1578
+ return narrowedType;
1579
+ if (isTypeSubtypeOf(c, candidate, t))
1580
+ return candidate;
1581
+ if (isTypeAssignableTo(c, t, candidate))
1582
+ return t;
1583
+ if (isTypeAssignableTo(c, candidate, t))
1584
+ return candidate;
1585
+ return getIntersectionType(c, [t, candidate]);
1586
+ }
1587
+ // ---- Discriminants
1588
+ function getDiscriminantPropertyAccess(c, f, expr, computedType) {
1589
+ if ((f.declaredType.flags & 134217728 /* TypeFlags.Union */) === 0 && (computedType.flags & 134217728 /* TypeFlags.Union */) === 0)
1590
+ return undefined;
1591
+ const access = getCandidateDiscriminantPropertyAccess(c, f, expr);
1592
+ if (access === undefined)
1593
+ return undefined;
1594
+ const name = getAccessedPropertyName(c, access);
1595
+ if (name === undefined)
1596
+ return undefined;
1597
+ const t = (f.declaredType.flags & 134217728 /* TypeFlags.Union */) !== 0 && isTypeSubsetOf(c, computedType, f.declaredType)
1598
+ ? f.declaredType
1599
+ : computedType;
1600
+ return isDiscriminantProperty(c, t, name) ? access : undefined;
1601
+ }
1602
+ function getCandidateDiscriminantPropertyAccess(c, f, expr) {
1603
+ const reference = f.reference;
1604
+ if (reference.kind === 207 /* SyntaxKind.ObjectBindingPattern */ ||
1605
+ reference.kind === 208 /* SyntaxKind.ArrayBindingPattern */ ||
1606
+ reference.kind === 219 /* SyntaxKind.FunctionExpression */ ||
1607
+ reference.kind === 220 /* SyntaxKind.ArrowFunction */ ||
1608
+ isObjectLiteralMethod(reference)) {
1609
+ // A pseudo-reference from getNarrowedTypeOfSymbol: a sibling destructured variable or parameter is a candidate.
1610
+ if (expr.kind !== 79 /* SyntaxKind.Identifier */)
1611
+ return undefined;
1612
+ const declaration = getExportSymbolOfValueSymbolIfExported(c, getResolvedSymbol(c, expr)).valueDeclaration;
1613
+ if (declaration !== undefined &&
1614
+ (declaration.kind === 209 /* SyntaxKind.BindingElement */ || declaration.kind === 170 /* SyntaxKind.Parameter */) &&
1615
+ declaration.parent === reference &&
1616
+ getInitializer(declaration) === undefined &&
1617
+ (declaration.kind === 209 /* SyntaxKind.BindingElement */
1618
+ ? declaration.dotDotDotToken
1619
+ : getDotDotDotToken(declaration)) === undefined)
1620
+ return declaration;
1621
+ return undefined;
1622
+ }
1623
+ if (isAccessExpression(expr))
1624
+ return isMatchingReference(c, reference, getExpression(expr)) ? expr : undefined;
1625
+ if (expr.kind === 79 /* SyntaxKind.Identifier */) {
1626
+ const symbol = getResolvedSymbol(c, expr);
1627
+ if (isConstantVariable(c, symbol)) {
1628
+ const declaration = symbol.valueDeclaration;
1629
+ // `const x = obj.kind` makes `x` an alias of `obj.kind`.
1630
+ const initializer = getCandidateVariableDeclarationInitializer(declaration);
1631
+ if (initializer !== undefined &&
1632
+ isAccessExpression(initializer) &&
1633
+ isMatchingReference(c, reference, getExpression(initializer)))
1634
+ return initializer;
1635
+ // `const { kind: x } = obj` makes `x` an alias of `obj.kind`.
1636
+ if (declaration !== undefined &&
1637
+ declaration.kind === 209 /* SyntaxKind.BindingElement */ &&
1638
+ getInitializer(declaration) === undefined) {
1639
+ const parentInitializer = getCandidateVariableDeclarationInitializer(declaration.parent.parent);
1640
+ if (parentInitializer !== undefined &&
1641
+ (parentInitializer.kind === 79 /* SyntaxKind.Identifier */ || isAccessExpression(parentInitializer)) &&
1642
+ isMatchingReference(c, reference, parentInitializer))
1643
+ return declaration;
1644
+ }
1645
+ }
1646
+ }
1647
+ return undefined;
1648
+ }
1649
+ function getCandidateVariableDeclarationInitializer(node) {
1650
+ if (node === undefined || node.kind !== 261 /* SyntaxKind.VariableDeclaration */ || getType(node) !== undefined)
1651
+ return undefined;
1652
+ const initializer = getInitializer(node);
1653
+ return initializer !== undefined ? skipParentheses(initializer) : undefined;
1654
+ }
1655
+ function narrowTypeByDiscriminant(c, t, access, narrow) {
1656
+ const propName = getAccessedPropertyName(c, access);
1657
+ if (propName === undefined)
1658
+ return t;
1659
+ const optionalChain = isOptionalChain(access);
1660
+ const removeNullable = c.strictNullChecks && (optionalChain || isNonNullAccess(access)) && maybeTypeOfKind(t, 12 /* TypeFlags.Nullable */);
1661
+ const nonNullType = removeNullable ? getTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */) : t;
1662
+ let propType = getTypeOfPropertyOfType(c, nonNullType, propName);
1663
+ if (propType === undefined)
1664
+ return t;
1665
+ if (removeNullable && optionalChain)
1666
+ propType = getOptionalType(c, propType, false);
1667
+ const narrowedPropType = narrow(propType);
1668
+ return filterType(c, t, (u) => {
1669
+ const discriminantType = getTypeOfPropertyOrIndexSignatureOfType(c, u, propName) ?? c.unknownType;
1670
+ return ((discriminantType.flags & 262144 /* TypeFlags.Never */) === 0 &&
1671
+ (narrowedPropType.flags & 262144 /* TypeFlags.Never */) === 0 &&
1672
+ areTypesComparable(c, narrowedPropType, discriminantType));
1673
+ });
1674
+ }
1675
+ function isNonNullAccess(node) {
1676
+ return isAccessExpression(node) && getExpression(node).kind === 236 /* SyntaxKind.NonNullExpression */;
1677
+ }
1678
+ function getTypeOfPropertyOrIndexSignatureOfType(c, t, name) {
1679
+ const propType = getTypeOfPropertyOfType(c, t, name);
1680
+ if (propType !== undefined)
1681
+ return propType;
1682
+ const info = getApplicableIndexInfoForName(c, t, name);
1683
+ return info !== undefined ? getOptionalType(c, info.valueType, true) : undefined;
1684
+ }
1685
+ function narrowTypeByDiscriminantProperty(c, t, access, operator, value, assumeTrue) {
1686
+ if ((operator === 36 /* SyntaxKind.EqualsEqualsEqualsToken */ || operator === 37 /* SyntaxKind.ExclamationEqualsEqualsToken */) &&
1687
+ (t.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1688
+ const keyPropertyName = getKeyPropertyName(c, t);
1689
+ if (keyPropertyName !== '' && keyPropertyName === getAccessedPropertyName(c, access)) {
1690
+ const candidate = getConstituentTypeForKeyType(c, t, getTypeOfExpression(c, value));
1691
+ if (candidate !== undefined) {
1692
+ if ((assumeTrue && operator === 36 /* SyntaxKind.EqualsEqualsEqualsToken */) ||
1693
+ (!assumeTrue && operator === 37 /* SyntaxKind.ExclamationEqualsEqualsToken */))
1694
+ return candidate;
1695
+ const propType = getTypeOfPropertyOfType(c, candidate, keyPropertyName);
1696
+ return propType !== undefined && isUnitType(propType) ? removeType(c, t, candidate) : t;
1697
+ }
1698
+ }
1699
+ }
1700
+ return narrowTypeByDiscriminant(c, t, access, (u) => narrowTypeByEquality(c, u, operator, value, assumeTrue));
1701
+ }
1702
+ /** The name of a property usable to index the constituents of a large union by literal values, or ''. */
1703
+ export function getKeyPropertyName(c, t) {
1704
+ const side = sideOf(c, t);
1705
+ if (side.keyPropertyName === '')
1706
+ computeKeyPropertyNameAndMap(c, t, side);
1707
+ return side.keyPropertyName === "\uFFFEmissing" /* InternalSymbolName.Missing */ ? '' : side.keyPropertyName;
1708
+ }
1709
+ export function getConstituentTypeForKeyType(c, t, keyType) {
1710
+ const result = sideOf(c, t).constituentMap?.get(getRegularTypeOfLiteralType(c, keyType));
1711
+ return result !== c.unknownType ? result : undefined;
1712
+ }
1713
+ /** Computes the key property of `t` and its constituents by key into `side`, the holder of its state. */
1714
+ function computeKeyPropertyNameAndMap(c, t, side) {
1715
+ side.keyPropertyName = "\uFFFEmissing" /* InternalSymbolName.Missing */;
1716
+ const types = t.types;
1717
+ let objectCount = 0;
1718
+ for (const u of types)
1719
+ if ((u.flags & (1048576 /* TypeFlags.Object */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */)) !== 0)
1720
+ objectCount++;
1721
+ if (types.length < 10 || (t.objectFlags & 32768 /* ObjectFlags.PrimitiveUnion */) !== 0 || objectCount < 10)
1722
+ return;
1723
+ let keyPropertyName = '';
1724
+ for (const u of types) {
1725
+ if (keyPropertyName !== '')
1726
+ break;
1727
+ if ((u.flags & (1048576 /* TypeFlags.Object */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */)) === 0)
1728
+ continue;
1729
+ for (const p of getPropertiesOfType(c, u)) {
1730
+ if (isUnitType(getTypeOfSymbol(c, p))) {
1731
+ keyPropertyName = p.name;
1732
+ break;
1733
+ }
1734
+ }
1735
+ }
1736
+ if (keyPropertyName === '')
1737
+ return;
1738
+ const map = new Map();
1739
+ let count = 0;
1740
+ for (const u of types) {
1741
+ if ((u.flags & (1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */)) === 0)
1742
+ continue;
1743
+ const discriminant = getTypeOfPropertyOfType(c, u, keyPropertyName);
1744
+ if (discriminant === undefined || !isLiteralType(discriminant))
1745
+ return;
1746
+ let duplicate = false;
1747
+ for (const d of (discriminant.flags & 134217728 /* TypeFlags.Union */) !== 0 ? typesOf(discriminant) : [discriminant]) {
1748
+ const key = getRegularTypeOfLiteralType(c, d);
1749
+ const existing = map.get(key);
1750
+ if (existing === undefined)
1751
+ map.set(key, u);
1752
+ else if (existing !== c.unknownType) {
1753
+ map.set(key, c.unknownType);
1754
+ duplicate = true;
1755
+ }
1756
+ }
1757
+ if (!duplicate)
1758
+ count++;
1759
+ }
1760
+ if (count >= 10 && count * 2 >= types.length) {
1761
+ side.keyPropertyName = keyPropertyName;
1762
+ side.constituentMap = map;
1763
+ }
1764
+ }
1765
+ // ---- Switch statements
1766
+ function getTypeAtSwitchClause(c, f, flow) {
1767
+ const statement = flow.switchStatement;
1768
+ const expr = skipParentheses(statement.expression);
1769
+ const flowType = getTypeAtFlowNode(c, f, flow.antecedent);
1770
+ const incomplete = f.incomplete;
1771
+ let t = flowType;
1772
+ if (isMatchingReference(c, f.reference, expr))
1773
+ t = narrowTypeBySwitchOnDiscriminant(c, t, flow);
1774
+ else if (expr.kind === 222 /* SyntaxKind.TypeOfExpression */ && isMatchingReference(c, f.reference, getExpression(expr)))
1775
+ t = narrowTypeBySwitchOnTypeOf(c, t, flow);
1776
+ else if (expr.kind === 111 /* SyntaxKind.TrueKeyword */)
1777
+ t = narrowTypeBySwitchOnTrue(c, f, t, flow);
1778
+ else {
1779
+ if (c.strictNullChecks) {
1780
+ if (optionalChainContainsReference(c, expr, f.reference))
1781
+ t = narrowTypeBySwitchOptionalChainContainment(c, t, flow, (u) => (u.flags & (4 /* TypeFlags.Undefined */ | 262144 /* TypeFlags.Never */)) === 0);
1782
+ else if (expr.kind === 222 /* SyntaxKind.TypeOfExpression */ &&
1783
+ optionalChainContainsReference(c, getExpression(expr), f.reference))
1784
+ t = narrowTypeBySwitchOptionalChainContainment(c, t, flow, (u) => !((u.flags & 262144 /* TypeFlags.Never */) !== 0 ||
1785
+ ((u.flags & 1024 /* TypeFlags.StringLiteral */) !== 0 && u.stringValue === 'undefined')));
1786
+ }
1787
+ const access = getDiscriminantPropertyAccess(c, f, expr, t);
1788
+ if (access !== undefined)
1789
+ t = narrowTypeBySwitchOnDiscriminantProperty(c, t, access, flow);
1790
+ }
1791
+ return newFlowType(c, f, t, incomplete);
1792
+ }
1793
+ function narrowTypeBySwitchOptionalChainContainment(c, t, flow, clauseCheck) {
1794
+ if (flow.clauseStart === flow.clauseEnd)
1795
+ return t;
1796
+ const clauseTypes = getSwitchClauseTypes(c, flow.switchStatement);
1797
+ for (let i = flow.clauseStart; i < flow.clauseEnd; i++)
1798
+ if (!clauseCheck(clauseTypes[i]))
1799
+ return t;
1800
+ return getTypeWithFacts(c, t, 2097152 /* TypeFacts.NEUndefinedOrNull */);
1801
+ }
1802
+ function narrowTypeBySwitchOnDiscriminantProperty(c, t, access, flow) {
1803
+ if (flow.clauseStart < flow.clauseEnd && (t.flags & 134217728 /* TypeFlags.Union */) !== 0) {
1804
+ const accessedName = getAccessedPropertyName(c, access);
1805
+ if (accessedName !== undefined && accessedName !== '' && getKeyPropertyName(c, t) === accessedName) {
1806
+ const clauseTypes = getSwitchClauseTypes(c, flow.switchStatement);
1807
+ const types = [];
1808
+ for (let i = flow.clauseStart; i < flow.clauseEnd && i < clauseTypes.length; i++)
1809
+ types.push(getConstituentTypeForKeyType(c, t, clauseTypes[i]) ?? c.unknownType);
1810
+ const candidate = getUnionType(c, types);
1811
+ if (candidate !== c.unknownType)
1812
+ return candidate;
1813
+ }
1814
+ }
1815
+ return narrowTypeByDiscriminant(c, t, access, (u) => narrowTypeBySwitchOnDiscriminant(c, u, flow));
1816
+ }
1817
+ function getClauses(statement) {
1818
+ return statement.caseBlock.clauses.nodes;
1819
+ }
1820
+ function getSwitchClauseTypes(c, statement) {
1821
+ const links = c.nodeLinks(statement);
1822
+ if (links.switchTypes === undefined) {
1823
+ const types = [];
1824
+ for (const clause of getClauses(statement)) {
1825
+ const expr = clause.expression;
1826
+ types.push(expr !== undefined ? getRegularTypeOfLiteralType(c, getTypeOfExpression(c, expr)) : c.neverType);
1827
+ }
1828
+ links.switchTypes = types;
1829
+ }
1830
+ return links.switchTypes;
1831
+ }
1832
+ function narrowTypeBySwitchOnDiscriminant(c, t, flow) {
1833
+ if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
1834
+ return narrowTypeBySwitchOnDiscriminantWorker(c, t, flow);
1835
+ // As in narrowTypeByEquality, each union is narrowed by each range of clauses once.
1836
+ const key = 'narrowedBySwitch' + t.id + ',' + getNodeId(flow.switchStatement) + ',' + flow.clauseStart + ',' + flow.clauseEnd;
1837
+ let result = c.cachedTypes.get(key);
1838
+ if (result === undefined) {
1839
+ result = narrowTypeBySwitchOnDiscriminantWorker(c, t, flow);
1840
+ c.cachedTypes.set(key, result);
1841
+ }
1842
+ return result;
1843
+ }
1844
+ function narrowTypeBySwitchOnDiscriminantWorker(c, t, flow) {
1845
+ const switchTypes = getSwitchClauseTypes(c, flow.switchStatement);
1846
+ if (switchTypes.length === 0)
1847
+ return t;
1848
+ const clauseTypes = switchTypes.slice(flow.clauseStart, flow.clauseEnd);
1849
+ const hasDefaultClause = flow.clauseStart === flow.clauseEnd || clauseTypes.includes(c.neverType);
1850
+ if ((t.flags & 2 /* TypeFlags.Unknown */) !== 0 && !hasDefaultClause) {
1851
+ const ground = [];
1852
+ for (const s of clauseTypes) {
1853
+ if ((s.flags & (12713980 /* TypeFlags.Primitive */ | 131072 /* TypeFlags.NonPrimitive */)) !== 0)
1854
+ ground.push(s);
1855
+ else if ((s.flags & 1048576 /* TypeFlags.Object */) !== 0)
1856
+ ground.push(c.nonPrimitiveType);
1857
+ else
1858
+ return t;
1859
+ }
1860
+ return getUnionType(c, ground);
1861
+ }
1862
+ const discriminantType = getUnionType(c, clauseTypes);
1863
+ const caseType = (discriminantType.flags & 262144 /* TypeFlags.Never */) !== 0
1864
+ ? c.neverType
1865
+ : replacePrimitivesWithLiterals(c, filterType(c, t, (u) => areTypesComparable(c, discriminantType, u)), discriminantType);
1866
+ if (!hasDefaultClause)
1867
+ return caseType;
1868
+ const defaultType = filterType(c, t, (u) => {
1869
+ const unit = extractUnitType(u);
1870
+ if (unit === undefined)
1871
+ return true;
1872
+ const v = (u.flags & 4 /* TypeFlags.Undefined */) !== 0 ? c.undefinedType : getRegularTypeOfLiteralType(c, unit);
1873
+ for (const st of switchTypes)
1874
+ if (isUnitType(st) && areTypesComparable(c, st, v))
1875
+ return false;
1876
+ return true;
1877
+ });
1878
+ if ((caseType.flags & 262144 /* TypeFlags.Never */) !== 0)
1879
+ return defaultType;
1880
+ return getUnionType(c, [caseType, defaultType]);
1881
+ }
1882
+ function getSwitchClauseTypeOfWitnesses(c, statement) {
1883
+ const links = c.nodeLinks(statement);
1884
+ if (links.switchWitnesses === undefined) {
1885
+ const witnesses = [];
1886
+ let valid = true;
1887
+ for (const clause of getClauses(statement)) {
1888
+ const expr = clause.expression;
1889
+ if (expr === undefined) {
1890
+ witnesses.push('');
1891
+ continue;
1892
+ }
1893
+ if (!isStringLiteralLike(expr)) {
1894
+ valid = false;
1895
+ break;
1896
+ }
1897
+ const text = getText(expr);
1898
+ witnesses.push(witnesses.includes(text) ? '' : text);
1899
+ }
1900
+ links.switchWitnesses = valid ? witnesses : [];
1901
+ links.switchWitnessesValid = valid;
1902
+ }
1903
+ return links.switchWitnessesValid ? links.switchWitnesses : undefined;
1904
+ }
1905
+ function getNotEqualFactsFromTypeofSwitch(start, end, witnesses) {
1906
+ let facts = 0 /* TypeFacts.None */;
1907
+ for (let i = 0; i < witnesses.length; i++)
1908
+ if ((i < start || i >= end) && witnesses[i] !== '')
1909
+ facts |= typeofNEFacts(witnesses[i]);
1910
+ return facts;
1911
+ }
1912
+ function getDefaultClauseIndex(statement) {
1913
+ const clauses = getClauses(statement);
1914
+ for (let i = 0; i < clauses.length; i++)
1915
+ if (clauses[i].kind === 298 /* SyntaxKind.DefaultClause */)
1916
+ return i;
1917
+ return -1;
1918
+ }
1919
+ function narrowTypeBySwitchOnTypeOf(c, t, flow) {
1920
+ const witnesses = getSwitchClauseTypeOfWitnesses(c, flow.switchStatement);
1921
+ if (witnesses === undefined)
1922
+ return t;
1923
+ const defaultIndex = getDefaultClauseIndex(flow.switchStatement);
1924
+ const hasDefaultClause = flow.clauseStart === flow.clauseEnd || (defaultIndex >= flow.clauseStart && defaultIndex < flow.clauseEnd);
1925
+ if (hasDefaultClause) {
1926
+ const notEqualFacts = getNotEqualFactsFromTypeofSwitch(flow.clauseStart, flow.clauseEnd, witnesses);
1927
+ return filterType(c, t, (u) => getTypeFacts(c, u, notEqualFacts) === notEqualFacts);
1928
+ }
1929
+ const types = [];
1930
+ for (const text of witnesses.slice(flow.clauseStart, flow.clauseEnd))
1931
+ types.push(text !== '' ? narrowTypeByTypeName(c, t, text) : c.neverType);
1932
+ return getUnionType(c, types);
1933
+ }
1934
+ function narrowTypeBySwitchOnTrue(c, f, typeIn, flow) {
1935
+ let t = typeIn;
1936
+ const clauses = getClauses(flow.switchStatement);
1937
+ const defaultIndex = getDefaultClauseIndex(flow.switchStatement);
1938
+ const hasDefaultClause = flow.clauseStart === flow.clauseEnd || (defaultIndex >= flow.clauseStart && defaultIndex < flow.clauseEnd);
1939
+ for (let i = 0; i < flow.clauseStart; i++) {
1940
+ const expr = clauses[i].expression;
1941
+ if (expr !== undefined)
1942
+ t = narrowType(c, f, t, expr, false);
1943
+ }
1944
+ if (hasDefaultClause) {
1945
+ for (let i = flow.clauseEnd; i < clauses.length; i++) {
1946
+ const expr = clauses[i].expression;
1947
+ if (expr !== undefined)
1948
+ t = narrowType(c, f, t, expr, false);
1949
+ }
1950
+ return t;
1951
+ }
1952
+ const types = [];
1953
+ for (let i = flow.clauseStart; i < flow.clauseEnd; i++) {
1954
+ const expr = clauses[i].expression;
1955
+ types.push(expr !== undefined ? narrowType(c, f, t, expr, true) : c.neverType);
1956
+ }
1957
+ return getUnionType(c, types);
1958
+ }
1959
+ /** Whether every value of the switch expression is handled by a case. */
1960
+ export function isExhaustiveSwitchStatement(c, statement) {
1961
+ const links = c.nodeLinks(statement);
1962
+ if (links.exhaustiveState === 0) {
1963
+ links.exhaustiveState = 1;
1964
+ const exhaustive = computeExhaustiveSwitchStatement(c, statement);
1965
+ if (links.exhaustiveState === 1)
1966
+ links.exhaustiveState = exhaustive ? 3 : 2;
1967
+ }
1968
+ else if (links.exhaustiveState === 1)
1969
+ links.exhaustiveState = 2;
1970
+ return links.exhaustiveState === 3;
1971
+ }
1972
+ function computeExhaustiveSwitchStatement(c, statement) {
1973
+ const expression = statement.expression;
1974
+ if (expression.kind === 222 /* SyntaxKind.TypeOfExpression */) {
1975
+ const witnesses = getSwitchClauseTypeOfWitnesses(c, statement);
1976
+ if (witnesses === undefined)
1977
+ return false;
1978
+ const operandConstraint = getBaseConstraintOrType(c, checkExpressionCached(c, getExpression(expression)));
1979
+ const notEqualFacts = getNotEqualFactsFromTypeofSwitch(0, 0, witnesses);
1980
+ if ((operandConstraint.flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0)
1981
+ return (556800 /* TypeFacts.AllTypeofNE */ & notEqualFacts) === 556800 /* TypeFacts.AllTypeofNE */;
1982
+ return !someType(operandConstraint, (u) => getTypeFacts(c, u, notEqualFacts) === notEqualFacts);
1983
+ }
1984
+ const t = getBaseConstraintOrType(c, checkExpressionCached(c, expression));
1985
+ if (!isLiteralType(t))
1986
+ return false;
1987
+ const switchTypes = getSwitchClauseTypes(c, statement);
1988
+ if (switchTypes.length === 0)
1989
+ return false;
1990
+ for (const s of switchTypes)
1991
+ if (!isUnitType(s) && (s.flags & 262144 /* TypeFlags.Never */) === 0)
1992
+ return false;
1993
+ const regular = mapType(c, t, (u) => getRegularTypeOfLiteralType(c, u));
1994
+ if ((regular.flags & 134217728 /* TypeFlags.Union */) !== 0)
1995
+ return typesOf(regular).every((u) => switchTypes.includes(u));
1996
+ return switchTypes.includes(regular);
1997
+ }
1998
+ function getBaseConstraintOrType(c, t) {
1999
+ return getBaseConstraintOfType(c, t) ?? t;
2000
+ }
2001
+ // ---- References
2002
+ export function isMatchingReference(c, source, target) {
2003
+ // A declaration's symbol has the declaration's name, and an identifier's text is its name: an
2004
+ // identifier never refers to the variable or binding element of a declaration of another name.
2005
+ if (source.kind === 79 /* SyntaxKind.Identifier */ &&
2006
+ (target.kind === 261 /* SyntaxKind.VariableDeclaration */ || target.kind === 209 /* SyntaxKind.BindingElement */)) {
2007
+ // A cast to one class: a union of the two would be boxed in native builds.
2008
+ const name = target.kind === 261 /* SyntaxKind.VariableDeclaration */
2009
+ ? target.name
2010
+ : target.name;
2011
+ if (name !== undefined &&
2012
+ name.kind === 79 /* SyntaxKind.Identifier */ &&
2013
+ name.text !== source.text)
2014
+ return false;
2015
+ }
2016
+ switch (target.kind) {
2017
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
2018
+ case 236 /* SyntaxKind.NonNullExpression */:
2019
+ return isMatchingReference(c, source, getExpression(target));
2020
+ case 227 /* SyntaxKind.BinaryExpression */: {
2021
+ const binary = target;
2022
+ return ((isAssignmentExpression(target, false) && isMatchingReference(c, source, binary.left)) ||
2023
+ (binary.operatorToken.kind === 27 /* SyntaxKind.CommaToken */ && isMatchingReference(c, source, binary.right)));
2024
+ }
2025
+ }
2026
+ switch (source.kind) {
2027
+ case 79 /* SyntaxKind.Identifier */:
2028
+ case 80 /* SyntaxKind.PrivateIdentifier */:
2029
+ if (isThisInTypeQuery(source))
2030
+ return target.kind === 109 /* SyntaxKind.ThisKeyword */;
2031
+ if (target.kind === 79 /* SyntaxKind.Identifier */)
2032
+ return getResolvedSymbol(c, source) === getResolvedSymbol(c, target);
2033
+ if (target.kind === 261 /* SyntaxKind.VariableDeclaration */ || target.kind === 209 /* SyntaxKind.BindingElement */)
2034
+ return (getExportSymbolOfValueSymbolIfExported(c, getResolvedSymbol(c, source)) === getSymbolOfDeclaration(c, target));
2035
+ return false;
2036
+ case 109 /* SyntaxKind.ThisKeyword */:
2037
+ return target.kind === 109 /* SyntaxKind.ThisKeyword */;
2038
+ case 107 /* SyntaxKind.SuperKeyword */:
2039
+ return target.kind === 107 /* SyntaxKind.SuperKeyword */;
2040
+ case 237 /* SyntaxKind.MetaProperty */:
2041
+ return (target.kind === 237 /* SyntaxKind.MetaProperty */ &&
2042
+ source.keywordToken === target.keywordToken &&
2043
+ source.name.text === target.name.text);
2044
+ case 236 /* SyntaxKind.NonNullExpression */:
2045
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
2046
+ case 239 /* SyntaxKind.SatisfiesExpression */:
2047
+ return isMatchingReference(c, getExpression(source), target);
2048
+ case 212 /* SyntaxKind.PropertyAccessExpression */:
2049
+ case 213 /* SyntaxKind.ElementAccessExpression */: {
2050
+ const sourceName = getAccessedPropertyName(c, source);
2051
+ if (sourceName !== undefined && isAccessExpression(target)) {
2052
+ const targetName = getAccessedPropertyName(c, target);
2053
+ if (targetName !== undefined)
2054
+ return targetName === sourceName && isMatchingReference(c, getExpression(source), getExpression(target));
2055
+ }
2056
+ // `obj[key]` matches `obj[key]` for a key that isn't reassigned.
2057
+ if (source.kind === 213 /* SyntaxKind.ElementAccessExpression */ && target.kind === 213 /* SyntaxKind.ElementAccessExpression */) {
2058
+ const sourceArg = source.argumentExpression;
2059
+ const targetArg = target.argumentExpression;
2060
+ if (sourceArg.kind === 79 /* SyntaxKind.Identifier */ && targetArg.kind === 79 /* SyntaxKind.Identifier */) {
2061
+ const symbol = getResolvedSymbol(c, sourceArg);
2062
+ if (symbol === getResolvedSymbol(c, targetArg) && isUnreassignedVariable(c, symbol))
2063
+ return isMatchingReference(c, getExpression(source), getExpression(target));
2064
+ }
2065
+ }
2066
+ return false;
2067
+ }
2068
+ case 167 /* SyntaxKind.QualifiedName */:
2069
+ if (isAccessExpression(target)) {
2070
+ const targetName = getAccessedPropertyName(c, target);
2071
+ if (targetName !== undefined)
2072
+ return (source.right.text === targetName &&
2073
+ isMatchingReference(c, source.left, getExpression(target)));
2074
+ }
2075
+ return false;
2076
+ case 227 /* SyntaxKind.BinaryExpression */:
2077
+ return (source.operatorToken.kind === 27 /* SyntaxKind.CommaToken */ &&
2078
+ isMatchingReference(c, source.right, target));
2079
+ }
2080
+ return false;
2081
+ }
2082
+ /** Whether `expr` is `reference.constructor`. */
2083
+ function isMatchingConstructorReference(c, f, expr) {
2084
+ return (isAccessExpression(expr) &&
2085
+ getAccessedPropertyName(c, expr) === 'constructor' &&
2086
+ isMatchingReference(c, f.reference, getExpression(expr)));
2087
+ }
2088
+ /** Narrows by `x.constructor === C` to the constituents constructed by `C`. */
2089
+ function narrowTypeByConstructor(c, t, operator, identifier, assumeTrue) {
2090
+ // Inequality doesn't narrow.
2091
+ if (assumeTrue
2092
+ ? operator !== 34 /* SyntaxKind.EqualsEqualsToken */ && operator !== 36 /* SyntaxKind.EqualsEqualsEqualsToken */
2093
+ : operator !== 35 /* SyntaxKind.ExclamationEqualsToken */ && operator !== 37 /* SyntaxKind.ExclamationEqualsEqualsToken */)
2094
+ return t;
2095
+ const identifierType = getTypeOfExpression(c, identifier);
2096
+ if (!((identifierType.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
2097
+ getSignaturesOfType(c, identifierType, 0 /* SignatureKind.Call */).length > 0) &&
2098
+ !isConstructorType(c, identifierType))
2099
+ return t;
2100
+ const prototypeProperty = getPropertyOfType(c, identifierType, 'prototype');
2101
+ if (prototypeProperty === undefined)
2102
+ return t;
2103
+ const prototypeType = getTypeOfSymbol(c, prototypeProperty);
2104
+ if ((prototypeType.flags & 1 /* TypeFlags.Any */) !== 0 ||
2105
+ prototypeType === c.globalObjectType ||
2106
+ prototypeType === c.globalFunctionType)
2107
+ return t;
2108
+ if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
2109
+ return prototypeType;
2110
+ return filterType(c, t, (u) => isConstructedBy(c, u, prototypeType));
2111
+ }
2112
+ function isConstructedBy(c, source, target) {
2113
+ // Class instances are only constructed by their own class, even when structurally identical to another.
2114
+ if (((source.flags & 1048576 /* TypeFlags.Object */) !== 0 && (source.objectFlags & 1 /* ObjectFlags.Class */) !== 0) ||
2115
+ ((target.flags & 1048576 /* TypeFlags.Object */) !== 0 && (target.objectFlags & 1 /* ObjectFlags.Class */) !== 0))
2116
+ return source.symbol === target.symbol;
2117
+ return isTypeSubtypeOf(c, source, target);
2118
+ }
2119
+ export function getAccessedPropertyName(c, access) {
2120
+ if (access.kind === 212 /* SyntaxKind.PropertyAccessExpression */)
2121
+ return getText(access.name);
2122
+ if (access.kind === 209 /* SyntaxKind.BindingElement */)
2123
+ return getDestructuringPropertyName(c, access);
2124
+ if (access.kind === 213 /* SyntaxKind.ElementAccessExpression */) {
2125
+ const argument = access.argumentExpression;
2126
+ if (isStringLiteralLike(argument))
2127
+ return getText(argument);
2128
+ if (argument.kind === 8 /* SyntaxKind.NumericLiteral */)
2129
+ return String(Number(getText(argument)));
2130
+ if (isEntityNameExpression(argument, false))
2131
+ return tryGetNameFromEntityNameExpression(c, argument);
2132
+ }
2133
+ if (access.kind === 170 /* SyntaxKind.Parameter */)
2134
+ return String(getParameters(access.parent).nodes.indexOf(access));
2135
+ return undefined;
2136
+ }
2137
+ /** The property name a constant or enum member used as an index stands for. */
2138
+ export function tryGetNameFromEntityNameExpression(c, node) {
2139
+ const symbol = resolveEntityName(c, node, 111551 /* SymbolFlags.Value */, true, false, undefined);
2140
+ if (symbol === undefined || !(isConstantVariable(c, symbol) || (symbol.flags & 8 /* SymbolFlags.EnumMember */) !== 0))
2141
+ return undefined;
2142
+ const declaration = symbol.valueDeclaration;
2143
+ if (declaration === undefined)
2144
+ return undefined;
2145
+ const typeNode = getType(declaration);
2146
+ if (typeNode !== undefined) {
2147
+ const name = tryGetNameFromType(getTypeFromTypeNode(c, typeNode));
2148
+ if (name !== undefined)
2149
+ return name;
2150
+ }
2151
+ // Binding elements are excluded: their initializers don't alone determine their types, and resolving
2152
+ // their full types can be circular.
2153
+ if ((declaration.kind === 261 /* SyntaxKind.VariableDeclaration */ ||
2154
+ declaration.kind === 173 /* SyntaxKind.PropertyDeclaration */ ||
2155
+ declaration.kind === 303 /* SyntaxKind.PropertyAssignment */ ||
2156
+ declaration.kind === 170 /* SyntaxKind.Parameter */ ||
2157
+ declaration.kind === 306 /* SyntaxKind.EnumMember */) &&
2158
+ isBlockScopedNameDeclaredBeforeUse(c, declaration, node)) {
2159
+ const initializer = getInitializer(declaration);
2160
+ if (initializer !== undefined)
2161
+ return tryGetNameFromType(getTypeOfExpression(c, initializer));
2162
+ if (declaration.kind === 306 /* SyntaxKind.EnumMember */) {
2163
+ const name = getName(declaration);
2164
+ if (isStringLiteralLike(name) || name.kind === 8 /* SyntaxKind.NumericLiteral */ || name.kind === 79 /* SyntaxKind.Identifier */)
2165
+ return getText(name);
2166
+ }
2167
+ }
2168
+ return undefined;
2169
+ }
2170
+ export function tryGetNameFromType(t) {
2171
+ return (t.flags & (16384 /* TypeFlags.UniqueESSymbol */ | 3072 /* TypeFlags.StringOrNumberLiteral */)) !== 0
2172
+ ? getPropertyNameFromType(t)
2173
+ : undefined;
2174
+ }
2175
+ /** The property name or element index a destructuring element reads. */
2176
+ export function getDestructuringPropertyName(c, node) {
2177
+ const parent = node.parent;
2178
+ let name;
2179
+ if (node.kind === 209 /* SyntaxKind.BindingElement */ && parent.kind === 207 /* SyntaxKind.ObjectBindingPattern */)
2180
+ name = node.propertyName ?? node.name;
2181
+ else if (node.kind === 303 /* SyntaxKind.PropertyAssignment */ || node.kind === 304 /* SyntaxKind.ShorthandPropertyAssignment */)
2182
+ name = getName(node);
2183
+ else if (parent.kind === 210 /* SyntaxKind.ArrayLiteralExpression */ || parent.kind === 208 /* SyntaxKind.ArrayBindingPattern */)
2184
+ return String(getElements(parent).nodes.indexOf(node));
2185
+ if (name === undefined)
2186
+ return undefined;
2187
+ const t = getLiteralTypeFromPropertyName(c, name);
2188
+ return t !== undefined && (t.flags & (1024 /* TypeFlags.StringLiteral */ | 2048 /* TypeFlags.NumberLiteral */)) !== 0
2189
+ ? getPropertyNameFromType(t)
2190
+ : undefined;
2191
+ }
2192
+ export function isConstantReference(c, node) {
2193
+ switch (node.kind) {
2194
+ case 109 /* SyntaxKind.ThisKeyword */:
2195
+ return true;
2196
+ case 79 /* SyntaxKind.Identifier */: {
2197
+ if (isThisInTypeQuery(node))
2198
+ return false;
2199
+ const symbol = getResolvedSymbol(c, node);
2200
+ return (isConstantVariable(c, symbol) ||
2201
+ (isParameterOrMutableLocalVariable(c, symbol) && !isSymbolAssigned(c, symbol)) ||
2202
+ (symbol.valueDeclaration !== undefined && symbol.valueDeclaration.kind === 219 /* SyntaxKind.FunctionExpression */));
2203
+ }
2204
+ case 212 /* SyntaxKind.PropertyAccessExpression */:
2205
+ case 213 /* SyntaxKind.ElementAccessExpression */: {
2206
+ if (!isConstantReference(c, getExpression(node)))
2207
+ return false;
2208
+ const symbol = c.resolvedSymbols.get(node.id);
2209
+ return symbol !== undefined && isReadonlySymbol(c, symbol);
2210
+ }
2211
+ case 207 /* SyntaxKind.ObjectBindingPattern */:
2212
+ case 208 /* SyntaxKind.ArrayBindingPattern */: {
2213
+ // A destructuring pseudo-reference is constant when none of its variables are reassigned.
2214
+ const rootDeclaration = getRootDeclaration(node.parent);
2215
+ if (rootDeclaration.kind === 170 /* SyntaxKind.Parameter */ ||
2216
+ (rootDeclaration.kind === 261 /* SyntaxKind.VariableDeclaration */ &&
2217
+ rootDeclaration.parent.kind === 300 /* SyntaxKind.CatchClause */))
2218
+ return !isSomeSymbolAssigned(c, getName(rootDeclaration));
2219
+ if (rootDeclaration.kind !== 261 /* SyntaxKind.VariableDeclaration */)
2220
+ return false;
2221
+ const blockScopeKind = getCombinedNodeFlags(rootDeclaration) & 7 /* NodeFlags.BlockScoped */;
2222
+ return (blockScopeKind === 2 /* NodeFlags.Const */ ||
2223
+ blockScopeKind === 4 /* NodeFlags.Using */ ||
2224
+ blockScopeKind === 6 /* NodeFlags.AwaitUsing */);
2225
+ }
2226
+ }
2227
+ return false;
2228
+ }
2229
+ function containsMatchingReference(c, sourceIn, target) {
2230
+ let source = sourceIn;
2231
+ while (isAccessExpression(source)) {
2232
+ source = getExpression(source);
2233
+ if (isMatchingReference(c, source, target))
2234
+ return true;
2235
+ }
2236
+ return false;
2237
+ }
2238
+ function optionalChainContainsReference(c, sourceIn, target) {
2239
+ let source = sourceIn;
2240
+ while (isOptionalChain(source)) {
2241
+ source = getExpression(source);
2242
+ if (isMatchingReference(c, source, target))
2243
+ return true;
2244
+ }
2245
+ return false;
2246
+ }
2247
+ function getReferenceCandidate(node) {
2248
+ switch (node.kind) {
2249
+ case 218 /* SyntaxKind.ParenthesizedExpression */:
2250
+ return getReferenceCandidate(getExpression(node));
2251
+ case 227 /* SyntaxKind.BinaryExpression */: {
2252
+ const binary = node;
2253
+ switch (binary.operatorToken.kind) {
2254
+ case 63 /* SyntaxKind.EqualsToken */:
2255
+ case 75 /* SyntaxKind.BarBarEqualsToken */:
2256
+ case 76 /* SyntaxKind.AmpersandAmpersandEqualsToken */:
2257
+ case 77 /* SyntaxKind.QuestionQuestionEqualsToken */:
2258
+ return getReferenceCandidate(binary.left);
2259
+ case 27 /* SyntaxKind.CommaToken */:
2260
+ return getReferenceCandidate(binary.right);
2261
+ }
2262
+ }
2263
+ }
2264
+ return node;
2265
+ }
2266
+ export function hasMatchingArgument(c, call, reference) {
2267
+ const args = getArguments(call);
2268
+ if (args !== undefined) {
2269
+ for (const argument of args.nodes)
2270
+ if (isOrContainsMatchingReference(c, reference, argument) ||
2271
+ optionalChainContainsReference(c, argument, reference))
2272
+ return true;
2273
+ }
2274
+ const callee = getExpression(call);
2275
+ return (callee.kind === 212 /* SyntaxKind.PropertyAccessExpression */ &&
2276
+ isOrContainsMatchingReference(c, reference, getExpression(callee)));
2277
+ }
2278
+ function isOrContainsMatchingReference(c, source, target) {
2279
+ return isMatchingReference(c, source, target) || containsMatchingReference(c, source, target);
2280
+ }
2281
+ function isCallChain(node) {
2282
+ return node.kind === 214 /* SyntaxKind.CallExpression */ && isOptionalChain(node);
2283
+ }
2284
+ // ---- Reachability
2285
+ export function isReachableFlowNode(c, flow) {
2286
+ const result = isReachableFlowNodeWorker(c, flow, false);
2287
+ c.lastFlowNode = flow;
2288
+ c.lastFlowNodeReachable = result;
2289
+ return result;
2290
+ }
2291
+ function isReachableFlowNodeWorker(c, start, noCacheCheckIn) {
2292
+ let flow = start;
2293
+ let noCacheCheck = noCacheCheckIn;
2294
+ for (;;) {
2295
+ if (flow === c.lastFlowNode)
2296
+ return c.lastFlowNodeReachable;
2297
+ const flags = flow.flags;
2298
+ if ((flags & 4096 /* FlowFlags.Shared */) !== 0) {
2299
+ if (!noCacheCheck) {
2300
+ const cached = c.flowNodeReachable.get(flow);
2301
+ if (cached !== undefined)
2302
+ return cached;
2303
+ const reachable = isReachableFlowNodeWorker(c, flow, true);
2304
+ c.flowNodeReachable.set(flow, reachable);
2305
+ return reachable;
2306
+ }
2307
+ noCacheCheck = false;
2308
+ }
2309
+ if ((flags & (16 /* FlowFlags.Assignment */ | 96 /* FlowFlags.Condition */ | 256 /* FlowFlags.ArrayMutation */)) !== 0)
2310
+ flow = flow.antecedent;
2311
+ else if ((flags & 512 /* FlowFlags.Call */) !== 0) {
2312
+ const signature = getEffectsSignature(c, flow.node);
2313
+ if (signature !== undefined) {
2314
+ const predicate = getTypePredicateOfSignature(c, signature);
2315
+ if (predicate !== undefined &&
2316
+ predicate.kind === 3 /* TypePredicateKind.AssertsIdentifier */ &&
2317
+ predicate.type === undefined) {
2318
+ const args = getArguments(flow.node).nodes;
2319
+ if (predicate.parameterIndex >= 0 &&
2320
+ predicate.parameterIndex < args.length &&
2321
+ isFalseExpression(args[predicate.parameterIndex]))
2322
+ return false;
2323
+ }
2324
+ if ((getReturnTypeOfSignature(c, signature).flags & 262144 /* TypeFlags.Never */) !== 0)
2325
+ return false;
2326
+ }
2327
+ flow = flow.antecedent;
2328
+ }
2329
+ else if ((flags & 4 /* FlowFlags.BranchLabel */) !== 0) {
2330
+ for (const antecedent of getBranchLabelAntecedents(c, flow))
2331
+ if (isReachableFlowNodeWorker(c, antecedent, false))
2332
+ return true;
2333
+ return false;
2334
+ }
2335
+ else if ((flags & 8 /* FlowFlags.LoopLabel */) !== 0) {
2336
+ const antecedents = flow.antecedents;
2337
+ if (antecedents === undefined || antecedents.length === 0)
2338
+ return false;
2339
+ flow = antecedents[0];
2340
+ }
2341
+ else if ((flags & 128 /* FlowFlags.SwitchClause */) !== 0) {
2342
+ const clause = flow;
2343
+ if (clause.isEmpty() && isExhaustiveSwitchStatement(c, clause.switchStatement))
2344
+ return false;
2345
+ flow = flow.antecedent;
2346
+ }
2347
+ else if ((flags & 1024 /* FlowFlags.ReduceLabel */) !== 0) {
2348
+ c.lastFlowNode = undefined;
2349
+ c.flowReduceLabels.push(flow);
2350
+ const result = isReachableFlowNodeWorker(c, flow.antecedent, false);
2351
+ c.flowReduceLabels.pop();
2352
+ return result;
2353
+ }
2354
+ else
2355
+ return (flags & 1 /* FlowFlags.Unreachable */) === 0;
2356
+ }
2357
+ }
2358
+ function isFalseExpression(expr) {
2359
+ const node = skipParentheses(expr);
2360
+ if (node.kind === 96 /* SyntaxKind.FalseKeyword */)
2361
+ return true;
2362
+ if (node.kind === 227 /* SyntaxKind.BinaryExpression */) {
2363
+ const binary = node;
2364
+ if (binary.operatorToken.kind === 55 /* SyntaxKind.AmpersandAmpersandToken */)
2365
+ return isFalseExpression(binary.left) || isFalseExpression(binary.right);
2366
+ if (binary.operatorToken.kind === 56 /* SyntaxKind.BarBarToken */)
2367
+ return isFalseExpression(binary.left) && isFalseExpression(binary.right);
2368
+ }
2369
+ return false;
2370
+ }
2371
+ export function getAssignmentTargetKind(node) {
2372
+ const target = getAssignmentTarget(node);
2373
+ if (target === undefined)
2374
+ return 0 /* AssignmentKind.None */;
2375
+ if (target.kind === 227 /* SyntaxKind.BinaryExpression */) {
2376
+ const op = target.operatorToken.kind;
2377
+ return op === 63 /* SyntaxKind.EqualsToken */ || isLogicalOrCoalescingAssignmentOperator(op)
2378
+ ? 1 /* AssignmentKind.Definite */
2379
+ : 2 /* AssignmentKind.Compound */;
2380
+ }
2381
+ if (target.kind === 225 /* SyntaxKind.PrefixUnaryExpression */ || target.kind === 226 /* SyntaxKind.PostfixUnaryExpression */)
2382
+ return 2 /* AssignmentKind.Compound */;
2383
+ return 1 /* AssignmentKind.Definite */;
2384
+ }
2385
+ export function isInCompoundLikeAssignment(node) {
2386
+ const target = getAssignmentTarget(node);
2387
+ if (target === undefined || !isAssignmentExpression(target, true))
2388
+ return false;
2389
+ const right = skipParentheses(target.right);
2390
+ return (right.kind === 227 /* SyntaxKind.BinaryExpression */ &&
2391
+ isShiftOperatorOrHigher(right.operatorToken.kind));
2392
+ }
2393
+ function isShiftOperatorOrHigher(kind) {
2394
+ switch (kind) {
2395
+ case 47 /* SyntaxKind.LessThanLessThanToken */:
2396
+ case 48 /* SyntaxKind.GreaterThanGreaterThanToken */:
2397
+ case 49 /* SyntaxKind.GreaterThanGreaterThanGreaterThanToken */:
2398
+ case 41 /* SyntaxKind.AsteriskToken */:
2399
+ case 43 /* SyntaxKind.SlashToken */:
2400
+ case 44 /* SyntaxKind.PercentToken */:
2401
+ case 39 /* SyntaxKind.PlusToken */:
2402
+ case 40 /* SyntaxKind.MinusToken */:
2403
+ case 42 /* SyntaxKind.AsteriskAsteriskToken */:
2404
+ return true;
2405
+ }
2406
+ return false;
2407
+ }
2408
+ function isUnreassignedVariable(c, symbol) {
2409
+ return (isConstantVariable(c, symbol) || (isParameterOrMutableLocalVariable(c, symbol) && !isSymbolAssigned(c, symbol)));
2410
+ }
2411
+ export function isConstantVariable(_c, symbol) {
2412
+ if ((symbol.flags & 3 /* SymbolFlags.Variable */) === 0 || symbol.valueDeclaration === undefined)
2413
+ return false;
2414
+ return (getCombinedNodeFlags(symbol.valueDeclaration) & 6 /* NodeFlags.Constant */) !== 0;
2415
+ }
2416
+ export function isParameterOrMutableLocalVariable(_c, symbol) {
2417
+ if (symbol.valueDeclaration === undefined)
2418
+ return false;
2419
+ const declaration = getRootDeclaration(symbol.valueDeclaration);
2420
+ return (declaration.kind === 170 /* SyntaxKind.Parameter */ ||
2421
+ (declaration.kind === 261 /* SyntaxKind.VariableDeclaration */ &&
2422
+ (declaration.parent.kind === 300 /* SyntaxKind.CatchClause */ || isMutableLocalVariableDeclaration(declaration))));
2423
+ }
2424
+ export function isMutableLocalVariableDeclaration(declaration) {
2425
+ const list = declaration.parent;
2426
+ if ((list.flags & 1 /* NodeFlags.Let */) === 0)
2427
+ return false;
2428
+ if ((getCombinedModifierFlags(declaration) & 32 /* ModifierFlags.Export */) !== 0)
2429
+ return false;
2430
+ const statement = list.parent;
2431
+ return !(statement.kind === 244 /* SyntaxKind.VariableStatement */ && isGlobalSourceFile(statement.parent));
2432
+ }
2433
+ export function isSymbolAssigned(c, symbol) {
2434
+ ensureAssignmentsMarked(c, symbol);
2435
+ return c.symbolLinks(symbol).lastAssignmentPos !== 0;
2436
+ }
2437
+ export function isSymbolAssignedDefinitely(c, symbol) {
2438
+ ensureAssignmentsMarked(c, symbol);
2439
+ return c.symbolLinks(symbol).hasDefiniteAssignment;
2440
+ }
2441
+ /** Whether there are no assignments to `symbol`, or `location` is past the last one. */
2442
+ export function isPastLastAssignment(c, symbol, location) {
2443
+ ensureAssignmentsMarked(c, symbol);
2444
+ const pos = c.symbolLinks(symbol).lastAssignmentPos;
2445
+ return pos === 0 || (location !== undefined && pos < location.pos);
2446
+ }
2447
+ function isFunctionOrSourceFile(node) {
2448
+ return node.kind === 307 /* SyntaxKind.SourceFile */ || isFunctionLike(node);
2449
+ }
2450
+ function findFunctionOrSourceFile(node) {
2451
+ let current = node;
2452
+ while (current !== undefined && !isFunctionOrSourceFile(current))
2453
+ current = current.parent;
2454
+ return current;
2455
+ }
2456
+ function ensureAssignmentsMarked(c, symbol) {
2457
+ if (c.symbolLinks(symbol).lastAssignmentPos !== 0)
2458
+ return;
2459
+ const parent = findFunctionOrSourceFile(symbol.valueDeclaration);
2460
+ if (parent === undefined)
2461
+ return;
2462
+ const links = c.nodeLinks(parent);
2463
+ if ((links.flags & 131072 /* NodeCheckFlags.AssignmentsMarked */) !== 0)
2464
+ return;
2465
+ links.flags |= 131072 /* NodeCheckFlags.AssignmentsMarked */;
2466
+ // An enclosing function already marked everything inside it.
2467
+ let outer = parent.parent;
2468
+ while (outer !== undefined) {
2469
+ if (isFunctionOrSourceFile(outer) &&
2470
+ ((c.existingNodeLinks(outer)?.flags ?? 0) & 131072 /* NodeCheckFlags.AssignmentsMarked */) !== 0)
2471
+ return;
2472
+ outer = outer.parent;
2473
+ }
2474
+ c.assignmentMarker.visitNode(parent);
2475
+ }
2476
+ const maxAssignmentPos = 2147483647;
2477
+ /** Records the last assignment position of parameters and mutable locals in one walk. */
2478
+ export class AssignmentMarker extends NodeVisitor {
2479
+ c;
2480
+ constructor(c) {
2481
+ super();
2482
+ this.c = c;
2483
+ }
2484
+ visitNode(node) {
2485
+ const c = this.c;
2486
+ switch (node.kind) {
2487
+ case 79 /* SyntaxKind.Identifier */: {
2488
+ const kind = getAssignmentTargetKind(node);
2489
+ if (kind === 0 /* AssignmentKind.None */)
2490
+ return false;
2491
+ const symbol = getResolvedSymbol(c, node);
2492
+ if (!isParameterOrMutableLocalVariable(c, symbol))
2493
+ return false;
2494
+ const links = c.symbolLinks(symbol);
2495
+ if (links.lastAssignmentPos !== maxAssignmentPos) {
2496
+ const referencing = findFunctionOrSourceFile(node);
2497
+ const declaring = findFunctionOrSourceFile(symbol.valueDeclaration);
2498
+ links.lastAssignmentPos =
2499
+ referencing === declaring ? extendAssignmentPosition(node, symbol.valueDeclaration) : maxAssignmentPos;
2500
+ }
2501
+ if (kind === 1 /* AssignmentKind.Definite */)
2502
+ links.hasDefiniteAssignment = true;
2503
+ return false;
2504
+ }
2505
+ case 282 /* SyntaxKind.ExportSpecifier */: {
2506
+ const specifier = node;
2507
+ const declaration = specifier.parent.parent;
2508
+ const name = specifier.propertyName ?? specifier.name;
2509
+ if (!specifier.isTypeOnly &&
2510
+ !declaration.isTypeOnly &&
2511
+ declaration.moduleSpecifier === undefined &&
2512
+ name.kind !== 10 /* SyntaxKind.StringLiteral */) {
2513
+ const symbol = resolveEntityName(c, name, 111551 /* SymbolFlags.Value */, true, true, undefined);
2514
+ if (symbol !== undefined && isParameterOrMutableLocalVariable(c, symbol))
2515
+ c.symbolLinks(symbol).lastAssignmentPos = maxAssignmentPos;
2516
+ }
2517
+ return false;
2518
+ }
2519
+ case 265 /* SyntaxKind.InterfaceDeclaration */:
2520
+ case 266 /* SyntaxKind.TypeAliasDeclaration */:
2521
+ case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
2522
+ case 267 /* SyntaxKind.EnumDeclaration */:
2523
+ return false;
2524
+ }
2525
+ if (node.kind >= 183 /* SyntaxKind.FirstTypeNode */ && node.kind <= 206 /* SyntaxKind.LastTypeNode */)
2526
+ return false;
2527
+ return node.forEachChild(this);
2528
+ }
2529
+ }
2530
+ function extendAssignmentPosition(nodeIn, declaration) {
2531
+ let node = nodeIn;
2532
+ let pos = nodeIn.pos;
2533
+ while (node !== undefined && node.pos > declaration.pos) {
2534
+ switch (node.kind) {
2535
+ case 244 /* SyntaxKind.VariableStatement */:
2536
+ case 245 /* SyntaxKind.ExpressionStatement */:
2537
+ case 246 /* SyntaxKind.IfStatement */:
2538
+ case 247 /* SyntaxKind.DoStatement */:
2539
+ case 248 /* SyntaxKind.WhileStatement */:
2540
+ case 249 /* SyntaxKind.ForStatement */:
2541
+ case 250 /* SyntaxKind.ForInStatement */:
2542
+ case 251 /* SyntaxKind.ForOfStatement */:
2543
+ case 255 /* SyntaxKind.WithStatement */:
2544
+ case 256 /* SyntaxKind.SwitchStatement */:
2545
+ case 259 /* SyntaxKind.TryStatement */:
2546
+ case 264 /* SyntaxKind.ClassDeclaration */:
2547
+ pos = node.end;
2548
+ }
2549
+ node = node.parent;
2550
+ }
2551
+ return pos;
2552
+ }
2553
+ /** The function, module block, source file or property declaration whose flow graph contains `node`. */
2554
+ export function getControlFlowContainer(node) {
2555
+ let current = node.parent;
2556
+ for (;;) {
2557
+ if ((isFunctionLike(current) && getImmediatelyInvokedFunctionExpression(current) === undefined) ||
2558
+ current.kind === 269 /* SyntaxKind.ModuleBlock */ ||
2559
+ current.kind === 307 /* SyntaxKind.SourceFile */ ||
2560
+ current.kind === 173 /* SyntaxKind.PropertyDeclaration */)
2561
+ return current;
2562
+ current = current.parent;
2563
+ }
2564
+ }
2565
+ /** Array mutations of an auto-typed array (`x.push(...)`, `x[i] = ...`) on the way to its references. */
2566
+ function getTypeAtFlowArrayMutation(c, f, flow) {
2567
+ if (f.declaredType !== c.autoType && f.declaredType !== c.autoArrayType)
2568
+ return undefined;
2569
+ const node = flow.node;
2570
+ const expr = node.kind === 214 /* SyntaxKind.CallExpression */
2571
+ ? getExpression(getExpression(node))
2572
+ : getExpression(node.left);
2573
+ if (!isMatchingReference(c, f.reference, getReferenceCandidate(expr)))
2574
+ return undefined;
2575
+ const flowType = getTypeAtFlowNode(c, f, flow.antecedent);
2576
+ if ((flowType.objectFlags & 256 /* ObjectFlags.EvolvingArray */) === 0)
2577
+ return flowType;
2578
+ const incomplete = f.incomplete;
2579
+ let evolvedType = flowType;
2580
+ if (node.kind === 214 /* SyntaxKind.CallExpression */) {
2581
+ for (const arg of getArguments(node).nodes)
2582
+ evolvedType = addEvolvingArrayElementType(c, evolvedType, arg);
2583
+ }
2584
+ else {
2585
+ // The context-free type avoids rechecking the left side without caching, which blows up.
2586
+ const binary = node;
2587
+ const indexType = getContextFreeTypeOfExpression(c, binary.left.argumentExpression);
2588
+ if (isTypeAssignableToKindEx(c, indexType, 67648 /* TypeFlags.NumberLike */, false))
2589
+ evolvedType = addEvolvingArrayElementType(c, evolvedType, binary.right);
2590
+ }
2591
+ return newFlowType(c, f, evolvedType, incomplete);
2592
+ }
2593
+ // ---- Evolving arrays
2594
+ /** The evolving array type of `let x = []` with the element types added so far. */
2595
+ export function getEvolvingArrayType(c, elementType) {
2596
+ const key = cachedTypeKey(6 /* CachedTypeKind.EvolvingArray */, elementType.id);
2597
+ const cached = c.cachedTypesById.get(key);
2598
+ if (cached !== undefined)
2599
+ return cached;
2600
+ const evolving = newObjectType(c, 256 /* ObjectFlags.EvolvingArray */, undefined);
2601
+ evolving.elementType = elementType;
2602
+ c.cachedTypesById.set(key, evolving);
2603
+ return evolving;
2604
+ }
2605
+ function getElementTypeOfEvolvingArrayType(c, t) {
2606
+ return (t.objectFlags & 256 /* ObjectFlags.EvolvingArray */) !== 0 ? t.elementType : c.neverType;
2607
+ }
2608
+ function isEvolvingArrayTypeList(types) {
2609
+ let hasEvolvingArrayType = false;
2610
+ for (const t of types) {
2611
+ if ((t.flags & 262144 /* TypeFlags.Never */) !== 0)
2612
+ continue;
2613
+ if ((t.objectFlags & 256 /* ObjectFlags.EvolvingArray */) === 0)
2614
+ return false;
2615
+ hasEvolvingArrayType = true;
2616
+ }
2617
+ return hasEvolvingArrayType;
2618
+ }
2619
+ /**
2620
+ * Whether `node` is `x` in `x.length`, `x.push(v)`, `x.unshift(v)` or `x[n] = v` with a numeric,
2621
+ * undefined or any `n`.
2622
+ */
2623
+ export function isEvolvingArrayOperationTarget(c, node) {
2624
+ const root = getReferenceRoot(node);
2625
+ const parent = root.parent;
2626
+ if (parent.kind === 212 /* SyntaxKind.PropertyAccessExpression */) {
2627
+ const name = parent.name;
2628
+ return (getText(name) === 'length' ||
2629
+ (parent.parent.kind === 214 /* SyntaxKind.CallExpression */ &&
2630
+ name.kind === 79 /* SyntaxKind.Identifier */ &&
2631
+ isPushOrUnshiftIdentifier(name)));
2632
+ }
2633
+ if (parent.kind !== 213 /* SyntaxKind.ElementAccessExpression */ || getExpression(parent) !== root)
2634
+ return false;
2635
+ const grandparent = parent.parent;
2636
+ return (grandparent.kind === 227 /* SyntaxKind.BinaryExpression */ &&
2637
+ grandparent.operatorToken.kind === 63 /* SyntaxKind.EqualsToken */ &&
2638
+ grandparent.left === parent &&
2639
+ !isAssignmentTarget(grandparent) &&
2640
+ isTypeAssignableToKindEx(c, getTypeOfExpression(c, parent.argumentExpression), 67648 /* TypeFlags.NumberLike */, false));
2641
+ }
2642
+ function getReferenceRoot(node) {
2643
+ const parent = node.parent;
2644
+ if (parent.kind === 218 /* SyntaxKind.ParenthesizedExpression */)
2645
+ return getReferenceRoot(parent);
2646
+ if (parent.kind === 227 /* SyntaxKind.BinaryExpression */) {
2647
+ const binary = parent;
2648
+ if ((binary.operatorToken.kind === 63 /* SyntaxKind.EqualsToken */ && binary.left === node) ||
2649
+ (binary.operatorToken.kind === 27 /* SyntaxKind.CommaToken */ && binary.right === node))
2650
+ return getReferenceRoot(parent);
2651
+ }
2652
+ return node;
2653
+ }
2654
+ /** Adds the type of `node` to an evolving array; subtype reduction waits until it's finalized. */
2655
+ function addEvolvingArrayElementType(c, evolvingArrayType, node) {
2656
+ const newElementType = getRegularTypeOfObjectLiteral(c, getBaseTypeOfLiteralType(c, getContextFreeTypeOfExpression(c, node)));
2657
+ const elementType = evolvingArrayType.elementType;
2658
+ if (isTypeSubsetOf(c, newElementType, elementType))
2659
+ return evolvingArrayType;
2660
+ return getEvolvingArrayType(c, getUnionType(c, [elementType, newElementType]));
2661
+ }
2662
+ /** The array type an evolving array settles on; `any[]` while it has no elements. */
2663
+ export function finalizeEvolvingArrayType(c, t) {
2664
+ if ((t.objectFlags & 256 /* ObjectFlags.EvolvingArray */) === 0)
2665
+ return t;
2666
+ const evolving = t;
2667
+ const side = sideOf(c, evolving);
2668
+ if (side.finalArrayType !== undefined)
2669
+ return side.finalArrayType;
2670
+ const elementType = evolving.elementType;
2671
+ const finalArrayType = (elementType.flags & 262144 /* TypeFlags.Never */) !== 0
2672
+ ? c.autoArrayType
2673
+ : createArrayType(c, (elementType.flags & 134217728 /* TypeFlags.Union */) !== 0
2674
+ ? getUnionTypeEx(c, elementType.types, 2 /* UnionReduction.Subtype */, undefined, undefined)
2675
+ : elementType);
2676
+ side.finalArrayType = finalArrayType;
2677
+ return finalArrayType;
2678
+ }
2679
+ /** The type of an expression checked without contextual types, computed once. */
2680
+ function getContextFreeTypeOfExpression(c, node) {
2681
+ const links = c.nodeLinks(node);
2682
+ if (links.contextFreeType !== undefined)
2683
+ return links.contextFreeType;
2684
+ const t = checkExpressionEx(c, node, 4 /* CheckMode.SkipContextSensitive */);
2685
+ links.contextFreeType = t;
2686
+ return t;
2687
+ }
2688
+ function reportFlowControlError(c, node) {
2689
+ let block = node;
2690
+ while (block !== undefined && !isFunctionOrModuleBlock(block))
2691
+ block = block.parent;
2692
+ if (block === undefined)
2693
+ return;
2694
+ const file = getSourceFileOfNode(node);
2695
+ const span = getRangeOfTokenAtPosition(file, getStatements(block).pos);
2696
+ c.addDiagnostic(new Diagnostic(file.diagnosticFile(), span.pos, span.end, 717 /* Diagnostics.The_containing_function_or_module_body_is_too_large_for_control_flow_analysis */, []));
2697
+ }
2698
+ function isFunctionOrModuleBlock(node) {
2699
+ return (node.kind === 307 /* SyntaxKind.SourceFile */ ||
2700
+ node.kind === 269 /* SyntaxKind.ModuleBlock */ ||
2701
+ (node.kind === 242 /* SyntaxKind.Block */ && node.parent !== undefined && isFunctionLike(node.parent)));
2702
+ }
2703
+ /**
2704
+ * The type of `symbol` at `location`. A destructured variable of a const declaration or unassigned
2705
+ * parameter is narrowed by checks of its siblings: in `const { kind, payload } = action`, testing
2706
+ * `kind` narrows `payload`.
2707
+ */
2708
+ export function getNarrowedTypeOfSymbol(c, symbol, location) {
2709
+ const t = getTypeOfSymbol(c, symbol);
2710
+ const declaration = symbol.valueDeclaration;
2711
+ if (declaration !== undefined &&
2712
+ declaration.kind === 170 /* SyntaxKind.Parameter */ &&
2713
+ getType(declaration) === undefined &&
2714
+ getInitializer(declaration) === undefined &&
2715
+ getDotDotDotToken(declaration) === undefined)
2716
+ return getNarrowedTypeOfDependentParameter(c, t, declaration, location);
2717
+ if (declaration === undefined ||
2718
+ declaration.kind !== 209 /* SyntaxKind.BindingElement */ ||
2719
+ getInitializer(declaration) !== undefined ||
2720
+ declaration.dotDotDotToken !== undefined ||
2721
+ getElements(declaration.parent).nodes.length < 2)
2722
+ return t;
2723
+ const pattern = declaration.parent;
2724
+ const parent = pattern.parent;
2725
+ const rootDeclaration = getRootDeclaration(parent);
2726
+ const isParameter = rootDeclaration.kind === 170 /* SyntaxKind.Parameter */;
2727
+ if (!isParameter &&
2728
+ !(rootDeclaration.kind === 261 /* SyntaxKind.VariableDeclaration */ &&
2729
+ (getCombinedNodeFlags(rootDeclaration) & 6 /* NodeFlags.Constant */) !== 0))
2730
+ return t;
2731
+ const links = c.nodeLinks(parent);
2732
+ if ((links.flags & 4194304 /* NodeCheckFlags.InCheckIdentifier */) !== 0)
2733
+ return t;
2734
+ links.flags |= 4194304 /* NodeCheckFlags.InCheckIdentifier */;
2735
+ let result = t;
2736
+ const parentType = getTypeForBindingElementParent(c, parent, 0 /* CheckMode.Normal */);
2737
+ const parentTypeConstraint = parentType !== undefined ? mapToBaseConstraintOrType(c, parentType) : undefined;
2738
+ if (parentTypeConstraint !== undefined &&
2739
+ (parentTypeConstraint.flags & 134217728 /* TypeFlags.Union */) !== 0 &&
2740
+ !(isParameter && isSomeSymbolAssigned(c, getName(rootDeclaration)))) {
2741
+ const narrowedType = getFlowTypeOfReferenceAt(c, pattern, parentTypeConstraint, parentTypeConstraint, undefined, getFlowNode(location));
2742
+ // Tuple bounds aren't checked, as the narrowed parent may have fewer elements than the declared one.
2743
+ result =
2744
+ (narrowedType.flags & 262144 /* TypeFlags.Never */) !== 0
2745
+ ? c.neverType
2746
+ : getBindingElementTypeFromParentType(c, declaration, narrowedType, true);
2747
+ }
2748
+ links.flags &= ~4194304 /* NodeCheckFlags.InCheckIdentifier */;
2749
+ return result;
2750
+ }
2751
+ /**
2752
+ * A parameter of a function contextually typed by a rest union of tuples, as in
2753
+ * `const f: (...args: [1, string] | [2, number]) => void = (kind, data) => ...`, narrowed by checks of its
2754
+ * sibling parameters: the function is the pseudo-reference for the tuple.
2755
+ */
2756
+ function getNarrowedTypeOfDependentParameter(c, t, declaration, location) {
2757
+ const fn = declaration.parent;
2758
+ const parameters = getParameters(fn).nodes;
2759
+ if (parameters.length < 2 || !isContextSensitiveFunctionOrObjectLiteralMethod(c, fn))
2760
+ return t;
2761
+ const contextualSignature = getContextualSignature(c, fn);
2762
+ if (contextualSignature === undefined ||
2763
+ contextualSignature.parameters.length !== 1 ||
2764
+ !contextualSignature.hasRestParameter())
2765
+ return t;
2766
+ const context = getInferenceContext(c, fn);
2767
+ const restType = getReducedApparentType(c, instantiateType(c, getTypeOfSymbol(c, contextualSignature.parameters[0]), context !== undefined ? context.nonFixingMapper : undefined));
2768
+ if ((restType.flags & 134217728 /* TypeFlags.Union */) === 0 ||
2769
+ !everyType(restType, isTupleType) ||
2770
+ someParameterAssigned(c, parameters))
2771
+ return t;
2772
+ const narrowedType = getFlowTypeOfReferenceAt(c, fn, restType, restType, undefined, getFlowNode(location));
2773
+ const index = parameters.indexOf(declaration) - (isThisParameter(parameters[0]) ? 1 : 0);
2774
+ return getIndexedAccessType(c, narrowedType, getNumberLiteralType(c, index));
2775
+ }
2776
+ function isThisParameter(parameter) {
2777
+ const name = getName(parameter);
2778
+ return name !== undefined && name.kind === 79 /* SyntaxKind.Identifier */ && getText(name) === 'this';
2779
+ }
2780
+ function someParameterAssigned(c, parameters) {
2781
+ for (const p of parameters)
2782
+ if (isSomeSymbolAssigned(c, getName(p)))
2783
+ return true;
2784
+ return false;
2785
+ }
2786
+ function isSomeSymbolAssigned(c, name) {
2787
+ if (name.kind === 79 /* SyntaxKind.Identifier */) {
2788
+ const symbol = getSymbolOfDeclaration(c, name.parent);
2789
+ return symbol !== undefined && isSymbolAssigned(c, symbol);
2790
+ }
2791
+ for (const e of getElements(name).nodes) {
2792
+ const elementName = getName(e);
2793
+ if (elementName !== undefined && isSomeSymbolAssigned(c, elementName))
2794
+ return true;
2795
+ }
2796
+ return false;
2797
+ }