tsc-ts 0.0.4 → 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.
- package/README.md +33 -1
- package/dist/api/compiler/ast-utilities.d.ts +82 -0
- package/dist/api/compiler/ast-utilities.js +462 -0
- package/dist/api/compiler/ast.d.ts +2350 -0
- package/dist/api/compiler/ast.js +11206 -0
- package/dist/api/compiler/astnav.d.ts +37 -0
- package/dist/api/compiler/astnav.js +767 -0
- package/dist/api/compiler/binder-utilities.d.ts +182 -0
- package/dist/api/compiler/binder-utilities.js +1240 -0
- package/dist/api/compiler/binder.d.ts +33 -0
- package/dist/api/compiler/binder.js +2358 -0
- package/dist/api/compiler/character-codes.d.ts +161 -0
- package/dist/api/compiler/character-codes.js +160 -0
- package/dist/api/compiler/checker-calls.d.ts +46 -0
- package/dist/api/compiler/checker-calls.js +1640 -0
- package/dist/api/compiler/checker-classes.d.ts +30 -0
- package/dist/api/compiler/checker-classes.js +582 -0
- package/dist/api/compiler/checker-contextual.d.ts +77 -0
- package/dist/api/compiler/checker-contextual.js +1136 -0
- package/dist/api/compiler/checker-declaration-order.d.ts +12 -0
- package/dist/api/compiler/checker-declaration-order.js +356 -0
- package/dist/api/compiler/checker-declarations.d.ts +33 -0
- package/dist/api/compiler/checker-declarations.js +888 -0
- package/dist/api/compiler/checker-declared-types.d.ts +48 -0
- package/dist/api/compiler/checker-declared-types.js +787 -0
- package/dist/api/compiler/checker-decorators.d.ts +26 -0
- package/dist/api/compiler/checker-decorators.js +562 -0
- package/dist/api/compiler/checker-deprecation.d.ts +33 -0
- package/dist/api/compiler/checker-deprecation.js +315 -0
- package/dist/api/compiler/checker-destructuring.d.ts +34 -0
- package/dist/api/compiler/checker-destructuring.js +584 -0
- package/dist/api/compiler/checker-elaboration.d.ts +15 -0
- package/dist/api/compiler/checker-elaboration.js +290 -0
- package/dist/api/compiler/checker-emit-helpers.d.ts +58 -0
- package/dist/api/compiler/checker-emit-helpers.js +154 -0
- package/dist/api/compiler/checker-emit-resolver.d.ts +75 -0
- package/dist/api/compiler/checker-emit-resolver.js +579 -0
- package/dist/api/compiler/checker-evaluator.d.ts +27 -0
- package/dist/api/compiler/checker-evaluator.js +188 -0
- package/dist/api/compiler/checker-expressions.d.ts +67 -0
- package/dist/api/compiler/checker-expressions.js +1351 -0
- package/dist/api/compiler/checker-features.d.ts +6 -0
- package/dist/api/compiler/checker-features.js +354 -0
- package/dist/api/compiler/checker-flow.d.ts +86 -0
- package/dist/api/compiler/checker-flow.js +169 -0
- package/dist/api/compiler/checker-functions.d.ts +43 -0
- package/dist/api/compiler/checker-functions.js +1002 -0
- package/dist/api/compiler/checker-generated-code.d.ts +25 -0
- package/dist/api/compiler/checker-generated-code.js +168 -0
- package/dist/api/compiler/checker-globals.d.ts +14 -0
- package/dist/api/compiler/checker-globals.js +37 -0
- package/dist/api/compiler/checker-grammar.d.ts +35 -0
- package/dist/api/compiler/checker-grammar.js +2573 -0
- package/dist/api/compiler/checker-heritage.d.ts +28 -0
- package/dist/api/compiler/checker-heritage.js +900 -0
- package/dist/api/compiler/checker-inference.d.ts +179 -0
- package/dist/api/compiler/checker-inference.js +2105 -0
- package/dist/api/compiler/checker-iteration.d.ts +115 -0
- package/dist/api/compiler/checker-iteration.js +857 -0
- package/dist/api/compiler/checker-javascript.d.ts +42 -0
- package/dist/api/compiler/checker-javascript.js +441 -0
- package/dist/api/compiler/checker-jsdoc.d.ts +30 -0
- package/dist/api/compiler/checker-jsdoc.js +320 -0
- package/dist/api/compiler/checker-jsx.d.ts +94 -0
- package/dist/api/compiler/checker-jsx.js +1433 -0
- package/dist/api/compiler/checker-literals.d.ts +6 -0
- package/dist/api/compiler/checker-literals.js +43 -0
- package/dist/api/compiler/checker-members.d.ts +198 -0
- package/dist/api/compiler/checker-members.js +2126 -0
- package/dist/api/compiler/checker-modules.d.ts +85 -0
- package/dist/api/compiler/checker-modules.js +2141 -0
- package/dist/api/compiler/checker-narrowing.d.ts +120 -0
- package/dist/api/compiler/checker-narrowing.js +2797 -0
- package/dist/api/compiler/checker-object-literals.d.ts +14 -0
- package/dist/api/compiler/checker-object-literals.js +479 -0
- package/dist/api/compiler/checker-operators.d.ts +33 -0
- package/dist/api/compiler/checker-operators.js +1040 -0
- package/dist/api/compiler/checker-overloads.d.ts +7 -0
- package/dist/api/compiler/checker-overloads.js +270 -0
- package/dist/api/compiler/checker-property-access.d.ts +34 -0
- package/dist/api/compiler/checker-property-access.js +672 -0
- package/dist/api/compiler/checker-services.d.ts +49 -0
- package/dist/api/compiler/checker-services.js +995 -0
- package/dist/api/compiler/checker-signatures.d.ts +55 -0
- package/dist/api/compiler/checker-signatures.js +788 -0
- package/dist/api/compiler/checker-source-file.d.ts +27 -0
- package/dist/api/compiler/checker-source-file.js +619 -0
- package/dist/api/compiler/checker-statements.d.ts +18 -0
- package/dist/api/compiler/checker-statements.js +226 -0
- package/dist/api/compiler/checker-super.d.ts +13 -0
- package/dist/api/compiler/checker-super.js +241 -0
- package/dist/api/compiler/checker-symbol-types.d.ts +67 -0
- package/dist/api/compiler/checker-symbol-types.js +1885 -0
- package/dist/api/compiler/checker-symbols.d.ts +181 -0
- package/dist/api/compiler/checker-symbols.js +1782 -0
- package/dist/api/compiler/checker-type-factory.d.ts +142 -0
- package/dist/api/compiler/checker-type-factory.js +2501 -0
- package/dist/api/compiler/checker-type-nodes.d.ts +26 -0
- package/dist/api/compiler/checker-type-nodes.js +828 -0
- package/dist/api/compiler/checker-type-operators.d.ts +109 -0
- package/dist/api/compiler/checker-type-operators.js +1842 -0
- package/dist/api/compiler/checker-types.d.ts +1027 -0
- package/dist/api/compiler/checker-types.js +1697 -0
- package/dist/api/compiler/checker-unused.d.ts +19 -0
- package/dist/api/compiler/checker-unused.js +524 -0
- package/dist/api/compiler/checker-utilities.d.ts +33 -0
- package/dist/api/compiler/checker-utilities.js +141 -0
- package/dist/api/compiler/checker.d.ts +491 -0
- package/dist/api/compiler/checker.js +912 -0
- package/dist/api/compiler/command-line.d.ts +32 -0
- package/dist/api/compiler/command-line.js +383 -0
- package/dist/api/compiler/compile-program.d.ts +118 -0
- package/dist/api/compiler/compile-program.js +384 -0
- package/dist/api/compiler/diagnostic-locales.d.ts +22 -0
- package/dist/api/compiler/diagnostic-locales.js +64 -0
- package/dist/api/compiler/diagnostic-messages.d.ts +2166 -0
- package/dist/api/compiler/diagnostic-messages.js +184 -0
- package/dist/api/compiler/diagnostics.d.ts +167 -0
- package/dist/api/compiler/diagnostics.js +820 -0
- package/dist/api/compiler/emitter.d.ts +71 -0
- package/dist/api/compiler/emitter.js +327 -0
- package/dist/api/compiler/file-include.d.ts +117 -0
- package/dist/api/compiler/file-include.js +536 -0
- package/dist/api/compiler/file-loader.d.ts +261 -0
- package/dist/api/compiler/file-loader.js +1257 -0
- package/dist/api/compiler/file-system.d.ts +51 -0
- package/dist/api/compiler/file-system.js +132 -0
- package/dist/api/compiler/flow.d.ts +60 -0
- package/dist/api/compiler/flow.js +45 -0
- package/dist/api/compiler/instantiation.d.ts +74 -0
- package/dist/api/compiler/instantiation.js +799 -0
- package/dist/api/compiler/jsdoc.d.ts +75 -0
- package/dist/api/compiler/jsdoc.js +1229 -0
- package/dist/api/compiler/link-tables.d.ts +36 -0
- package/dist/api/compiler/link-tables.js +83 -0
- package/dist/api/compiler/locale-data.d.ts +24 -0
- package/dist/api/compiler/locale-data.js +183 -0
- package/dist/api/compiler/locale.d.ts +4 -0
- package/dist/api/compiler/locale.js +317 -0
- package/dist/api/compiler/locales/cs-CZ.d.ts +2 -0
- package/dist/api/compiler/locales/cs-CZ.js +3 -0
- package/dist/api/compiler/locales/de-DE.d.ts +2 -0
- package/dist/api/compiler/locales/de-DE.js +3 -0
- package/dist/api/compiler/locales/es-ES.d.ts +2 -0
- package/dist/api/compiler/locales/es-ES.js +3 -0
- package/dist/api/compiler/locales/fr-FR.d.ts +2 -0
- package/dist/api/compiler/locales/fr-FR.js +3 -0
- package/dist/api/compiler/locales/it-IT.d.ts +2 -0
- package/dist/api/compiler/locales/it-IT.js +3 -0
- package/dist/api/compiler/locales/ja-JP.d.ts +2 -0
- package/dist/api/compiler/locales/ja-JP.js +3 -0
- package/dist/api/compiler/locales/ko-KR.d.ts +2 -0
- package/dist/api/compiler/locales/ko-KR.js +3 -0
- package/dist/api/compiler/locales/pl-PL.d.ts +2 -0
- package/dist/api/compiler/locales/pl-PL.js +3 -0
- package/dist/api/compiler/locales/pt-BR.d.ts +2 -0
- package/dist/api/compiler/locales/pt-BR.js +3 -0
- package/dist/api/compiler/locales/ru-RU.d.ts +2 -0
- package/dist/api/compiler/locales/ru-RU.js +3 -0
- package/dist/api/compiler/locales/tr-TR.d.ts +2 -0
- package/dist/api/compiler/locales/tr-TR.js +3 -0
- package/dist/api/compiler/locales/zh-CN.d.ts +2 -0
- package/dist/api/compiler/locales/zh-CN.js +3 -0
- package/dist/api/compiler/locales/zh-TW.d.ts +2 -0
- package/dist/api/compiler/locales/zh-TW.js +3 -0
- package/dist/api/compiler/module-resolution-types.d.ts +83 -0
- package/dist/api/compiler/module-resolution-types.js +176 -0
- package/dist/api/compiler/module-resolution.d.ts +71 -0
- package/dist/api/compiler/module-resolution.js +1598 -0
- package/dist/api/compiler/module-specifiers.d.ts +64 -0
- package/dist/api/compiler/module-specifiers.js +1101 -0
- package/dist/api/compiler/name-resolver.d.ts +43 -0
- package/dist/api/compiler/name-resolver.js +509 -0
- package/dist/api/compiler/node-builder-names.d.ts +32 -0
- package/dist/api/compiler/node-builder-names.js +854 -0
- package/dist/api/compiler/node-builder-pseudo.d.ts +36 -0
- package/dist/api/compiler/node-builder-pseudo.js +744 -0
- package/dist/api/compiler/node-builder-reuse.d.ts +4 -0
- package/dist/api/compiler/node-builder-reuse.js +647 -0
- package/dist/api/compiler/node-builder.d.ts +270 -0
- package/dist/api/compiler/node-builder.js +2645 -0
- package/dist/api/compiler/option-declarations.d.ts +90 -0
- package/dist/api/compiler/option-declarations.js +1460 -0
- package/dist/api/compiler/options.d.ts +289 -0
- package/dist/api/compiler/options.js +1197 -0
- package/dist/api/compiler/output-paths.d.ts +42 -0
- package/dist/api/compiler/output-paths.js +195 -0
- package/dist/api/compiler/package-json.d.ts +175 -0
- package/dist/api/compiler/package-json.js +1015 -0
- package/dist/api/compiler/parallel-parse.d.ts +219 -0
- package/dist/api/compiler/parallel-parse.js +537 -0
- package/dist/api/compiler/parser.d.ts +587 -0
- package/dist/api/compiler/parser.js +6407 -0
- package/dist/api/compiler/path.d.ts +91 -0
- package/dist/api/compiler/path.js +705 -0
- package/dist/api/compiler/printer/async-helpers.d.ts +22 -0
- package/dist/api/compiler/printer/async-helpers.js +91 -0
- package/dist/api/compiler/printer/auto-generate.d.ts +13 -0
- package/dist/api/compiler/printer/auto-generate.js +20 -0
- package/dist/api/compiler/printer/decorator-helpers.d.ts +24 -0
- package/dist/api/compiler/printer/decorator-helpers.js +118 -0
- package/dist/api/compiler/printer/emit-context.d.ts +154 -0
- package/dist/api/compiler/printer/emit-context.js +576 -0
- package/dist/api/compiler/printer/emit-flags.d.ts +60 -0
- package/dist/api/compiler/printer/emit-flags.js +2 -0
- package/dist/api/compiler/printer/factory-base.d.ts +171 -0
- package/dist/api/compiler/printer/factory-base.js +1505 -0
- package/dist/api/compiler/printer/factory.d.ts +147 -0
- package/dist/api/compiler/printer/factory.js +542 -0
- package/dist/api/compiler/printer/helpers.d.ts +27 -0
- package/dist/api/compiler/printer/helpers.js +164 -0
- package/dist/api/compiler/printer/name-generator.d.ts +41 -0
- package/dist/api/compiler/printer/name-generator.js +283 -0
- package/dist/api/compiler/printer/printer.d.ts +411 -0
- package/dist/api/compiler/printer/printer.js +4517 -0
- package/dist/api/compiler/printer/text-writer.d.ts +46 -0
- package/dist/api/compiler/printer/text-writer.js +172 -0
- package/dist/api/compiler/printer/utilities.d.ts +82 -0
- package/dist/api/compiler/printer/utilities.js +817 -0
- package/dist/api/compiler/program-checker.d.ts +156 -0
- package/dist/api/compiler/program-checker.js +526 -0
- package/dist/api/compiler/program-utilities.d.ts +52 -0
- package/dist/api/compiler/program-utilities.js +365 -0
- package/dist/api/compiler/program.d.ts +180 -0
- package/dist/api/compiler/program.js +1011 -0
- package/dist/api/compiler/pseudo-bigint.d.ts +5 -0
- package/dist/api/compiler/pseudo-bigint.js +61 -0
- package/dist/api/compiler/regexp-checker.d.ts +38 -0
- package/dist/api/compiler/regexp-checker.js +942 -0
- package/dist/api/compiler/relations.d.ts +263 -0
- package/dist/api/compiler/relations.js +4002 -0
- package/dist/api/compiler/reparser.d.ts +48 -0
- package/dist/api/compiler/reparser.js +1323 -0
- package/dist/api/compiler/scanner.d.ts +256 -0
- package/dist/api/compiler/scanner.js +2853 -0
- package/dist/api/compiler/side-state.d.ts +16 -0
- package/dist/api/compiler/side-state.js +89 -0
- package/dist/api/compiler/sourcemap.d.ts +55 -0
- package/dist/api/compiler/sourcemap.js +340 -0
- package/dist/api/compiler/spelling.d.ts +14 -0
- package/dist/api/compiler/spelling.js +128 -0
- package/dist/api/compiler/symbol-accessibility.d.ts +57 -0
- package/dist/api/compiler/symbol-accessibility.js +894 -0
- package/dist/api/compiler/symbol.d.ts +240 -0
- package/dist/api/compiler/symbol.js +228 -0
- package/dist/api/compiler/symlinks.d.ts +32 -0
- package/dist/api/compiler/symlinks.js +114 -0
- package/dist/api/compiler/syntax-kind.d.ts +389 -0
- package/dist/api/compiler/syntax-kind.js +2 -0
- package/dist/api/compiler/timeline.d.ts +9 -0
- package/dist/api/compiler/timeline.js +54 -0
- package/dist/api/compiler/transformers/alias-references.d.ts +39 -0
- package/dist/api/compiler/transformers/alias-references.js +676 -0
- package/dist/api/compiler/transformers/async-utilities.d.ts +51 -0
- package/dist/api/compiler/transformers/async-utilities.js +260 -0
- package/dist/api/compiler/transformers/async.d.ts +53 -0
- package/dist/api/compiler/transformers/async.js +645 -0
- package/dist/api/compiler/transformers/class-fields.d.ts +206 -0
- package/dist/api/compiler/transformers/class-fields.js +2418 -0
- package/dist/api/compiler/transformers/class-utilities.d.ts +46 -0
- package/dist/api/compiler/transformers/class-utilities.js +261 -0
- package/dist/api/compiler/transformers/commonjs-module.d.ts +103 -0
- package/dist/api/compiler/transformers/commonjs-module.js +1180 -0
- package/dist/api/compiler/transformers/const-enum-inliner.d.ts +11 -0
- package/dist/api/compiler/transformers/const-enum-inliner.js +36 -0
- package/dist/api/compiler/transformers/declarations-diagnostics.d.ts +52 -0
- package/dist/api/compiler/transformers/declarations-diagnostics.js +538 -0
- package/dist/api/compiler/transformers/declarations-pseudo.d.ts +160 -0
- package/dist/api/compiler/transformers/declarations-pseudo.js +797 -0
- package/dist/api/compiler/transformers/declarations-resolver.d.ts +152 -0
- package/dist/api/compiler/transformers/declarations-resolver.js +35 -0
- package/dist/api/compiler/transformers/declarations-syntactic.d.ts +119 -0
- package/dist/api/compiler/transformers/declarations-syntactic.js +1582 -0
- package/dist/api/compiler/transformers/declarations-tracker.d.ts +46 -0
- package/dist/api/compiler/transformers/declarations-tracker.js +197 -0
- package/dist/api/compiler/transformers/declarations.d.ts +175 -0
- package/dist/api/compiler/transformers/declarations.js +2380 -0
- package/dist/api/compiler/transformers/decorator-metadata.d.ts +54 -0
- package/dist/api/compiler/transformers/decorator-metadata.js +554 -0
- package/dist/api/compiler/transformers/decorator-utilities.d.ts +38 -0
- package/dist/api/compiler/transformers/decorator-utilities.js +238 -0
- package/dist/api/compiler/transformers/destructuring.d.ts +25 -0
- package/dist/api/compiler/transformers/destructuring.js +626 -0
- package/dist/api/compiler/transformers/downlevel.d.ts +6 -0
- package/dist/api/compiler/transformers/downlevel.js +45 -0
- package/dist/api/compiler/transformers/emit-resolver.d.ts +61 -0
- package/dist/api/compiler/transformers/emit-resolver.js +197 -0
- package/dist/api/compiler/transformers/es-decorators.d.ts +118 -0
- package/dist/api/compiler/transformers/es-decorators.js +1901 -0
- package/dist/api/compiler/transformers/exponentiation.d.ts +11 -0
- package/dist/api/compiler/transformers/exponentiation.js +76 -0
- package/dist/api/compiler/transformers/external-module-info.d.ts +35 -0
- package/dist/api/compiler/transformers/external-module-info.js +351 -0
- package/dist/api/compiler/transformers/for-await.d.ts +43 -0
- package/dist/api/compiler/transformers/for-await.js +469 -0
- package/dist/api/compiler/transformers/import-elision.d.ts +16 -0
- package/dist/api/compiler/transformers/import-elision.js +124 -0
- package/dist/api/compiler/transformers/jsx-entities.d.ts +1 -0
- package/dist/api/compiler/transformers/jsx-entities.js +256 -0
- package/dist/api/compiler/transformers/jsx.d.ts +71 -0
- package/dist/api/compiler/transformers/jsx.js +700 -0
- package/dist/api/compiler/transformers/legacy-decorators.d.ts +56 -0
- package/dist/api/compiler/transformers/legacy-decorators.js +564 -0
- package/dist/api/compiler/transformers/logical-assignment.d.ts +14 -0
- package/dist/api/compiler/transformers/logical-assignment.js +62 -0
- package/dist/api/compiler/transformers/module-utilities.d.ts +9 -0
- package/dist/api/compiler/transformers/module-utilities.js +36 -0
- package/dist/api/compiler/transformers/module.d.ts +49 -0
- package/dist/api/compiler/transformers/module.js +296 -0
- package/dist/api/compiler/transformers/named-evaluation.d.ts +26 -0
- package/dist/api/compiler/transformers/named-evaluation.js +266 -0
- package/dist/api/compiler/transformers/nullish-coalescing.d.ts +9 -0
- package/dist/api/compiler/transformers/nullish-coalescing.js +26 -0
- package/dist/api/compiler/transformers/object-rest-spread.d.ts +34 -0
- package/dist/api/compiler/transformers/object-rest-spread.js +411 -0
- package/dist/api/compiler/transformers/optional-catch.d.ts +9 -0
- package/dist/api/compiler/transformers/optional-catch.js +20 -0
- package/dist/api/compiler/transformers/optional-chain.d.ts +14 -0
- package/dist/api/compiler/transformers/optional-chain.js +188 -0
- package/dist/api/compiler/transformers/reference-resolver.d.ts +128 -0
- package/dist/api/compiler/transformers/reference-resolver.js +1155 -0
- package/dist/api/compiler/transformers/runtime-syntax.d.ts +71 -0
- package/dist/api/compiler/transformers/runtime-syntax.js +753 -0
- package/dist/api/compiler/transformers/subtree-facts.d.ts +9 -0
- package/dist/api/compiler/transformers/subtree-facts.js +203 -0
- package/dist/api/compiler/transformers/syntax-facts.d.ts +51 -0
- package/dist/api/compiler/transformers/syntax-facts.js +254 -0
- package/dist/api/compiler/transformers/tagged-template.d.ts +17 -0
- package/dist/api/compiler/transformers/tagged-template.js +148 -0
- package/dist/api/compiler/transformers/transformer.d.ts +17 -0
- package/dist/api/compiler/transformers/transformer.js +54 -0
- package/dist/api/compiler/transformers/type-eraser.d.ts +21 -0
- package/dist/api/compiler/transformers/type-eraser.js +346 -0
- package/dist/api/compiler/transformers/type-serialization.d.ts +20 -0
- package/dist/api/compiler/transformers/type-serialization.js +347 -0
- package/dist/api/compiler/transformers/using.d.ts +56 -0
- package/dist/api/compiler/transformers/using.js +453 -0
- package/dist/api/compiler/transformers/utilities.d.ts +50 -0
- package/dist/api/compiler/transformers/utilities.js +347 -0
- package/dist/api/compiler/transformers/visitor-base.d.ts +6 -0
- package/dist/api/compiler/transformers/visitor-base.js +817 -0
- package/dist/api/compiler/transformers/visitor-hooks.d.ts +29 -0
- package/dist/api/compiler/transformers/visitor-hooks.js +189 -0
- package/dist/api/compiler/tsconfig.d.ts +177 -0
- package/dist/api/compiler/tsconfig.js +1311 -0
- package/dist/api/compiler/type-printer.d.ts +15 -0
- package/dist/api/compiler/type-printer.js +74 -0
- package/dist/api/compiler/types.d.ts +87 -0
- package/dist/api/compiler/types.js +23 -0
- package/dist/api/compiler/unicode-identifiers.d.ts +2 -0
- package/dist/api/compiler/unicode-identifiers.js +209 -0
- package/dist/api/compiler/unicode-properties.d.ts +8 -0
- package/dist/api/compiler/unicode-properties.js +531 -0
- package/dist/api/compiler/vfs-match.d.ts +24 -0
- package/dist/api/compiler/vfs-match.js +587 -0
- package/dist/api/library/ast.d.ts +3 -2
- package/dist/api/library/ast.generated.d.ts +127 -139
- package/dist/api/library/checker.d.ts +51 -0
- package/dist/api/library/checker.js +318 -0
- package/dist/api/library/diagnostics.d.ts +13 -0
- package/dist/api/library/diagnostics.js +50 -0
- package/dist/api/library/disk.d.ts +13 -0
- package/dist/api/library/disk.js +66 -0
- package/dist/api/library/errors.d.ts +13 -0
- package/dist/api/library/errors.js +24 -0
- package/dist/api/library/host.d.ts +24 -0
- package/dist/api/library/host.js +52 -0
- package/dist/api/library/index.d.ts +5 -1
- package/dist/api/library/index.js +4 -0
- package/dist/api/library/nodes.d.ts +51 -0
- package/dist/api/library/nodes.js +196 -0
- package/dist/api/library/options.d.ts +144 -0
- package/dist/api/library/options.js +308 -0
- package/dist/api/library/parse.d.ts +19 -0
- package/dist/api/library/parse.js +41 -0
- package/dist/api/library/program.d.ts +21 -0
- package/dist/api/library/program.js +234 -0
- package/dist/api/library/types.d.ts +14 -1
- package/package.json +12 -8
- package/src/compiler/ast-utilities.ts +548 -0
- package/src/compiler/ast.ts +12862 -0
- package/src/compiler/astnav.ts +799 -0
- package/src/compiler/binder-utilities.ts +1392 -0
- package/src/compiler/binder.ts +2703 -0
- package/src/compiler/build-info.ts +1197 -0
- package/src/compiler/character-codes.ts +323 -0
- package/src/compiler/checker-calls.ts +2155 -0
- package/src/compiler/checker-classes.ts +706 -0
- package/src/compiler/checker-contextual.ts +1457 -0
- package/src/compiler/checker-declaration-order.ts +438 -0
- package/src/compiler/checker-declarations.ts +1194 -0
- package/src/compiler/checker-declared-types.ts +919 -0
- package/src/compiler/checker-decorators.ts +694 -0
- package/src/compiler/checker-deprecation.ts +359 -0
- package/src/compiler/checker-destructuring.ts +805 -0
- package/src/compiler/checker-elaboration.ts +477 -0
- package/src/compiler/checker-emit-helpers.ts +224 -0
- package/src/compiler/checker-emit-resolver.ts +769 -0
- package/src/compiler/checker-evaluator.ts +243 -0
- package/src/compiler/checker-expressions.ts +1831 -0
- package/src/compiler/checker-features.ts +361 -0
- package/src/compiler/checker-flow.ts +394 -0
- package/src/compiler/checker-functions.ts +1296 -0
- package/src/compiler/checker-generated-code.ts +227 -0
- package/src/compiler/checker-globals.ts +52 -0
- package/src/compiler/checker-grammar.ts +3584 -0
- package/src/compiler/checker-heritage.ts +1233 -0
- package/src/compiler/checker-inference.ts +2576 -0
- package/src/compiler/checker-iteration.ts +1206 -0
- package/src/compiler/checker-javascript.ts +502 -0
- package/src/compiler/checker-jsdoc.ts +360 -0
- package/src/compiler/checker-jsx.ts +1924 -0
- package/src/compiler/checker-literals.ts +61 -0
- package/src/compiler/checker-members.ts +2436 -0
- package/src/compiler/checker-modules.ts +2886 -0
- package/src/compiler/checker-narrowing.ts +3230 -0
- package/src/compiler/checker-object-literals.ts +575 -0
- package/src/compiler/checker-operators.ts +1422 -0
- package/src/compiler/checker-overloads.ts +328 -0
- package/src/compiler/checker-property-access.ts +906 -0
- package/src/compiler/checker-services.ts +1149 -0
- package/src/compiler/checker-signatures.ts +1032 -0
- package/src/compiler/checker-source-file.ts +732 -0
- package/src/compiler/checker-statements.ts +292 -0
- package/src/compiler/checker-super.ts +261 -0
- package/src/compiler/checker-symbol-types.ts +2107 -0
- package/src/compiler/checker-symbols.ts +2150 -0
- package/src/compiler/checker-type-factory.ts +2579 -0
- package/src/compiler/checker-type-nodes.ts +1033 -0
- package/src/compiler/checker-type-operators.ts +2502 -0
- package/src/compiler/checker-types.ts +2340 -0
- package/src/compiler/checker-unused.ts +642 -0
- package/src/compiler/checker-utilities.ts +161 -0
- package/src/compiler/checker.ts +1116 -0
- package/src/compiler/command-line.ts +436 -0
- package/src/compiler/compile-program.ts +439 -0
- package/src/compiler/decoder.ts +1209 -0
- package/src/compiler/diagnostic-locales.ts +86 -0
- package/src/compiler/diagnostic-messages.ts +2349 -0
- package/src/compiler/diagnostics.ts +897 -0
- package/src/compiler/emitter.ts +434 -0
- package/src/compiler/encoder.ts +1945 -0
- package/src/compiler/file-include.ts +673 -0
- package/src/compiler/file-loader.ts +1528 -0
- package/src/compiler/file-system.ts +143 -0
- package/src/compiler/flow.ts +89 -0
- package/src/compiler/help.ts +473 -0
- package/src/compiler/incremental.ts +2784 -0
- package/src/compiler/init.ts +141 -0
- package/src/compiler/instantiation.ts +894 -0
- package/src/compiler/jsdoc.ts +1386 -0
- package/src/compiler/link-tables.ts +98 -0
- package/src/compiler/locale-data.ts +203 -0
- package/src/compiler/locale.ts +307 -0
- package/src/compiler/locales/cs-CZ.ts +5 -0
- package/src/compiler/locales/de-DE.ts +5 -0
- package/src/compiler/locales/es-ES.ts +5 -0
- package/src/compiler/locales/fr-FR.ts +5 -0
- package/src/compiler/locales/it-IT.ts +5 -0
- package/src/compiler/locales/ja-JP.ts +5 -0
- package/src/compiler/locales/ko-KR.ts +5 -0
- package/src/compiler/locales/pl-PL.ts +5 -0
- package/src/compiler/locales/pt-BR.ts +5 -0
- package/src/compiler/locales/ru-RU.ts +5 -0
- package/src/compiler/locales/tr-TR.ts +5 -0
- package/src/compiler/locales/zh-CN.ts +5 -0
- package/src/compiler/locales/zh-TW.ts +5 -0
- package/src/compiler/module-resolution-types.ts +243 -0
- package/src/compiler/module-resolution.ts +2072 -0
- package/src/compiler/module-specifiers.ts +1491 -0
- package/src/compiler/name-resolver.ts +646 -0
- package/src/compiler/node-builder-names.ts +1069 -0
- package/src/compiler/node-builder-pseudo.ts +892 -0
- package/src/compiler/node-builder-reuse.ts +764 -0
- package/src/compiler/node-builder.ts +3425 -0
- package/src/compiler/option-declarations.ts +1560 -0
- package/src/compiler/options.ts +1576 -0
- package/src/compiler/output-paths.ts +334 -0
- package/src/compiler/package-json.ts +1083 -0
- package/src/compiler/parallel-check.ts +1951 -0
- package/src/compiler/parallel-parse.ts +590 -0
- package/src/compiler/parse-thread.ts +140 -0
- package/src/compiler/parser.ts +7716 -0
- package/src/compiler/path.ts +717 -0
- package/src/compiler/printer/async-helpers.ts +183 -0
- package/src/compiler/printer/auto-generate.ts +27 -0
- package/src/compiler/printer/decorator-helpers.ts +218 -0
- package/src/compiler/printer/emit-context.ts +679 -0
- package/src/compiler/printer/emit-flags.ts +64 -0
- package/src/compiler/printer/factory-base.ts +2577 -0
- package/src/compiler/printer/factory.ts +764 -0
- package/src/compiler/printer/helpers.ts +291 -0
- package/src/compiler/printer/name-generator.ts +372 -0
- package/src/compiler/printer/printer.ts +5072 -0
- package/src/compiler/printer/text-writer.ts +183 -0
- package/src/compiler/printer/utilities.ts +884 -0
- package/src/compiler/program-checker.ts +592 -0
- package/src/compiler/program-utilities.ts +435 -0
- package/src/compiler/program.ts +1419 -0
- package/src/compiler/pseudo-bigint.ts +62 -0
- package/src/compiler/regexp-checker.ts +1142 -0
- package/src/compiler/relations.ts +5149 -0
- package/src/compiler/reparser.ts +1566 -0
- package/src/compiler/scanner.ts +2951 -0
- package/src/compiler/show-config.ts +239 -0
- package/src/compiler/side-state.ts +125 -0
- package/src/compiler/sourcemap.ts +366 -0
- package/src/compiler/spelling.ts +122 -0
- package/src/compiler/symbol-accessibility.ts +1071 -0
- package/src/compiler/symbol.ts +439 -0
- package/src/compiler/symlinks.ts +137 -0
- package/src/compiler/syntax-kind.ts +391 -0
- package/src/compiler/thread-entry.ts +191 -0
- package/src/compiler/timeline.ts +53 -0
- package/src/compiler/transformers/alias-references.ts +757 -0
- package/src/compiler/transformers/async-utilities.ts +335 -0
- package/src/compiler/transformers/async.ts +838 -0
- package/src/compiler/transformers/class-fields.ts +2967 -0
- package/src/compiler/transformers/class-utilities.ts +324 -0
- package/src/compiler/transformers/commonjs-module.ts +1621 -0
- package/src/compiler/transformers/const-enum-inliner.ts +50 -0
- package/src/compiler/transformers/declarations-diagnostics.ts +832 -0
- package/src/compiler/transformers/declarations-pseudo.ts +966 -0
- package/src/compiler/transformers/declarations-resolver.ts +249 -0
- package/src/compiler/transformers/declarations-syntactic.ts +1859 -0
- package/src/compiler/transformers/declarations-tracker.ts +269 -0
- package/src/compiler/transformers/declarations.ts +3120 -0
- package/src/compiler/transformers/decorator-metadata.ts +697 -0
- package/src/compiler/transformers/decorator-utilities.ts +283 -0
- package/src/compiler/transformers/destructuring.ts +708 -0
- package/src/compiler/transformers/downlevel.ts +50 -0
- package/src/compiler/transformers/emit-resolver.ts +258 -0
- package/src/compiler/transformers/es-decorators.ts +2682 -0
- package/src/compiler/transformers/exponentiation.ts +80 -0
- package/src/compiler/transformers/external-module-info.ts +412 -0
- package/src/compiler/transformers/for-await.ts +687 -0
- package/src/compiler/transformers/import-elision.ts +152 -0
- package/src/compiler/transformers/jsx-entities.ts +256 -0
- package/src/compiler/transformers/jsx.ts +824 -0
- package/src/compiler/transformers/legacy-decorators.ts +726 -0
- package/src/compiler/transformers/logical-assignment.ts +75 -0
- package/src/compiler/transformers/module-utilities.ts +42 -0
- package/src/compiler/transformers/module.ts +474 -0
- package/src/compiler/transformers/named-evaluation.ts +389 -0
- package/src/compiler/transformers/nullish-coalescing.ts +35 -0
- package/src/compiler/transformers/object-rest-spread.ts +577 -0
- package/src/compiler/transformers/optional-catch.ts +29 -0
- package/src/compiler/transformers/optional-chain.ts +246 -0
- package/src/compiler/transformers/reference-resolver.ts +1201 -0
- package/src/compiler/transformers/runtime-syntax.ts +942 -0
- package/src/compiler/transformers/subtree-facts.ts +245 -0
- package/src/compiler/transformers/syntax-facts.ts +353 -0
- package/src/compiler/transformers/tagged-template.ts +167 -0
- package/src/compiler/transformers/transformer.ts +56 -0
- package/src/compiler/transformers/type-eraser.ts +520 -0
- package/src/compiler/transformers/type-serialization.ts +374 -0
- package/src/compiler/transformers/using.ts +613 -0
- package/src/compiler/transformers/utilities.ts +419 -0
- package/src/compiler/transformers/visitor-base.ts +1601 -0
- package/src/compiler/transformers/visitor-hooks.ts +252 -0
- package/src/compiler/tsc-build.ts +1418 -0
- package/src/compiler/tsc-cli.ts +666 -0
- package/src/compiler/tsc-compile.ts +437 -0
- package/src/compiler/tsc-watch.ts +854 -0
- package/src/compiler/tsconfig.ts +1574 -0
- package/src/compiler/type-printer.ts +110 -0
- package/src/compiler/types.ts +115 -0
- package/src/compiler/unicode-identifiers.ts +212 -0
- package/src/compiler/unicode-properties.ts +537 -0
- package/src/compiler/vfs-match.ts +597 -0
- package/src/compiler/xxh3.ts +650 -0
- package/src/library/ast.generated.ts +141 -167
- package/src/library/ast.ts +4 -1
- package/src/library/checker.ts +384 -0
- package/src/library/diagnostics.ts +72 -0
- package/src/library/disk.ts +73 -0
- package/src/library/errors.ts +26 -0
- package/src/library/host.ts +63 -0
- package/src/library/index.ts +21 -0
- package/src/library/nodes.ts +232 -0
- package/src/library/options.ts +449 -0
- package/src/library/parse.ts +63 -0
- package/src/library/program.ts +293 -0
- package/src/library/types.ts +14 -1
|
@@ -0,0 +1,1842 @@
|
|
|
1
|
+
// Type operators: `keyof`, indexed access, mapped, conditional, substitution and string mapping
|
|
2
|
+
// types, and genericity tests.
|
|
3
|
+
import { sideOf } from './side-state.js';
|
|
4
|
+
import { getTypeOfPropertyOfContextualType } from './checker-contextual.js';
|
|
5
|
+
import { checkDeprecatedIndexedAccess } from './checker-deprecation.js';
|
|
6
|
+
import { isSelfTypeAccess, markPropertyAsReferenced } from './checker-unused.js';
|
|
7
|
+
import { getName, getQuestionToken, getType, getSymbol, getDotDotDotToken, getElements, getExpression, getText, } from './ast.js';
|
|
8
|
+
import { getSymbolId } from './symbol.js';
|
|
9
|
+
import { cachedTypeKey, ConditionalRoot, ConditionalType, IndexInfo, StringMappingType, appendTypeMapping, prependTypeMapping, newTypeMapper, ReferenceArgs, } from './checker-types.js';
|
|
10
|
+
import { aliasKey, createArrayTypeEx, createTupleTypeEx, everyType, filterType, getElementTypes, getIntersectionType, getIntersectionTypeEx, getRegularTypeOfLiteralType, getStringLiteralType, getTemplateLiteralType, getTotalFixedElementCount, getTypeArguments, getUnionType, getUnionTypeEx, isArrayType, isEmptyAnonymousObjectType, isErrorType, isGenericStringLikeType, isGenericTupleType, isPatternLiteralType, isReadonlyArrayType, isTupleType, mapType, mapTypeEx, intersectTypes, mapTypeWithAlias, newIndexType, newIndexedAccessType, newObjectType, newSubstitutionType, setStructuredTypeMembers, someType, tupleTargetOf, typeListKey, typesOf, typeListHash, } from './checker-type-factory.js';
|
|
11
|
+
import { appendIndexInfo, getApparentType, getApplicableIndexInfo, getDeclarationModifierFlagsFromSymbol, getIndexInfoOfType, getPropertyOfObjectType, getIndexInfosOfType, getPropertiesOfType, getPropertyOfType, getReducedApparentType, getReducedType, getBaseConstraintOfType, getIndexTypeOfType, hasPropertiesOfType, keepPropertiesOnly, memberResolutionFlags, } from './checker-members.js';
|
|
12
|
+
import { getLateBoundSymbol, getSymbolOfDeclaration, popTypeResolution, pushTypeResolution, getParentOfSymbol, findResolutionCycleStartIndex, getFullyQualifiedName, } from './checker-symbols.js';
|
|
13
|
+
import { getConstraintOfTypeParameter, getDeclaredTypeOfTypeParameter, getElementTypeOfArrayType, getInferTypeParameters, getOuterTypeParameters, hasNonCircularBaseConstraint, } from './checker-declared-types.js';
|
|
14
|
+
import { addOptionality, getOptionalType, getTypeOfSymbol, getWriteTypeOfSymbol, isReadonlySymbol, removeMissingOrUndefinedType, } from './checker-symbol-types.js';
|
|
15
|
+
import { getTypeFromTypeNode, getAliasForTypeNode } from './checker-type-nodes.js';
|
|
16
|
+
import { combineTypeMappers, getRestrictiveInstantiation, instantiateAnonymousType, isRestrictiveInstance, instantiateType, instantiateTypeAlias, isTypeParameterPossiblyReferenced, containsTypeParameterReference, isNodeDescendantOf, } from './instantiation.js';
|
|
17
|
+
import { getVariances, isMarkerType, isTypeAssignableTo } from './relations.js';
|
|
18
|
+
import { symbolToString, typeToString } from './type-printer.js';
|
|
19
|
+
import { getSourceFileOfNode, isAssignmentTarget, isEntityNameExpression, nodeText } from './binder-utilities.js';
|
|
20
|
+
import { getTextOfNodeFromSourceText } from './ast-utilities.js';
|
|
21
|
+
import { getMinArgumentCount, getSingleCallSignature, getTypeAtPosition } from './checker-signatures.js';
|
|
22
|
+
import { getAssignmentTargetKind, getFlowTypeOfReference } from './checker-narrowing.js';
|
|
23
|
+
import { getSuggestionForNonexistentProperty, isAssignmentToReadonlyEntity, isDeleteTarget, typeHasStaticProperty, } from './checker-property-access.js';
|
|
24
|
+
import { checkComputedPropertyName, checkExpression } from './checker-expressions.js';
|
|
25
|
+
import { getPropertyNameFromType, isNumericLiteralName, isTypeUsableAsPropertyName, isValidIndexKeyType, maybeTypeOfKind, isLateBoundName, } from './checker-utilities.js';
|
|
26
|
+
import { inferTypesForConditional, inferTypesFromTemplateLiteralType, isValidBigIntString, isValidNumberString, instantiateReverseMappedType as instantiateReverseMappedTypeOfInference, resolveReverseMappedTypeMembers as resolveReverseMappedTypeMembersOfInference, } from './checker-inference.js';
|
|
27
|
+
// ---- Genericity
|
|
28
|
+
export function isGenericType(c, t) {
|
|
29
|
+
return getGenericObjectFlags(c, t) !== 0;
|
|
30
|
+
}
|
|
31
|
+
export function isGenericObjectType(c, t) {
|
|
32
|
+
return (getGenericObjectFlags(c, t) & 8388608 /* ObjectFlags.IsGenericObjectType */) !== 0;
|
|
33
|
+
}
|
|
34
|
+
export function isGenericIndexType(c, t) {
|
|
35
|
+
return (getGenericObjectFlags(c, t) & 16777216 /* ObjectFlags.IsGenericIndexType */) !== 0;
|
|
36
|
+
}
|
|
37
|
+
function getGenericObjectFlags(c, t) {
|
|
38
|
+
if ((t.flags & (402653184 /* TypeFlags.UnionOrIntersection */ | 16777216 /* TypeFlags.Substitution */)) !== 0) {
|
|
39
|
+
if ((t.objectFlags & 4194304 /* ObjectFlags.IsGenericTypeComputed */) !== 0)
|
|
40
|
+
return t.objectFlags & 25165824 /* ObjectFlags.IsGenericType */;
|
|
41
|
+
const side = sideOf(c, t);
|
|
42
|
+
if ((side.objectFlags & 4194304 /* ObjectFlags.IsGenericTypeComputed */) === 0) {
|
|
43
|
+
let combined = 0;
|
|
44
|
+
if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0)
|
|
45
|
+
for (const u of typesOf(t))
|
|
46
|
+
combined |= getGenericObjectFlags(c, u);
|
|
47
|
+
else {
|
|
48
|
+
const s = t;
|
|
49
|
+
combined = getGenericObjectFlags(c, s.baseType) | getGenericObjectFlags(c, s.constraint);
|
|
50
|
+
}
|
|
51
|
+
side.objectFlags |= 4194304 /* ObjectFlags.IsGenericTypeComputed */ | combined;
|
|
52
|
+
}
|
|
53
|
+
return side.objectFlags & 25165824 /* ObjectFlags.IsGenericType */;
|
|
54
|
+
}
|
|
55
|
+
let combined = 0;
|
|
56
|
+
if ((t.flags & 117964800 /* TypeFlags.InstantiableNonPrimitive */) !== 0 || isGenericMappedType(c, t) || isGenericTupleType(t))
|
|
57
|
+
combined |= 8388608 /* ObjectFlags.IsGenericObjectType */;
|
|
58
|
+
if ((t.flags & (117964800 /* TypeFlags.InstantiableNonPrimitive */ | 2097152 /* TypeFlags.Index */)) !== 0 || isGenericStringLikeType(c, t))
|
|
59
|
+
combined |= 16777216 /* ObjectFlags.IsGenericIndexType */;
|
|
60
|
+
return combined;
|
|
61
|
+
}
|
|
62
|
+
export function isGenericMappedType(c, t) {
|
|
63
|
+
if ((t.objectFlags & 32 /* ObjectFlags.Mapped */) === 0 || (t.flags & 1048576 /* TypeFlags.Object */) === 0)
|
|
64
|
+
return false;
|
|
65
|
+
const mapped = t;
|
|
66
|
+
const constraint = getConstraintTypeFromMappedType(c, mapped);
|
|
67
|
+
if (isGenericIndexType(c, constraint))
|
|
68
|
+
return true;
|
|
69
|
+
// Generic when the `as` clause references generic types other than the iteration type.
|
|
70
|
+
const nameType = getNameTypeFromMappedType(c, mapped);
|
|
71
|
+
return (nameType !== undefined &&
|
|
72
|
+
isGenericIndexType(c, instantiateType(c, nameType, newTypeMapper([getTypeParameterFromMappedType(c, mapped)], [constraint]))));
|
|
73
|
+
}
|
|
74
|
+
/** Unions reducible on instantiation stay generic so the reduction flows through. */
|
|
75
|
+
export function isGenericReducibleType(c, t) {
|
|
76
|
+
if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0) {
|
|
77
|
+
if ((t.objectFlags & 33554432 /* ObjectFlags.ContainsIntersections */) === 0)
|
|
78
|
+
return false;
|
|
79
|
+
for (const u of typesOf(t))
|
|
80
|
+
if (isGenericReducibleType(c, u))
|
|
81
|
+
return true;
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return (t.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && isReducibleIntersection(c, t);
|
|
85
|
+
}
|
|
86
|
+
function isReducibleIntersection(c, t) {
|
|
87
|
+
const side = sideOf(c, t);
|
|
88
|
+
if (side.uniqueLiteralFilledInstantiation === undefined)
|
|
89
|
+
side.uniqueLiteralFilledInstantiation = instantiateType(c, t, c.uniqueLiteralMapper);
|
|
90
|
+
return getReducedType(c, side.uniqueLiteralFilledInstantiation) !== side.uniqueLiteralFilledInstantiation;
|
|
91
|
+
}
|
|
92
|
+
// ---- keyof
|
|
93
|
+
export function getIndexType(c, t) {
|
|
94
|
+
return getIndexTypeEx(c, t, 0 /* IndexFlags.None */);
|
|
95
|
+
}
|
|
96
|
+
export function getIndexTypeEx(c, t, indexFlags) {
|
|
97
|
+
t = getReducedType(c, t);
|
|
98
|
+
if (isNoInferType(c, t))
|
|
99
|
+
return getNoInferType(c, getIndexTypeEx(c, t.baseType, indexFlags));
|
|
100
|
+
if (shouldDeferIndexType(c, t, indexFlags))
|
|
101
|
+
return getIndexTypeForGenericType(c, t, indexFlags);
|
|
102
|
+
if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
|
|
103
|
+
const types = [];
|
|
104
|
+
for (const u of typesOf(t))
|
|
105
|
+
types.push(getIndexTypeEx(c, u, indexFlags));
|
|
106
|
+
return (t.flags & 134217728 /* TypeFlags.Union */) !== 0 ? getIntersectionType(c, types) : getUnionType(c, types);
|
|
107
|
+
}
|
|
108
|
+
if ((t.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0 && (t.flags & 1048576 /* TypeFlags.Object */) !== 0)
|
|
109
|
+
return getIndexForMappedType(c, t, indexFlags);
|
|
110
|
+
if (t === c.wildcardType)
|
|
111
|
+
return c.wildcardType;
|
|
112
|
+
if ((t.flags & 2 /* TypeFlags.Unknown */) !== 0)
|
|
113
|
+
return c.neverType;
|
|
114
|
+
if ((t.flags & (1 /* TypeFlags.Any */ | 262144 /* TypeFlags.Never */)) !== 0)
|
|
115
|
+
return c.stringNumberSymbolType;
|
|
116
|
+
const include = ((indexFlags & 2 /* IndexFlags.NoIndexSignatures */) !== 0 ? 1024 /* TypeFlags.StringLiteral */ : 12583968 /* TypeFlags.StringLike */) |
|
|
117
|
+
((indexFlags & 1 /* IndexFlags.StringsOnly */) !== 0 ? 0 : 67648 /* TypeFlags.NumberLike */ | 16896 /* TypeFlags.ESSymbolLike */);
|
|
118
|
+
return getLiteralTypeFromProperties(c, t, include, indexFlags === 0 /* IndexFlags.None */);
|
|
119
|
+
}
|
|
120
|
+
export function shouldDeferIndexType(c, t, indexFlags) {
|
|
121
|
+
return ((t.flags & 117964800 /* TypeFlags.InstantiableNonPrimitive */) !== 0 ||
|
|
122
|
+
isGenericTupleType(t) ||
|
|
123
|
+
(isGenericMappedType(c, t) && getNameTypeFromMappedType(c, t) !== undefined) ||
|
|
124
|
+
((t.flags & 134217728 /* TypeFlags.Union */) !== 0 &&
|
|
125
|
+
(indexFlags & 4 /* IndexFlags.NoReducibleCheck */) === 0 &&
|
|
126
|
+
isGenericReducibleType(c, t)) ||
|
|
127
|
+
((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0 &&
|
|
128
|
+
maybeTypeOfKind(t, 132644864 /* TypeFlags.Instantiable */) &&
|
|
129
|
+
someEmptyAnonymousObjectType(c, typesOf(t))));
|
|
130
|
+
}
|
|
131
|
+
function someEmptyAnonymousObjectType(c, types) {
|
|
132
|
+
for (const t of types)
|
|
133
|
+
if (isEmptyAnonymousObjectType(c, t))
|
|
134
|
+
return true;
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
function getIndexTypeForGenericType(c, t, indexFlags) {
|
|
138
|
+
const key = cachedTypeKey((indexFlags & 1 /* IndexFlags.StringsOnly */) !== 0 ? 2 /* CachedTypeKind.StringIndexType */ : 1 /* CachedTypeKind.IndexType */, t.id);
|
|
139
|
+
let indexType = c.cachedTypesById.get(key);
|
|
140
|
+
if (indexType === undefined) {
|
|
141
|
+
indexType = newIndexType(c, t, indexFlags & 1 /* IndexFlags.StringsOnly */);
|
|
142
|
+
c.cachedTypesById.set(key, indexType);
|
|
143
|
+
}
|
|
144
|
+
return indexType;
|
|
145
|
+
}
|
|
146
|
+
export function getLiteralTypeFromProperties(c, t, include, includeOrigin) {
|
|
147
|
+
const key = 'props' +
|
|
148
|
+
t.id +
|
|
149
|
+
':' +
|
|
150
|
+
include +
|
|
151
|
+
(includeOrigin ? 'o' : '') +
|
|
152
|
+
((memberResolutionFlags(c, t) & 536870912 /* ObjectFlags.UnresolvedMembers */) !== 0 ? 'u' : '');
|
|
153
|
+
const cached = c.cachedTypes.get(key);
|
|
154
|
+
if (cached !== undefined)
|
|
155
|
+
return cached;
|
|
156
|
+
const origin = (includeOrigin && (t.objectFlags & (3 /* ObjectFlags.ClassOrInterface */ | 4 /* ObjectFlags.Reference */)) !== 0) ||
|
|
157
|
+
t.alias !== undefined
|
|
158
|
+
? newIndexType(c, t, 0 /* IndexFlags.None */)
|
|
159
|
+
: undefined;
|
|
160
|
+
const types = [];
|
|
161
|
+
for (const prop of getPropertiesOfType(c, t))
|
|
162
|
+
types.push(getLiteralTypeFromProperty(c, prop, include, false));
|
|
163
|
+
for (const info of getIndexInfosOfType(c, t))
|
|
164
|
+
if (info !== c.enumNumberIndexInfo && isKeyTypeIncluded(info.keyType, include))
|
|
165
|
+
types.push(info.keyType === c.stringType && (include & 64 /* TypeFlags.Number */) !== 0 ? c.stringOrNumberType : info.keyType);
|
|
166
|
+
const result = getUnionTypeEx(c, types, 1 /* UnionReduction.Literal */, undefined, origin);
|
|
167
|
+
c.cachedTypes.set(key, result);
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
export function getLiteralTypeFromProperty(c, prop, include, includeNonPublic) {
|
|
171
|
+
if (includeNonPublic ||
|
|
172
|
+
(getDeclarationModifierFlagsFromSymbol(c, prop, false) & 6 /* ModifierFlags.NonPublicAccessibilityModifier */) === 0) {
|
|
173
|
+
let t = c.symbolNameTypes.get(getSymbolId(getLateBoundSymbol(c, prop)));
|
|
174
|
+
if (t === undefined) {
|
|
175
|
+
if (prop.name === "default" /* InternalSymbolName.Default */)
|
|
176
|
+
t = getStringLiteralType(c, 'default');
|
|
177
|
+
else {
|
|
178
|
+
const name = prop.valueDeclaration !== undefined ? getName(prop.valueDeclaration) : undefined;
|
|
179
|
+
if (name !== undefined)
|
|
180
|
+
t = getLiteralTypeFromPropertyName(c, name);
|
|
181
|
+
if (t === undefined && !isKnownSymbol(prop))
|
|
182
|
+
t = getStringLiteralType(c, symbolNameText(prop));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (t !== undefined && (t.flags & include) !== 0)
|
|
186
|
+
return t;
|
|
187
|
+
}
|
|
188
|
+
return c.neverType;
|
|
189
|
+
}
|
|
190
|
+
function isKnownSymbol(symbol) {
|
|
191
|
+
return symbol.name.startsWith('@');
|
|
192
|
+
}
|
|
193
|
+
function symbolNameText(symbol) {
|
|
194
|
+
return symbol.name;
|
|
195
|
+
}
|
|
196
|
+
export function getLiteralTypeFromPropertyName(c, name) {
|
|
197
|
+
if (name.kind === 80 /* SyntaxKind.PrivateIdentifier */)
|
|
198
|
+
return c.neverType;
|
|
199
|
+
if (name.kind === 8 /* SyntaxKind.NumericLiteral */)
|
|
200
|
+
return getRegularTypeOfLiteralType(c, checkExpression(c, name));
|
|
201
|
+
if (name.kind === 168 /* SyntaxKind.ComputedPropertyName */)
|
|
202
|
+
return getRegularTypeOfLiteralType(c, checkComputedPropertyName(c, name));
|
|
203
|
+
if (name.kind === 79 /* SyntaxKind.Identifier */ ||
|
|
204
|
+
name.kind === 10 /* SyntaxKind.StringLiteral */ ||
|
|
205
|
+
name.kind === 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */)
|
|
206
|
+
return getStringLiteralType(c, getText(name));
|
|
207
|
+
if (name.kind === 296 /* SyntaxKind.JsxNamespacedName */)
|
|
208
|
+
return getStringLiteralType(c, nodeText(name));
|
|
209
|
+
if (name.kind === 9 /* SyntaxKind.BigIntLiteral */)
|
|
210
|
+
return getRegularTypeOfLiteralType(c, checkExpression(c, name));
|
|
211
|
+
return c.neverType;
|
|
212
|
+
}
|
|
213
|
+
function isKeyTypeIncluded(keyType, include) {
|
|
214
|
+
if ((keyType.flags & include) !== 0)
|
|
215
|
+
return true;
|
|
216
|
+
if ((keyType.flags & 268435456 /* TypeFlags.Intersection */) === 0)
|
|
217
|
+
return false;
|
|
218
|
+
for (const t of typesOf(keyType))
|
|
219
|
+
if (isKeyTypeIncluded(t, include))
|
|
220
|
+
return true;
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
/** The keys of a mapped type, without resolving its properties. */
|
|
224
|
+
export function getIndexForMappedType(c, t, indexFlags = 0 /* IndexFlags.None */) {
|
|
225
|
+
const typeParameter = getTypeParameterFromMappedType(c, t);
|
|
226
|
+
const constraintType = getConstraintTypeFromMappedType(c, t);
|
|
227
|
+
const nameType = getNameTypeFromMappedType(c, mappedTypeTarget(t));
|
|
228
|
+
if (nameType === undefined && (indexFlags & 2 /* IndexFlags.NoIndexSignatures */) === 0)
|
|
229
|
+
return constraintType;
|
|
230
|
+
const keyTypes = [];
|
|
231
|
+
const add = (keyType) => {
|
|
232
|
+
const propNameType = nameType !== undefined
|
|
233
|
+
? instantiateType(c, nameType, appendTypeMapping(t.mapper, typeParameter, keyType))
|
|
234
|
+
: keyType;
|
|
235
|
+
// `keyof` of a string index signature is `string | number`.
|
|
236
|
+
keyTypes.push(propNameType === c.stringType ? c.stringOrNumberType : propNameType);
|
|
237
|
+
};
|
|
238
|
+
if (isGenericIndexType(c, constraintType)) {
|
|
239
|
+
if (isMappedTypeWithKeyofConstraintDeclaration(c, t))
|
|
240
|
+
return getIndexTypeForGenericType(c, t, indexFlags);
|
|
241
|
+
forEachType(constraintType, add);
|
|
242
|
+
}
|
|
243
|
+
else if (isMappedTypeWithKeyofConstraintDeclaration(c, t)) {
|
|
244
|
+
const modifiersType = getApparentType(c, getModifiersTypeFromMappedType(c, t));
|
|
245
|
+
forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(c, modifiersType, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, (indexFlags & 1 /* IndexFlags.StringsOnly */) !== 0, add);
|
|
246
|
+
}
|
|
247
|
+
else
|
|
248
|
+
forEachType(getLowerBoundOfKeyType(c, constraintType), add);
|
|
249
|
+
const result = (indexFlags & 2 /* IndexFlags.NoIndexSignatures */) !== 0
|
|
250
|
+
? filterType(c, getUnionType(c, keyTypes), (u) => (u.flags & (1 /* TypeFlags.Any */ | 32 /* TypeFlags.String */)) === 0)
|
|
251
|
+
: getUnionType(c, keyTypes);
|
|
252
|
+
if ((result.flags & 134217728 /* TypeFlags.Union */) !== 0 &&
|
|
253
|
+
(constraintType.flags & 134217728 /* TypeFlags.Union */) !== 0 &&
|
|
254
|
+
typeListKey(typesOf(result)) === typeListKey(typesOf(constraintType)))
|
|
255
|
+
return constraintType;
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
export function forEachType(t, f) {
|
|
259
|
+
if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0)
|
|
260
|
+
for (const u of typesOf(t))
|
|
261
|
+
f(u);
|
|
262
|
+
else
|
|
263
|
+
f(t);
|
|
264
|
+
}
|
|
265
|
+
function forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(c, t, include, stringsOnly, cb) {
|
|
266
|
+
for (const prop of getPropertiesOfType(c, t))
|
|
267
|
+
cb(getLiteralTypeFromProperty(c, prop, include, false));
|
|
268
|
+
if ((t.flags & 1 /* TypeFlags.Any */) !== 0)
|
|
269
|
+
cb(c.stringType);
|
|
270
|
+
else
|
|
271
|
+
for (const info of getIndexInfosOfType(c, t))
|
|
272
|
+
if (!stringsOnly || (info.keyType.flags & (32 /* TypeFlags.String */ | 4194304 /* TypeFlags.TemplateLiteral */)) !== 0)
|
|
273
|
+
cb(info.keyType);
|
|
274
|
+
}
|
|
275
|
+
/** The keys a generic mapped type with an `as` clause maps the apparent keys of its modifiers type to. */
|
|
276
|
+
export function getApparentMappedTypeKeys(c, nameType, targetType) {
|
|
277
|
+
const modifiersType = getApparentType(c, getModifiersTypeFromMappedType(c, targetType));
|
|
278
|
+
const mappedKeys = [];
|
|
279
|
+
forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(c, modifiersType, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, false, (t) => {
|
|
280
|
+
mappedKeys.push(instantiateType(c, nameType, appendTypeMapping(targetType.mapper, getTypeParameterFromMappedType(c, targetType), t)));
|
|
281
|
+
});
|
|
282
|
+
return getUnionType(c, mappedKeys);
|
|
283
|
+
}
|
|
284
|
+
/** Keys known to always be present, e.g. from constraints of type parameters. */
|
|
285
|
+
function getLowerBoundOfKeyType(c, t) {
|
|
286
|
+
if ((t.flags & 2097152 /* TypeFlags.Index */) !== 0) {
|
|
287
|
+
const target = getApparentType(c, t.target);
|
|
288
|
+
return isGenericTupleType(target) ? getKnownKeysOfTupleType(c, target) : getIndexType(c, target);
|
|
289
|
+
}
|
|
290
|
+
if ((t.flags & 67108864 /* TypeFlags.Conditional */) !== 0) {
|
|
291
|
+
const conditional = t;
|
|
292
|
+
if (conditional.root.isDistributive) {
|
|
293
|
+
const checkType = conditional.checkType;
|
|
294
|
+
const constraint = getLowerBoundOfKeyType(c, checkType);
|
|
295
|
+
if (constraint !== checkType)
|
|
296
|
+
return getConditionalTypeInstantiation(c, conditional, prependTypeMapping(conditional.root.checkType, constraint, conditional.mapper), false, undefined);
|
|
297
|
+
}
|
|
298
|
+
return t;
|
|
299
|
+
}
|
|
300
|
+
if ((t.flags & 134217728 /* TypeFlags.Union */) !== 0)
|
|
301
|
+
return mapTypeEx(c, t, (u) => getLowerBoundOfKeyType(c, u), true);
|
|
302
|
+
if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
|
|
303
|
+
const types = typesOf(t);
|
|
304
|
+
// Keep `string & {}` and friends, which prevent subtype reduction.
|
|
305
|
+
if (types.length === 2 &&
|
|
306
|
+
(types[0].flags & (32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */ | 128 /* TypeFlags.BigInt */)) !== 0 &&
|
|
307
|
+
types[1] === c.emptyTypeLiteralType)
|
|
308
|
+
return t;
|
|
309
|
+
const bounds = [];
|
|
310
|
+
for (const u of types)
|
|
311
|
+
bounds.push(getLowerBoundOfKeyType(c, u));
|
|
312
|
+
return getIntersectionType(c, bounds);
|
|
313
|
+
}
|
|
314
|
+
return t;
|
|
315
|
+
}
|
|
316
|
+
function getKnownKeysOfTupleType(c, t) {
|
|
317
|
+
const target = tupleTargetOf(t);
|
|
318
|
+
const keys = [];
|
|
319
|
+
for (let i = 0; i < target.fixedLength; i++)
|
|
320
|
+
keys.push(getStringLiteralType(c, String(i)));
|
|
321
|
+
keys.push(getIndexType(c, target.readonly ? c.globalReadonlyArrayType : c.globalArrayType));
|
|
322
|
+
return getUnionType(c, keys);
|
|
323
|
+
}
|
|
324
|
+
// ---- Indexed access
|
|
325
|
+
export function getIndexedAccessType(c, objectType, indexType) {
|
|
326
|
+
return getIndexedAccessTypeEx(c, objectType, indexType, 0 /* AccessFlags.None */, undefined, undefined);
|
|
327
|
+
}
|
|
328
|
+
export function getIndexedAccessTypeEx(c, objectType, indexType, accessFlags, accessNode, alias) {
|
|
329
|
+
return (getIndexedAccessTypeOrUndefinedEx(c, objectType, indexType, accessFlags, accessNode, alias) ??
|
|
330
|
+
(accessNode !== undefined ? c.errorType : c.unknownType));
|
|
331
|
+
}
|
|
332
|
+
export function getIndexedAccessTypeOrUndefined(c, objectType, indexType, accessFlags) {
|
|
333
|
+
return getIndexedAccessTypeOrUndefinedEx(c, objectType, indexType, accessFlags, undefined, undefined);
|
|
334
|
+
}
|
|
335
|
+
export function getIndexedAccessTypeOrUndefinedEx(c, objectType, indexType, accessFlags, accessNode, alias) {
|
|
336
|
+
if (objectType === c.wildcardType || indexType === c.wildcardType)
|
|
337
|
+
return c.wildcardType;
|
|
338
|
+
objectType = getReducedType(c, objectType);
|
|
339
|
+
// A type with only a string index signature always yields that signature's type.
|
|
340
|
+
if (isStringIndexSignatureOnlyType(c, objectType) &&
|
|
341
|
+
(indexType.flags & 12 /* TypeFlags.Nullable */) === 0 &&
|
|
342
|
+
isTypeAssignableToKind(c, indexType, 32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */))
|
|
343
|
+
indexType = c.stringType;
|
|
344
|
+
if (c.noUncheckedIndexedAccess && (accessFlags & 32 /* AccessFlags.ExpressionPosition */) !== 0)
|
|
345
|
+
accessFlags |= 1 /* AccessFlags.IncludeUndefined */;
|
|
346
|
+
// Generic index or object types defer to an indexed access type.
|
|
347
|
+
if (shouldDeferIndexedAccessType(c, objectType, indexType, accessNode)) {
|
|
348
|
+
if ((objectType.flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0)
|
|
349
|
+
return objectType;
|
|
350
|
+
const persistent = accessFlags & 1 /* AccessFlags.Persistent */;
|
|
351
|
+
const key = objectType.id + ',' + indexType.id + ',' + accessFlags + aliasKey(alias);
|
|
352
|
+
let t = c.indexedAccessTypes.get(key);
|
|
353
|
+
if (t === undefined) {
|
|
354
|
+
t = newIndexedAccessType(c, objectType, indexType, persistent);
|
|
355
|
+
t.alias = alias;
|
|
356
|
+
c.indexedAccessTypes.set(key, t);
|
|
357
|
+
}
|
|
358
|
+
return t;
|
|
359
|
+
}
|
|
360
|
+
// `boolean` stays whole for better errors.
|
|
361
|
+
const apparentObjectType = getReducedApparentType(c, objectType);
|
|
362
|
+
if ((indexType.flags & 134217728 /* TypeFlags.Union */) !== 0 && (indexType.flags & 256 /* TypeFlags.Boolean */) === 0) {
|
|
363
|
+
const propTypes = [];
|
|
364
|
+
let wasMissingProp = false;
|
|
365
|
+
for (const t of typesOf(indexType)) {
|
|
366
|
+
const propType = getPropertyTypeForIndexType(c, objectType, apparentObjectType, t, indexType, accessNode, accessFlags | (wasMissingProp ? 128 /* AccessFlags.SuppressNoImplicitAnyError */ : 0));
|
|
367
|
+
if (propType !== undefined)
|
|
368
|
+
propTypes.push(propType);
|
|
369
|
+
else if (accessNode === undefined)
|
|
370
|
+
return undefined;
|
|
371
|
+
else
|
|
372
|
+
wasMissingProp = true;
|
|
373
|
+
}
|
|
374
|
+
if (wasMissingProp)
|
|
375
|
+
return undefined;
|
|
376
|
+
if ((accessFlags & 4 /* AccessFlags.Writing */) !== 0)
|
|
377
|
+
return getIntersectionTypeEx(c, propTypes, 0 /* IntersectionFlags.None */, alias);
|
|
378
|
+
return getUnionTypeEx(c, propTypes, 1 /* UnionReduction.Literal */, alias, undefined);
|
|
379
|
+
}
|
|
380
|
+
return getPropertyTypeForIndexType(c, objectType, apparentObjectType, indexType, indexType, accessNode, accessFlags | 8 /* AccessFlags.CacheSymbol */ | 64 /* AccessFlags.ReportDeprecated */);
|
|
381
|
+
}
|
|
382
|
+
function shouldDeferIndexedAccessType(c, objectType, indexType, accessNode) {
|
|
383
|
+
if (isGenericIndexType(c, indexType))
|
|
384
|
+
return true;
|
|
385
|
+
if (accessNode !== undefined && accessNode.kind !== 200 /* SyntaxKind.IndexedAccessType */)
|
|
386
|
+
return (isGenericTupleType(objectType) &&
|
|
387
|
+
!indexTypeLessThan(indexType, getTotalFixedElementCount(tupleTargetOf(objectType))));
|
|
388
|
+
return ((isGenericObjectType(c, objectType) &&
|
|
389
|
+
!(isTupleType(objectType) && indexTypeLessThan(indexType, getTotalFixedElementCount(tupleTargetOf(objectType))))) ||
|
|
390
|
+
isGenericReducibleType(c, objectType));
|
|
391
|
+
}
|
|
392
|
+
function indexTypeLessThan(indexType, limit) {
|
|
393
|
+
return everyType(indexType, (t) => {
|
|
394
|
+
if ((t.flags & 3072 /* TypeFlags.StringOrNumberLiteral */) !== 0) {
|
|
395
|
+
const name = getPropertyNameFromType(t);
|
|
396
|
+
if (isNumericLiteralName(name)) {
|
|
397
|
+
const index = Number(name);
|
|
398
|
+
return index >= 0 && index < limit;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
export function isStringIndexSignatureOnlyType(c, t) {
|
|
405
|
+
return (((t.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
|
|
406
|
+
!isGenericMappedType(c, t) &&
|
|
407
|
+
!hasPropertiesOfType(c, t) &&
|
|
408
|
+
getIndexInfosOfType(c, t).length === 1 &&
|
|
409
|
+
getIndexInfoOfType(c, t, c.stringType) !== undefined) ||
|
|
410
|
+
((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0 && everyStringIndexSignatureOnlyType(c, typesOf(t))));
|
|
411
|
+
}
|
|
412
|
+
function everyStringIndexSignatureOnlyType(c, types) {
|
|
413
|
+
for (const t of types)
|
|
414
|
+
if (!isStringIndexSignatureOnlyType(c, t))
|
|
415
|
+
return false;
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
export function isTypeAssignableToKind(c, source, kind) {
|
|
419
|
+
return isTypeAssignableToKindEx(c, source, kind, false);
|
|
420
|
+
}
|
|
421
|
+
export function isTypeAssignableToKindEx(c, source, kind, strict) {
|
|
422
|
+
if ((source.flags & kind) !== 0)
|
|
423
|
+
return true;
|
|
424
|
+
if (strict && (source.flags & (3 /* TypeFlags.AnyOrUnknown */ | 16 /* TypeFlags.Void */ | 4 /* TypeFlags.Undefined */ | 8 /* TypeFlags.Null */)) !== 0)
|
|
425
|
+
return false;
|
|
426
|
+
return (((kind & 67648 /* TypeFlags.NumberLike */) !== 0 && isTypeAssignableTo(c, source, c.numberType)) ||
|
|
427
|
+
((kind & 4224 /* TypeFlags.BigIntLike */) !== 0 && isTypeAssignableTo(c, source, c.bigintType)) ||
|
|
428
|
+
((kind & 12583968 /* TypeFlags.StringLike */) !== 0 && isTypeAssignableTo(c, source, c.stringType)) ||
|
|
429
|
+
((kind & 8448 /* TypeFlags.BooleanLike */) !== 0 && isTypeAssignableTo(c, source, c.booleanType)) ||
|
|
430
|
+
((kind & 16 /* TypeFlags.Void */) !== 0 && isTypeAssignableTo(c, source, c.voidType)) ||
|
|
431
|
+
((kind & 262144 /* TypeFlags.Never */) !== 0 && isTypeAssignableTo(c, source, c.neverType)) ||
|
|
432
|
+
((kind & 8 /* TypeFlags.Null */) !== 0 && isTypeAssignableTo(c, source, c.nullType)) ||
|
|
433
|
+
((kind & 4 /* TypeFlags.Undefined */) !== 0 && isTypeAssignableTo(c, source, c.undefinedType)) ||
|
|
434
|
+
((kind & 512 /* TypeFlags.ESSymbol */) !== 0 && isTypeAssignableTo(c, source, c.esSymbolType)) ||
|
|
435
|
+
((kind & 131072 /* TypeFlags.NonPrimitive */) !== 0 && isTypeAssignableTo(c, source, c.nonPrimitiveType)));
|
|
436
|
+
}
|
|
437
|
+
export function getPropertyNameFromIndex(indexType) {
|
|
438
|
+
return isTypeUsableAsPropertyName(indexType) ? getPropertyNameFromType(indexType) : undefined;
|
|
439
|
+
}
|
|
440
|
+
/** The type of the property `indexType` selects; reporting of access errors happens in element access checks. */
|
|
441
|
+
function getPropertyTypeForIndexType(c, originalObjectType, objectType, indexType, fullIndexType, accessNode, accessFlags) {
|
|
442
|
+
const accessExpression = accessNode !== undefined && accessNode.kind === 213 /* SyntaxKind.ElementAccessExpression */ ? accessNode : undefined;
|
|
443
|
+
const propName = accessNode !== undefined && accessNode.kind === 80 /* SyntaxKind.PrivateIdentifier */
|
|
444
|
+
? undefined
|
|
445
|
+
: getPropertyNameFromIndex(indexType);
|
|
446
|
+
if (propName !== undefined) {
|
|
447
|
+
// Contextual accesses read the property without reducing a union of its types.
|
|
448
|
+
if ((accessFlags & 256 /* AccessFlags.Contextual */) !== 0)
|
|
449
|
+
return getTypeOfPropertyOfContextualType(c, objectType, propName) ?? c.anyType;
|
|
450
|
+
const prop = getPropertyOfType(c, objectType, propName);
|
|
451
|
+
if (prop !== undefined) {
|
|
452
|
+
if ((accessFlags & 64 /* AccessFlags.ReportDeprecated */) !== 0 && accessNode !== undefined)
|
|
453
|
+
checkDeprecatedIndexedAccess(c, accessNode, prop, propName);
|
|
454
|
+
if (accessExpression !== undefined) {
|
|
455
|
+
markPropertyAsReferenced(c, prop, accessExpression, isSelfTypeAccess(c, getExpression(accessExpression), objectType.symbol));
|
|
456
|
+
if (isAssignmentToReadonlyEntity(c, accessExpression, prop, getAssignmentTargetKind(accessExpression))) {
|
|
457
|
+
c.error(accessExpression.argumentExpression, 697 /* Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property */, isLateBoundName(prop.name) ? symbolToString(c, prop) : prop.name);
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
if ((accessFlags & 8 /* AccessFlags.CacheSymbol */) !== 0)
|
|
461
|
+
c.resolvedSymbols.set(accessExpression.id, prop);
|
|
462
|
+
if (isThisPropertyAccessInConstructor(c, accessExpression, prop))
|
|
463
|
+
return c.autoType;
|
|
464
|
+
}
|
|
465
|
+
const propType = (accessFlags & 4 /* AccessFlags.Writing */) !== 0 ? getWriteTypeOfSymbol(c, prop) : getTypeOfSymbol(c, prop);
|
|
466
|
+
// Each property an element access reads is narrowed by itself, as a reference to that property.
|
|
467
|
+
if (accessExpression !== undefined && getAssignmentTargetKind(accessExpression) !== 1 /* AssignmentKind.Definite */)
|
|
468
|
+
return getFlowTypeOfReference(c, accessExpression, propType, propType, undefined);
|
|
469
|
+
if (accessNode !== undefined && accessNode.kind === 200 /* SyntaxKind.IndexedAccessType */ && containsMissing(c, propType))
|
|
470
|
+
return getUnionType(c, [propType, c.undefinedType]);
|
|
471
|
+
return propType;
|
|
472
|
+
}
|
|
473
|
+
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName)) {
|
|
474
|
+
const index = Number(propName);
|
|
475
|
+
if (accessNode !== undefined &&
|
|
476
|
+
everyType(objectType, (t) => (tupleTargetOf(t).combinedFlags & 12 /* ElementFlags.Variable */) === 0) &&
|
|
477
|
+
(accessFlags & 16 /* AccessFlags.AllowMissing */) === 0) {
|
|
478
|
+
const indexNode = getIndexNodeForAccessExpression(accessNode);
|
|
479
|
+
if (isTupleType(objectType)) {
|
|
480
|
+
if (index < 0) {
|
|
481
|
+
c.error(indexNode, 674 /* Diagnostics.A_tuple_type_cannot_be_indexed_with_a_negative_value */);
|
|
482
|
+
return c.undefinedType;
|
|
483
|
+
}
|
|
484
|
+
c.error(indexNode, 653 /* Diagnostics.Tuple_type_0_of_length_1_has_no_element_at_index_2 */, typeToString(c, objectType), String(tupleTargetOf(objectType).typeParameterCount()), propName);
|
|
485
|
+
}
|
|
486
|
+
else
|
|
487
|
+
c.error(indexNode, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, propName, typeToString(c, objectType));
|
|
488
|
+
}
|
|
489
|
+
if (index >= 0) {
|
|
490
|
+
errorIfWritingToReadonlyIndex(c, getIndexInfoOfType(c, objectType, c.numberType), objectType, accessExpression);
|
|
491
|
+
return getTupleElementTypeOutOfStartCount(c, objectType, index, (accessFlags & 1 /* AccessFlags.IncludeUndefined */) !== 0 ? c.missingType : undefined);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if ((indexType.flags & 12 /* TypeFlags.Nullable */) === 0 &&
|
|
496
|
+
isTypeAssignableToKind(c, indexType, 12583968 /* TypeFlags.StringLike */ | 67648 /* TypeFlags.NumberLike */ | 16896 /* TypeFlags.ESSymbolLike */)) {
|
|
497
|
+
if ((objectType.flags & (1 /* TypeFlags.Any */ | 262144 /* TypeFlags.Never */)) !== 0)
|
|
498
|
+
return objectType;
|
|
499
|
+
// The string index signature applies even to symbol-like keys when nothing else does.
|
|
500
|
+
const indexInfo = getApplicableIndexInfo(c, objectType, indexType) ?? getIndexInfoOfType(c, objectType, c.stringType);
|
|
501
|
+
if (indexInfo !== undefined) {
|
|
502
|
+
if ((accessFlags & 2 /* AccessFlags.NoIndexSignatures */) !== 0 && indexInfo.keyType !== c.numberType) {
|
|
503
|
+
if (accessExpression !== undefined) {
|
|
504
|
+
if ((accessFlags & 4 /* AccessFlags.Writing */) !== 0)
|
|
505
|
+
c.error(accessExpression, 982 /* Diagnostics.Type_0_is_generic_and_can_only_be_indexed_for_reading */, typeToString(c, originalObjectType));
|
|
506
|
+
else
|
|
507
|
+
c.error(accessExpression, 693 /* Diagnostics.Type_0_cannot_be_used_to_index_type_1 */, typeToString(c, indexType), typeToString(c, originalObjectType));
|
|
508
|
+
}
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
if (accessNode !== undefined &&
|
|
512
|
+
indexInfo.keyType === c.stringType &&
|
|
513
|
+
!isTypeAssignableToKind(c, indexType, 32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */)) {
|
|
514
|
+
c.error(getIndexNodeForAccessExpression(accessNode), 695 /* Diagnostics.Type_0_cannot_be_used_as_an_index_type */, typeToString(c, indexType));
|
|
515
|
+
return (accessFlags & 1 /* AccessFlags.IncludeUndefined */) !== 0
|
|
516
|
+
? getUnionType(c, [indexInfo.valueType, c.missingType])
|
|
517
|
+
: indexInfo.valueType;
|
|
518
|
+
}
|
|
519
|
+
errorIfWritingToReadonlyIndex(c, indexInfo, objectType, accessExpression);
|
|
520
|
+
// An enum object indexed with its own members, as in `E[E.A]`, can't be out of bounds.
|
|
521
|
+
if ((accessFlags & 1 /* AccessFlags.IncludeUndefined */) !== 0 &&
|
|
522
|
+
!(objectType.symbol !== undefined &&
|
|
523
|
+
(objectType.symbol.flags & (256 /* SymbolFlags.RegularEnum */ | 128 /* SymbolFlags.ConstEnum */)) !== 0 &&
|
|
524
|
+
indexType.symbol !== undefined &&
|
|
525
|
+
(indexType.flags & 32768 /* TypeFlags.EnumLiteral */) !== 0 &&
|
|
526
|
+
getParentOfSymbol(c, indexType.symbol) === objectType.symbol))
|
|
527
|
+
return getUnionType(c, [indexInfo.valueType, c.missingType]);
|
|
528
|
+
return indexInfo.valueType;
|
|
529
|
+
}
|
|
530
|
+
if ((indexType.flags & 262144 /* TypeFlags.Never */) !== 0)
|
|
531
|
+
return c.neverType;
|
|
532
|
+
if (isJSLiteralType(c, objectType))
|
|
533
|
+
return c.anyType;
|
|
534
|
+
if (accessExpression !== undefined && !isConstEnumObjectType(objectType)) {
|
|
535
|
+
if (isObjectLiteralType(objectType)) {
|
|
536
|
+
if (c.noImplicitAny && (indexType.flags & (1024 /* TypeFlags.StringLiteral */ | 2048 /* TypeFlags.NumberLiteral */)) !== 0) {
|
|
537
|
+
c.error(accessExpression, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, getPropertyNameFromType(indexType), typeToString(c, objectType));
|
|
538
|
+
return c.undefinedType;
|
|
539
|
+
}
|
|
540
|
+
if ((indexType.flags & (64 /* TypeFlags.Number */ | 32 /* TypeFlags.String */)) !== 0) {
|
|
541
|
+
const types = [];
|
|
542
|
+
for (const p of getPropertiesOfType(c, objectType))
|
|
543
|
+
types.push(getTypeOfSymbol(c, p));
|
|
544
|
+
types.push(c.undefinedType);
|
|
545
|
+
return getUnionType(c, types);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const globalSymbol = objectType.symbol === c.globalThisSymbol && propName !== undefined
|
|
549
|
+
? c.globalThisSymbol.exports?.get(propName)
|
|
550
|
+
: undefined;
|
|
551
|
+
if (globalSymbol !== undefined && (globalSymbol.flags & 418 /* SymbolFlags.BlockScoped */) !== 0)
|
|
552
|
+
c.error(accessExpression, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, propName, typeToString(c, objectType));
|
|
553
|
+
else if (c.noImplicitAny && (accessFlags & 128 /* AccessFlags.SuppressNoImplicitAnyError */) === 0) {
|
|
554
|
+
const argument = accessExpression.argumentExpression;
|
|
555
|
+
const objectText = typeToString(c, objectType);
|
|
556
|
+
const suggestion = propName !== undefined ? getSuggestionForNonexistentProperty(c, propName, objectType) : undefined;
|
|
557
|
+
if (propName !== undefined && typeHasStaticProperty(c, propName, objectType))
|
|
558
|
+
c.error(accessExpression, 727 /* Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_to_access_the_static_member_2_instead */, propName, objectText, objectText + '[' + getTextOfNodeFromSourceText(getSourceFileOfNode(argument).text, argument) + ']');
|
|
559
|
+
else if (getIndexTypeOfType(c, objectType, c.numberType) !== undefined)
|
|
560
|
+
c.error(argument, 1687 /* Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number */);
|
|
561
|
+
else if (suggestion !== undefined)
|
|
562
|
+
c.error(argument, 706 /* Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2 */, propName, objectText, suggestion.name);
|
|
563
|
+
else {
|
|
564
|
+
const methodSuggestion = getSuggestionForNonexistentIndexSignature(c, objectType, accessExpression, indexType);
|
|
565
|
+
if (methodSuggestion !== '') {
|
|
566
|
+
c.error(accessExpression, 1723 /* Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1 */, objectText, methodSuggestion);
|
|
567
|
+
return undefined;
|
|
568
|
+
}
|
|
569
|
+
let head = undefined;
|
|
570
|
+
if ((indexType.flags & 32768 /* TypeFlags.EnumLiteral */) !== 0)
|
|
571
|
+
head = createDiagnosticForNode(accessExpression, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, [
|
|
572
|
+
'[' + typeToString(c, indexType) + ']',
|
|
573
|
+
objectText,
|
|
574
|
+
]);
|
|
575
|
+
else if ((indexType.flags & 16384 /* TypeFlags.UniqueESSymbol */) !== 0)
|
|
576
|
+
head = createDiagnosticForNode(accessExpression, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, [
|
|
577
|
+
'[' + getFullyQualifiedName(c, indexType.symbol, accessExpression) + ']',
|
|
578
|
+
objectText,
|
|
579
|
+
]);
|
|
580
|
+
else if ((indexType.flags & (1024 /* TypeFlags.StringLiteral */ | 2048 /* TypeFlags.NumberLiteral */)) !== 0)
|
|
581
|
+
head = createDiagnosticForNode(accessExpression, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, [
|
|
582
|
+
getPropertyNameFromType(indexType),
|
|
583
|
+
objectText,
|
|
584
|
+
]);
|
|
585
|
+
else if ((indexType.flags & (64 /* TypeFlags.Number */ | 32 /* TypeFlags.String */)) !== 0)
|
|
586
|
+
head = createDiagnosticForNode(accessExpression, 1725 /* Diagnostics.No_index_signature_with_a_parameter_of_type_0_was_found_on_type_1 */, [typeToString(c, indexType), objectText]);
|
|
587
|
+
const diagnostic = createDiagnosticForNode(accessExpression, 1724 /* Diagnostics.Element_implicitly_has_an_any_type_because_expression_of_type_0_can_t_be_used_to_index_type_1 */, [typeToString(c, fullIndexType), objectText]);
|
|
588
|
+
if (head !== undefined)
|
|
589
|
+
diagnostic.addMessageChain(head);
|
|
590
|
+
c.addDiagnostic(diagnostic);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return undefined;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
if ((accessFlags & 16 /* AccessFlags.AllowMissing */) !== 0 && isObjectLiteralType(objectType))
|
|
597
|
+
return c.undefinedType;
|
|
598
|
+
if (isJSLiteralType(c, objectType))
|
|
599
|
+
return c.anyType;
|
|
600
|
+
if (accessNode !== undefined) {
|
|
601
|
+
const indexNode = getIndexNodeForAccessExpression(accessNode);
|
|
602
|
+
if (indexNode.kind !== 9 /* SyntaxKind.BigIntLiteral */ &&
|
|
603
|
+
(indexType.flags & (1024 /* TypeFlags.StringLiteral */ | 2048 /* TypeFlags.NumberLiteral */)) !== 0)
|
|
604
|
+
c.error(indexNode, 516 /* Diagnostics.Property_0_does_not_exist_on_type_1 */, getPropertyNameFromType(indexType), typeToString(c, objectType));
|
|
605
|
+
else if ((indexType.flags & (32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */)) !== 0)
|
|
606
|
+
c.error(indexNode, 694 /* Diagnostics.Type_0_has_no_matching_index_signature_for_type_1 */, typeToString(c, objectType), typeToString(c, indexType));
|
|
607
|
+
else
|
|
608
|
+
c.error(indexNode, 695 /* Diagnostics.Type_0_cannot_be_used_as_an_index_type */, indexNode.kind === 9 /* SyntaxKind.BigIntLiteral */ ? 'bigint' : typeToString(c, indexType));
|
|
609
|
+
}
|
|
610
|
+
if ((indexType.flags & 1 /* TypeFlags.Any */) !== 0)
|
|
611
|
+
return indexType;
|
|
612
|
+
return undefined;
|
|
613
|
+
}
|
|
614
|
+
function errorIfWritingToReadonlyIndex(c, indexInfo, objectType, accessExpression) {
|
|
615
|
+
if (indexInfo !== undefined &&
|
|
616
|
+
indexInfo.isReadonly &&
|
|
617
|
+
accessExpression !== undefined &&
|
|
618
|
+
(isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression)))
|
|
619
|
+
c.error(accessExpression, 698 /* Diagnostics.Index_signature_in_type_0_only_permits_reading */, typeToString(c, objectType));
|
|
620
|
+
}
|
|
621
|
+
/** `obj.get` or `obj.set` for keyed access to a type with such methods instead of an index signature. */
|
|
622
|
+
function getSuggestionForNonexistentIndexSignature(c, objectType, expr, keyedType) {
|
|
623
|
+
const suggestedMethod = isAssignmentTarget(expr) ? 'set' : 'get';
|
|
624
|
+
const prop = getPropertyOfObjectType(c, objectType, suggestedMethod);
|
|
625
|
+
if (prop === undefined)
|
|
626
|
+
return '';
|
|
627
|
+
const signature = getSingleCallSignature(c, getTypeOfSymbol(c, prop));
|
|
628
|
+
if (signature === undefined ||
|
|
629
|
+
getMinArgumentCount(c, signature) < 1 ||
|
|
630
|
+
!isTypeAssignableTo(c, keyedType, getTypeAtPosition(c, signature, 0)))
|
|
631
|
+
return '';
|
|
632
|
+
const target = getExpression(expr);
|
|
633
|
+
if (target.kind === 79 /* SyntaxKind.Identifier */)
|
|
634
|
+
return getText(target) + '.' + suggestedMethod;
|
|
635
|
+
if (target.kind === 212 /* SyntaxKind.PropertyAccessExpression */ && isEntityNameExpression(target, false))
|
|
636
|
+
return getTextOfNodeFromSourceText(getSourceFileOfNode(target).text, target) + '.' + suggestedMethod;
|
|
637
|
+
return suggestedMethod;
|
|
638
|
+
}
|
|
639
|
+
function isObjectLiteralType(t) {
|
|
640
|
+
return (t.objectFlags & 128 /* ObjectFlags.ObjectLiteral */) !== 0;
|
|
641
|
+
}
|
|
642
|
+
function isConstEnumObjectType(t) {
|
|
643
|
+
return ((t.objectFlags & 16 /* ObjectFlags.Anonymous */) !== 0 &&
|
|
644
|
+
t.symbol !== undefined &&
|
|
645
|
+
(t.symbol.flags & 128 /* SymbolFlags.ConstEnum */) !== 0);
|
|
646
|
+
}
|
|
647
|
+
function containsMissing(c, t) {
|
|
648
|
+
return t === c.missingType || ((t.flags & 134217728 /* TypeFlags.Union */) !== 0 && typesOf(t)[0] === c.missingType);
|
|
649
|
+
}
|
|
650
|
+
function getIndexNodeForAccessExpression(accessNode) {
|
|
651
|
+
if (accessNode.kind === 213 /* SyntaxKind.ElementAccessExpression */)
|
|
652
|
+
return accessNode.argumentExpression;
|
|
653
|
+
if (accessNode.kind === 200 /* SyntaxKind.IndexedAccessType */)
|
|
654
|
+
return accessNode.indexType;
|
|
655
|
+
if (accessNode.kind === 168 /* SyntaxKind.ComputedPropertyName */)
|
|
656
|
+
return getExpression(accessNode);
|
|
657
|
+
return accessNode;
|
|
658
|
+
}
|
|
659
|
+
export function getTupleElementTypeOutOfStartCount(c, t, index, undefinedLikeType) {
|
|
660
|
+
return mapType(c, t, (u) => {
|
|
661
|
+
const restType = getRestTypeOfTupleType(c, u);
|
|
662
|
+
if (restType === undefined)
|
|
663
|
+
return c.undefinedType;
|
|
664
|
+
if (undefinedLikeType !== undefined && index >= getTotalFixedElementCount(tupleTargetOf(u)))
|
|
665
|
+
return getUnionType(c, [restType, undefinedLikeType]);
|
|
666
|
+
return restType;
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
export function getRestTypeOfTupleType(c, t) {
|
|
670
|
+
return getElementTypeOfSliceOfTupleType(c, t, tupleTargetOf(t).fixedLength, 0, false, false);
|
|
671
|
+
}
|
|
672
|
+
export function getElementTypeOfSliceOfTupleType(c, t, index, endSkipCount, writing, noReductions) {
|
|
673
|
+
const length = tupleTargetOf(t).typeParameterCount() - endSkipCount;
|
|
674
|
+
const infos = tupleTargetOf(t).elementInfos;
|
|
675
|
+
if (index >= length)
|
|
676
|
+
return undefined;
|
|
677
|
+
const typeArguments = getTypeArguments(c, t);
|
|
678
|
+
const elementTypes = [];
|
|
679
|
+
for (let i = index; i < length; i++) {
|
|
680
|
+
const e = typeArguments[i];
|
|
681
|
+
elementTypes.push((infos[i].flags & 8 /* ElementFlags.Variadic */) !== 0 ? getIndexedAccessType(c, e, c.numberType) : e);
|
|
682
|
+
}
|
|
683
|
+
if (writing)
|
|
684
|
+
return getIntersectionType(c, elementTypes);
|
|
685
|
+
return getUnionTypeEx(c, elementTypes, noReductions ? 0 /* UnionReduction.None */ : 1 /* UnionReduction.Literal */, undefined, undefined);
|
|
686
|
+
}
|
|
687
|
+
export function getConstraintOfIndexedAccess(c, t) {
|
|
688
|
+
return hasNonCircularBaseConstraint(c, t) ? getConstraintFromIndexedAccess(c, t) : undefined;
|
|
689
|
+
}
|
|
690
|
+
function getConstraintFromIndexedAccess(c, t) {
|
|
691
|
+
if (isMappedTypeGenericIndexedAccess(c, t))
|
|
692
|
+
return substituteIndexedMappedType(c, t.objectType, t.indexType);
|
|
693
|
+
const indexConstraint = getSimplifiedTypeOrConstraint(c, t.indexType);
|
|
694
|
+
if (indexConstraint !== undefined && indexConstraint !== t.indexType) {
|
|
695
|
+
const indexed = getIndexedAccessTypeOrUndefined(c, t.objectType, indexConstraint, t.accessFlags);
|
|
696
|
+
if (indexed !== undefined)
|
|
697
|
+
return indexed;
|
|
698
|
+
}
|
|
699
|
+
const objectConstraint = getSimplifiedTypeOrConstraint(c, t.objectType);
|
|
700
|
+
if (objectConstraint !== undefined && objectConstraint !== t.objectType)
|
|
701
|
+
return getIndexedAccessTypeOrUndefined(c, objectConstraint, t.indexType, t.accessFlags);
|
|
702
|
+
return undefined;
|
|
703
|
+
}
|
|
704
|
+
function getSimplifiedTypeOrConstraint(c, t) {
|
|
705
|
+
const simplified = getSimplifiedType(c, t, false);
|
|
706
|
+
return simplified !== t ? simplified : getConstraintOfTypeLocal(c, t);
|
|
707
|
+
}
|
|
708
|
+
function getConstraintOfTypeLocal(c, t) {
|
|
709
|
+
if ((t.flags & 524288 /* TypeFlags.TypeParameter */) !== 0)
|
|
710
|
+
return getConstraintOfTypeParameter(c, t);
|
|
711
|
+
if ((t.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0)
|
|
712
|
+
return getConstraintOfIndexedAccess(c, t);
|
|
713
|
+
if ((t.flags & 67108864 /* TypeFlags.Conditional */) !== 0)
|
|
714
|
+
return getConstraintOfConditionalType(c, t);
|
|
715
|
+
return getBaseConstraintOfType(c, t);
|
|
716
|
+
}
|
|
717
|
+
export function isMappedTypeGenericIndexedAccess(c, t) {
|
|
718
|
+
if ((t.flags & 33554432 /* TypeFlags.IndexedAccess */) === 0)
|
|
719
|
+
return false;
|
|
720
|
+
const objectType = t.objectType;
|
|
721
|
+
return ((objectType.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0 &&
|
|
722
|
+
!isGenericMappedType(c, objectType) &&
|
|
723
|
+
isGenericIndexType(c, t.indexType) &&
|
|
724
|
+
(getMappedTypeModifiers(objectType) & 8 /* MappedTypeModifiers.ExcludeOptional */) === 0 &&
|
|
725
|
+
mappedNameTypeNode(objectType) === undefined);
|
|
726
|
+
}
|
|
727
|
+
/** For `{ [P in K]: E }[X]`, E with P replaced by X. */
|
|
728
|
+
export function substituteIndexedMappedType(c, objectType, index) {
|
|
729
|
+
const mapper = newTypeMapper([getTypeParameterFromMappedType(c, objectType)], [index]);
|
|
730
|
+
const templateMapper = combineTypeMappers(c, objectType.mapper, mapper);
|
|
731
|
+
const instantiatedTemplateType = instantiateType(c, getTemplateTypeFromMappedType(c, mappedTypeTarget(objectType)), templateMapper);
|
|
732
|
+
let isOptional = getMappedTypeOptionality(objectType) > 0;
|
|
733
|
+
if (!isOptional)
|
|
734
|
+
isOptional = isGenericType(c, objectType)
|
|
735
|
+
? getCombinedMappedTypeOptionality(c, getModifiersTypeFromMappedType(c, objectType)) > 0
|
|
736
|
+
: couldAccessOptionalProperty(c, objectType, index);
|
|
737
|
+
return addOptionality(c, instantiatedTemplateType, true, isOptional);
|
|
738
|
+
}
|
|
739
|
+
/** The optionality modifiers of a mapped type or its modifiers type, or of all the types of an intersection. */
|
|
740
|
+
export function getCombinedMappedTypeOptionality(c, t) {
|
|
741
|
+
if ((t.objectFlags & 32 /* ObjectFlags.Mapped */) !== 0) {
|
|
742
|
+
const optionality = getMappedTypeOptionality(t);
|
|
743
|
+
return optionality !== 0
|
|
744
|
+
? optionality
|
|
745
|
+
: getCombinedMappedTypeOptionality(c, getModifiersTypeFromMappedType(c, t));
|
|
746
|
+
}
|
|
747
|
+
if ((t.flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
|
|
748
|
+
const types = typesOf(t);
|
|
749
|
+
const optionality = getCombinedMappedTypeOptionality(c, types[0]);
|
|
750
|
+
for (let i = 1; i < types.length; i++)
|
|
751
|
+
if (getCombinedMappedTypeOptionality(c, types[i]) !== optionality)
|
|
752
|
+
return 0;
|
|
753
|
+
return optionality;
|
|
754
|
+
}
|
|
755
|
+
return 0;
|
|
756
|
+
}
|
|
757
|
+
/** Whether an indexed access with the given object and index types could access an optional property. */
|
|
758
|
+
function couldAccessOptionalProperty(c, objectType, indexType) {
|
|
759
|
+
const indexConstraint = getBaseConstraintOfType(c, indexType);
|
|
760
|
+
if (indexConstraint === undefined)
|
|
761
|
+
return false;
|
|
762
|
+
for (const p of getPropertiesOfType(c, objectType))
|
|
763
|
+
if ((p.flags & 16777216 /* SymbolFlags.Optional */) !== 0 &&
|
|
764
|
+
isTypeAssignableTo(c, getLiteralTypeFromProperty(c, p, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, false), indexConstraint))
|
|
765
|
+
return true;
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
// ---- Simplification
|
|
769
|
+
export function getSimplifiedType(c, t, writing) {
|
|
770
|
+
if ((t.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0)
|
|
771
|
+
return getSimplifiedIndexedAccessType(c, t, writing);
|
|
772
|
+
if ((t.flags & 67108864 /* TypeFlags.Conditional */) !== 0)
|
|
773
|
+
return getSimplifiedConditionalType(c, t, writing);
|
|
774
|
+
return t;
|
|
775
|
+
}
|
|
776
|
+
/** Simplifies `T extends U ? T : never` and `T extends U ? never : T` when the outcome is known. */
|
|
777
|
+
function getSimplifiedConditionalType(c, t, writing) {
|
|
778
|
+
const checkType = t.checkType;
|
|
779
|
+
const extendsType = t.extendsType;
|
|
780
|
+
const trueType = getTrueTypeFromConditionalType(c, t);
|
|
781
|
+
const falseType = getFalseTypeFromConditionalType(c, t);
|
|
782
|
+
if ((falseType.flags & 262144 /* TypeFlags.Never */) !== 0 &&
|
|
783
|
+
getActualTypeVariable(c, trueType) === getActualTypeVariable(c, checkType)) {
|
|
784
|
+
if ((checkType.flags & 1 /* TypeFlags.Any */) !== 0 ||
|
|
785
|
+
isTypeAssignableTo(c, getRestrictiveInstantiation(c, checkType), getRestrictiveInstantiation(c, extendsType)))
|
|
786
|
+
return getSimplifiedType(c, trueType, writing);
|
|
787
|
+
if (isIntersectionEmpty(c, checkType, extendsType))
|
|
788
|
+
return c.neverType;
|
|
789
|
+
}
|
|
790
|
+
else if ((trueType.flags & 262144 /* TypeFlags.Never */) !== 0 &&
|
|
791
|
+
getActualTypeVariable(c, falseType) === getActualTypeVariable(c, checkType)) {
|
|
792
|
+
if ((checkType.flags & 1 /* TypeFlags.Any */) === 0 &&
|
|
793
|
+
isTypeAssignableTo(c, getRestrictiveInstantiation(c, checkType), getRestrictiveInstantiation(c, extendsType)))
|
|
794
|
+
return c.neverType;
|
|
795
|
+
if ((checkType.flags & 1 /* TypeFlags.Any */) !== 0 || isIntersectionEmpty(c, checkType, extendsType))
|
|
796
|
+
return getSimplifiedType(c, falseType, writing);
|
|
797
|
+
}
|
|
798
|
+
return t;
|
|
799
|
+
}
|
|
800
|
+
/** Whether the intersection of two types reduces to `never` as a union constituent. */
|
|
801
|
+
function isIntersectionEmpty(c, type1, type2) {
|
|
802
|
+
return (getUnionType(c, [intersectTypes(c, type1, type2), c.neverType]).flags & 262144 /* TypeFlags.Never */) !== 0;
|
|
803
|
+
}
|
|
804
|
+
function getSimplifiedIndexedAccessType(c, t, writing) {
|
|
805
|
+
const key = cachedTypeKey(writing ? 12 /* CachedTypeKind.IndexedAccessForWriting */ : 11 /* CachedTypeKind.IndexedAccessForReading */, t.id);
|
|
806
|
+
const cached = c.cachedTypesById.get(key);
|
|
807
|
+
if (cached !== undefined)
|
|
808
|
+
return cached === c.circularConstraintType ? t : cached;
|
|
809
|
+
const self = t;
|
|
810
|
+
c.cachedTypesById.set(key, self);
|
|
811
|
+
const objectType = getSimplifiedType(c, t.objectType, writing);
|
|
812
|
+
const indexType = getSimplifiedType(c, t.indexType, writing);
|
|
813
|
+
let result = t;
|
|
814
|
+
if ((indexType.flags & 134217728 /* TypeFlags.Union */) !== 0) {
|
|
815
|
+
// T[A | B] -> T[A] | T[B] (reading), T[A] & T[B] (writing).
|
|
816
|
+
const types = [];
|
|
817
|
+
for (const u of typesOf(indexType))
|
|
818
|
+
types.push(getSimplifiedType(c, getIndexedAccessType(c, objectType, u), writing));
|
|
819
|
+
result = writing ? getIntersectionType(c, types) : getUnionType(c, types);
|
|
820
|
+
}
|
|
821
|
+
else if (
|
|
822
|
+
// Inner distributions only once the index can't be instantiated to distribute again:
|
|
823
|
+
// (T | U)[K] -> T[K] | U[K] (reading), T[K] & U[K] (writing); (T & U)[K] -> T[K] & U[K].
|
|
824
|
+
(indexType.flags & 132644864 /* TypeFlags.Instantiable */) === 0 &&
|
|
825
|
+
((objectType.flags & 134217728 /* TypeFlags.Union */) !== 0 ||
|
|
826
|
+
((objectType.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && !shouldDeferIndexType(c, objectType, 0 /* IndexFlags.None */)))) {
|
|
827
|
+
const types = [];
|
|
828
|
+
for (const u of typesOf(objectType))
|
|
829
|
+
types.push(getSimplifiedType(c, getIndexedAccessType(c, u, indexType), writing));
|
|
830
|
+
result =
|
|
831
|
+
(objectType.flags & 268435456 /* TypeFlags.Intersection */) !== 0 || writing
|
|
832
|
+
? getIntersectionType(c, types)
|
|
833
|
+
: getUnionType(c, types);
|
|
834
|
+
}
|
|
835
|
+
else if (isGenericTupleType(objectType) && (indexType.flags & 67648 /* TypeFlags.NumberLike */) !== 0) {
|
|
836
|
+
const elementType = getElementTypeOfSliceOfTupleType(c, objectType, (indexType.flags & 64 /* TypeFlags.Number */) !== 0 ? 0 : tupleTargetOf(objectType).fixedLength, 0, writing, false);
|
|
837
|
+
if (elementType !== undefined)
|
|
838
|
+
result = elementType;
|
|
839
|
+
}
|
|
840
|
+
else if (isGenericMappedType(c, objectType)) {
|
|
841
|
+
// { [P in K]: E }[X] -> E with X for P, unless the mapped type remaps its keys.
|
|
842
|
+
if (getMappedTypeNameTypeKind(c, objectType) !== 2 /* MappedTypeNameTypeKind.Remapping */)
|
|
843
|
+
result = mapType(c, substituteIndexedMappedType(c, objectType, t.indexType), (u) => getSimplifiedType(c, u, writing));
|
|
844
|
+
}
|
|
845
|
+
if (result.id !== t.id) {
|
|
846
|
+
const self = t;
|
|
847
|
+
result = filterType(c, result, (u) => u.id !== self.id);
|
|
848
|
+
c.cachedTypesById.set(key, result);
|
|
849
|
+
}
|
|
850
|
+
return result;
|
|
851
|
+
}
|
|
852
|
+
// ---- Substitution and NoInfer
|
|
853
|
+
export function isNoInferType(c, t) {
|
|
854
|
+
// NoInfer<T> is a substitution type with an `unknown` constraint.
|
|
855
|
+
return ((t.flags & 16777216 /* TypeFlags.Substitution */) !== 0 && (t.constraint.flags & 2 /* TypeFlags.Unknown */) !== 0);
|
|
856
|
+
}
|
|
857
|
+
export function getNoInferType(c, t) {
|
|
858
|
+
return isNoInferTargetType(c, t) ? getOrCreateSubstitutionType(c, t, c.unknownType) : t;
|
|
859
|
+
}
|
|
860
|
+
function isNoInferTargetType(c, t) {
|
|
861
|
+
if ((t.flags & 402653184 /* TypeFlags.UnionOrIntersection */) !== 0) {
|
|
862
|
+
for (const u of typesOf(t))
|
|
863
|
+
if (isNoInferTargetType(c, u))
|
|
864
|
+
return true;
|
|
865
|
+
return false;
|
|
866
|
+
}
|
|
867
|
+
if ((t.flags & 16777216 /* TypeFlags.Substitution */) !== 0)
|
|
868
|
+
return !isNoInferType(c, t) && isNoInferTargetType(c, t.baseType);
|
|
869
|
+
if ((t.flags & 1048576 /* TypeFlags.Object */) !== 0)
|
|
870
|
+
return !isEmptyAnonymousObjectType(c, t);
|
|
871
|
+
return (t.flags & (132644864 /* TypeFlags.Instantiable */ & ~16777216 /* TypeFlags.Substitution */)) !== 0 && !isPatternLiteralType(c, t);
|
|
872
|
+
}
|
|
873
|
+
export function getSubstitutionType(c, baseType, constraint) {
|
|
874
|
+
if ((constraint.flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0 ||
|
|
875
|
+
constraint === baseType ||
|
|
876
|
+
(baseType.flags & 1 /* TypeFlags.Any */) !== 0)
|
|
877
|
+
return baseType;
|
|
878
|
+
return getOrCreateSubstitutionType(c, baseType, constraint);
|
|
879
|
+
}
|
|
880
|
+
function getOrCreateSubstitutionType(c, baseType, constraint) {
|
|
881
|
+
const key = baseType.id + '>' + constraint.id;
|
|
882
|
+
let t = c.substitutionTypes.get(key);
|
|
883
|
+
if (t === undefined) {
|
|
884
|
+
t = newSubstitutionType(c, baseType, constraint);
|
|
885
|
+
c.substitutionTypes.set(key, t);
|
|
886
|
+
}
|
|
887
|
+
return t;
|
|
888
|
+
}
|
|
889
|
+
export function getSubstitutionIntersection(c, t) {
|
|
890
|
+
const s = t;
|
|
891
|
+
return isNoInferType(c, t) ? s.baseType : getIntersectionType(c, [s.constraint, s.baseType]);
|
|
892
|
+
}
|
|
893
|
+
export function intrinsicTypeKind(name) {
|
|
894
|
+
switch (name) {
|
|
895
|
+
case 'Uppercase':
|
|
896
|
+
return 1 /* IntrinsicTypeKind.Uppercase */;
|
|
897
|
+
case 'Lowercase':
|
|
898
|
+
return 2 /* IntrinsicTypeKind.Lowercase */;
|
|
899
|
+
case 'Capitalize':
|
|
900
|
+
return 3 /* IntrinsicTypeKind.Capitalize */;
|
|
901
|
+
case 'Uncapitalize':
|
|
902
|
+
return 4 /* IntrinsicTypeKind.Uncapitalize */;
|
|
903
|
+
case 'NoInfer':
|
|
904
|
+
return 5 /* IntrinsicTypeKind.NoInfer */;
|
|
905
|
+
}
|
|
906
|
+
return 0 /* IntrinsicTypeKind.Unknown */;
|
|
907
|
+
}
|
|
908
|
+
export function applyStringMapping(symbol, s) {
|
|
909
|
+
switch (intrinsicTypeKind(symbol.name)) {
|
|
910
|
+
case 1 /* IntrinsicTypeKind.Uppercase */:
|
|
911
|
+
return s.toUpperCase();
|
|
912
|
+
case 2 /* IntrinsicTypeKind.Lowercase */:
|
|
913
|
+
return s.toLowerCase();
|
|
914
|
+
case 3 /* IntrinsicTypeKind.Capitalize */: {
|
|
915
|
+
const size = s.length > 1 && s.charCodeAt(0) >= 0xd800 && s.charCodeAt(0) <= 0xdbff ? 2 : 1;
|
|
916
|
+
return s.substring(0, size).toUpperCase() + s.substring(size);
|
|
917
|
+
}
|
|
918
|
+
case 4 /* IntrinsicTypeKind.Uncapitalize */: {
|
|
919
|
+
const size = s.length > 1 && s.charCodeAt(0) >= 0xd800 && s.charCodeAt(0) <= 0xdbff ? 2 : 1;
|
|
920
|
+
return s.substring(0, size).toLowerCase() + s.substring(size);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
return s;
|
|
924
|
+
}
|
|
925
|
+
export function getStringMappingType(c, symbol, t) {
|
|
926
|
+
if ((t.flags & (134217728 /* TypeFlags.Union */ | 262144 /* TypeFlags.Never */)) !== 0)
|
|
927
|
+
return mapType(c, t, (u) => getStringMappingType(c, symbol, u));
|
|
928
|
+
if ((t.flags & 1024 /* TypeFlags.StringLiteral */) !== 0)
|
|
929
|
+
return getStringLiteralType(c, applyStringMapping(symbol, t.stringValue));
|
|
930
|
+
if ((t.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
|
|
931
|
+
const template = t;
|
|
932
|
+
const [texts, types] = applyTemplateStringMapping(c, symbol, template.texts, template.types);
|
|
933
|
+
return getTemplateLiteralType(c, texts, types);
|
|
934
|
+
}
|
|
935
|
+
if ((t.flags & 8388608 /* TypeFlags.StringMapping */) !== 0 && symbol === t.symbol)
|
|
936
|
+
return t;
|
|
937
|
+
if ((t.flags & (1 /* TypeFlags.Any */ | 32 /* TypeFlags.String */ | 8388608 /* TypeFlags.StringMapping */)) !== 0 || isGenericIndexType(c, t))
|
|
938
|
+
return getStringMappingTypeForGenericType(c, symbol, t);
|
|
939
|
+
if (isPatternLiteralPlaceholder(c, t))
|
|
940
|
+
return getStringMappingTypeForGenericType(c, symbol, getTemplateLiteralType(c, ['', ''], [t]));
|
|
941
|
+
return t;
|
|
942
|
+
}
|
|
943
|
+
function isPatternLiteralPlaceholder(c, t) {
|
|
944
|
+
return ((t.flags & (1 /* TypeFlags.Any */ | 32 /* TypeFlags.String */ | 64 /* TypeFlags.Number */ | 128 /* TypeFlags.BigInt */)) !== 0 ||
|
|
945
|
+
isPatternLiteralType(c, t));
|
|
946
|
+
}
|
|
947
|
+
function applyTemplateStringMapping(c, symbol, texts, types) {
|
|
948
|
+
const kind = intrinsicTypeKind(symbol.name);
|
|
949
|
+
if (kind === 1 /* IntrinsicTypeKind.Uppercase */ || kind === 2 /* IntrinsicTypeKind.Lowercase */) {
|
|
950
|
+
const mappedTexts = [];
|
|
951
|
+
for (const t of texts)
|
|
952
|
+
mappedTexts.push(applyStringMapping(symbol, t));
|
|
953
|
+
const mappedTypes = [];
|
|
954
|
+
for (const t of types)
|
|
955
|
+
mappedTypes.push(getStringMappingType(c, symbol, t));
|
|
956
|
+
return [mappedTexts, mappedTypes];
|
|
957
|
+
}
|
|
958
|
+
if (kind === 3 /* IntrinsicTypeKind.Capitalize */ || kind === 4 /* IntrinsicTypeKind.Uncapitalize */) {
|
|
959
|
+
if (texts[0] === '')
|
|
960
|
+
return [texts, [getStringMappingType(c, symbol, types[0])].concat(types.slice(1))];
|
|
961
|
+
return [[applyStringMapping(symbol, texts[0])].concat(texts.slice(1)), types];
|
|
962
|
+
}
|
|
963
|
+
return [texts, types];
|
|
964
|
+
}
|
|
965
|
+
function getStringMappingTypeForGenericType(c, symbol, t) {
|
|
966
|
+
const key = getSymbolId(symbol) + ',' + t.id;
|
|
967
|
+
let result = c.stringMappingTypes.get(key);
|
|
968
|
+
if (result === undefined) {
|
|
969
|
+
result = new StringMappingType(c.nextTypeId(), symbol, t);
|
|
970
|
+
c.stringMappingTypes.set(key, result);
|
|
971
|
+
}
|
|
972
|
+
return result;
|
|
973
|
+
}
|
|
974
|
+
function mappedDeclaration(t) {
|
|
975
|
+
return t.declaration;
|
|
976
|
+
}
|
|
977
|
+
function mappedNameTypeNode(t) {
|
|
978
|
+
return mappedDeclaration(t).nameType;
|
|
979
|
+
}
|
|
980
|
+
export function getMappedTypeModifiers(t) {
|
|
981
|
+
const declaration = mappedDeclaration(t);
|
|
982
|
+
let modifiers = 0;
|
|
983
|
+
if (declaration.readonlyToken !== undefined)
|
|
984
|
+
modifiers |=
|
|
985
|
+
declaration.readonlyToken.kind === 40 /* SyntaxKind.MinusToken */
|
|
986
|
+
? 2 /* MappedTypeModifiers.ExcludeReadonly */
|
|
987
|
+
: 1 /* MappedTypeModifiers.IncludeReadonly */;
|
|
988
|
+
if (declaration.questionToken !== undefined)
|
|
989
|
+
modifiers |=
|
|
990
|
+
declaration.questionToken.kind === 40 /* SyntaxKind.MinusToken */
|
|
991
|
+
? 8 /* MappedTypeModifiers.ExcludeOptional */
|
|
992
|
+
: 4 /* MappedTypeModifiers.IncludeOptional */;
|
|
993
|
+
return modifiers;
|
|
994
|
+
}
|
|
995
|
+
export function getMappedTypeOptionality(t) {
|
|
996
|
+
const modifiers = getMappedTypeModifiers(t);
|
|
997
|
+
if ((modifiers & 8 /* MappedTypeModifiers.ExcludeOptional */) !== 0)
|
|
998
|
+
return -1;
|
|
999
|
+
if ((modifiers & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0)
|
|
1000
|
+
return 1;
|
|
1001
|
+
return 0;
|
|
1002
|
+
}
|
|
1003
|
+
export function getTypeFromMappedTypeNode(c, node) {
|
|
1004
|
+
if (c.resolvedTypes.get(node.id) === undefined) {
|
|
1005
|
+
const t = newObjectType(c, 32 /* ObjectFlags.Mapped */, getSymbol(node));
|
|
1006
|
+
t.declaration = node;
|
|
1007
|
+
t.alias = getAliasForTypeNode(c, node);
|
|
1008
|
+
c.resolvedTypes.set(node.id, t);
|
|
1009
|
+
// Resolve the constraint eagerly to report circular aliases.
|
|
1010
|
+
getConstraintTypeFromMappedType(c, t);
|
|
1011
|
+
}
|
|
1012
|
+
return c.resolvedTypes.get(node.id);
|
|
1013
|
+
}
|
|
1014
|
+
export function getTypeParameterFromMappedType(c, t) {
|
|
1015
|
+
// Instantiations have it from creation; a declared mapped type gets it on first use.
|
|
1016
|
+
if (t.typeParameter !== undefined)
|
|
1017
|
+
return t.typeParameter;
|
|
1018
|
+
const side = sideOf(c, t);
|
|
1019
|
+
if (side.typeParameter === undefined)
|
|
1020
|
+
side.typeParameter = getDeclaredTypeOfTypeParameter(c, getSymbolOfDeclaration(c, mappedDeclaration(t).typeParameter));
|
|
1021
|
+
return side.typeParameter;
|
|
1022
|
+
}
|
|
1023
|
+
export function getConstraintTypeFromMappedType(c, t) {
|
|
1024
|
+
const side = sideOf(c, t);
|
|
1025
|
+
if (side.constraintType === undefined)
|
|
1026
|
+
side.constraintType = getConstraintOfTypeParameter(c, getTypeParameterFromMappedType(c, t)) ?? c.errorType;
|
|
1027
|
+
return side.constraintType;
|
|
1028
|
+
}
|
|
1029
|
+
export function getNameTypeFromMappedType(c, t) {
|
|
1030
|
+
const side = sideOf(c, t);
|
|
1031
|
+
const node = mappedNameTypeNode(t);
|
|
1032
|
+
if (node === undefined)
|
|
1033
|
+
return undefined;
|
|
1034
|
+
if (side.nameType === undefined)
|
|
1035
|
+
side.nameType = instantiateType(c, getTypeFromTypeNode(c, node), t.mapper);
|
|
1036
|
+
return side.nameType;
|
|
1037
|
+
}
|
|
1038
|
+
export function getTemplateTypeFromMappedType(c, t) {
|
|
1039
|
+
const side = sideOf(c, t);
|
|
1040
|
+
if (side.templateType === undefined) {
|
|
1041
|
+
const typeNode = mappedDeclaration(t).type;
|
|
1042
|
+
side.templateType =
|
|
1043
|
+
typeNode !== undefined
|
|
1044
|
+
? instantiateType(c, addOptionality(c, getTypeFromTypeNode(c, typeNode), true, (getMappedTypeModifiers(t) & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0), t.mapper)
|
|
1045
|
+
: c.errorType;
|
|
1046
|
+
}
|
|
1047
|
+
return side.templateType;
|
|
1048
|
+
}
|
|
1049
|
+
function getConstraintDeclarationForMappedType(t) {
|
|
1050
|
+
return mappedDeclaration(t).typeParameter.constraint;
|
|
1051
|
+
}
|
|
1052
|
+
export function isMappedTypeWithKeyofConstraintDeclaration(c, t) {
|
|
1053
|
+
const constraint = getConstraintDeclarationForMappedType(t);
|
|
1054
|
+
return (constraint !== undefined &&
|
|
1055
|
+
constraint.kind === 199 /* SyntaxKind.TypeOperator */ &&
|
|
1056
|
+
constraint.operator === 143 /* SyntaxKind.KeyOfKeyword */);
|
|
1057
|
+
}
|
|
1058
|
+
export function getModifiersTypeFromMappedType(c, t) {
|
|
1059
|
+
const side = sideOf(c, t);
|
|
1060
|
+
if (side.modifiersType === undefined) {
|
|
1061
|
+
if (isMappedTypeWithKeyofConstraintDeclaration(c, t)) {
|
|
1062
|
+
// For `keyof T`, the modifiers type is T, read from the node since `keyof` of a concrete T resolves eagerly.
|
|
1063
|
+
side.modifiersType = instantiateType(c, getTypeFromTypeNode(c, getType(getConstraintDeclarationForMappedType(t))), t.mapper);
|
|
1064
|
+
}
|
|
1065
|
+
else {
|
|
1066
|
+
const declaredType = getTypeFromMappedTypeNode(c, t.declaration);
|
|
1067
|
+
const constraint = getConstraintTypeFromMappedType(c, declaredType);
|
|
1068
|
+
const extended = (constraint.flags & 524288 /* TypeFlags.TypeParameter */) !== 0 ? getConstraintOfTypeParameter(c, constraint) : constraint;
|
|
1069
|
+
side.modifiersType =
|
|
1070
|
+
extended !== undefined && (extended.flags & 2097152 /* TypeFlags.Index */) !== 0
|
|
1071
|
+
? instantiateType(c, extended.target, t.mapper)
|
|
1072
|
+
: c.unknownType;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
return side.modifiersType;
|
|
1076
|
+
}
|
|
1077
|
+
export function getHomomorphicTypeVariable(c, t) {
|
|
1078
|
+
const constraintType = getConstraintTypeFromMappedType(c, t);
|
|
1079
|
+
if ((constraintType.flags & 2097152 /* TypeFlags.Index */) !== 0) {
|
|
1080
|
+
const typeVariable = getActualTypeVariable(c, constraintType.target);
|
|
1081
|
+
if ((typeVariable.flags & 524288 /* TypeFlags.TypeParameter */) !== 0)
|
|
1082
|
+
return typeVariable;
|
|
1083
|
+
}
|
|
1084
|
+
return undefined;
|
|
1085
|
+
}
|
|
1086
|
+
export function getActualTypeVariable(c, t) {
|
|
1087
|
+
if ((t.flags & 16777216 /* TypeFlags.Substitution */) !== 0)
|
|
1088
|
+
return getActualTypeVariable(c, t.baseType);
|
|
1089
|
+
if ((t.flags & 33554432 /* TypeFlags.IndexedAccess */) !== 0) {
|
|
1090
|
+
const access = t;
|
|
1091
|
+
if ((access.objectType.flags & 16777216 /* TypeFlags.Substitution */) !== 0 ||
|
|
1092
|
+
(access.indexType.flags & 16777216 /* TypeFlags.Substitution */) !== 0)
|
|
1093
|
+
return getIndexedAccessType(c, getActualTypeVariable(c, access.objectType), getActualTypeVariable(c, access.indexType));
|
|
1094
|
+
}
|
|
1095
|
+
return t;
|
|
1096
|
+
}
|
|
1097
|
+
export function getMappedTypeNameTypeKind(c, t) {
|
|
1098
|
+
const nameType = getNameTypeFromMappedType(c, t);
|
|
1099
|
+
if (nameType === undefined)
|
|
1100
|
+
return 0 /* MappedTypeNameTypeKind.None */;
|
|
1101
|
+
return isTypeAssignableTo(c, nameType, getTypeParameterFromMappedType(c, t))
|
|
1102
|
+
? 1 /* MappedTypeNameTypeKind.Filtering */
|
|
1103
|
+
: 2 /* MappedTypeNameTypeKind.Remapping */;
|
|
1104
|
+
}
|
|
1105
|
+
export function resolveMappedTypeMembers(c, t) {
|
|
1106
|
+
const members = new Map();
|
|
1107
|
+
let indexInfos = [];
|
|
1108
|
+
// Recursive references see an empty object type.
|
|
1109
|
+
setStructuredTypeMembers(c, t, undefined, [], [], []);
|
|
1110
|
+
const typeParameter = getTypeParameterFromMappedType(c, t);
|
|
1111
|
+
const constraintType = getConstraintTypeFromMappedType(c, t);
|
|
1112
|
+
const mappedType = mappedTypeTarget(t);
|
|
1113
|
+
const nameType = getNameTypeFromMappedType(c, mappedType);
|
|
1114
|
+
const shouldLinkPropDeclarations = getMappedTypeNameTypeKind(c, mappedType) !== 2 /* MappedTypeNameTypeKind.Remapping */;
|
|
1115
|
+
const templateType = getTemplateTypeFromMappedType(c, mappedType);
|
|
1116
|
+
const modifiersType = getApparentType(c, getModifiersTypeFromMappedType(c, t));
|
|
1117
|
+
const templateModifiers = getMappedTypeModifiers(t);
|
|
1118
|
+
const addForPropName = (keyType, propNameType) => {
|
|
1119
|
+
if (isTypeUsableAsPropertyName(propNameType)) {
|
|
1120
|
+
const propName = getPropertyNameFromType(propNameType);
|
|
1121
|
+
const existing = members.get(propName);
|
|
1122
|
+
if (existing !== undefined) {
|
|
1123
|
+
// Same-valued string enum members from different enums share a property.
|
|
1124
|
+
const id = getSymbolId(existing);
|
|
1125
|
+
c.symbolNameTypes.set(id, getUnionType(c, [c.symbolNameTypes.get(id), propNameType]));
|
|
1126
|
+
c.symbolKeyTypes.set(id, getUnionType(c, [c.symbolKeyTypes.get(id), keyType]));
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
const modifiersProp = isTypeUsableAsPropertyName(keyType)
|
|
1130
|
+
? getPropertyOfType(c, modifiersType, getPropertyNameFromType(keyType))
|
|
1131
|
+
: undefined;
|
|
1132
|
+
const isOptional = (templateModifiers & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0 ||
|
|
1133
|
+
((templateModifiers & 8 /* MappedTypeModifiers.ExcludeOptional */) === 0 &&
|
|
1134
|
+
modifiersProp !== undefined &&
|
|
1135
|
+
(modifiersProp.flags & 16777216 /* SymbolFlags.Optional */) !== 0);
|
|
1136
|
+
const isReadonly = (templateModifiers & 1 /* MappedTypeModifiers.IncludeReadonly */) !== 0 ||
|
|
1137
|
+
((templateModifiers & 2 /* MappedTypeModifiers.ExcludeReadonly */) === 0 &&
|
|
1138
|
+
modifiersProp !== undefined &&
|
|
1139
|
+
isReadonlySymbol(c, modifiersProp));
|
|
1140
|
+
const stripOptional = c.strictNullChecks &&
|
|
1141
|
+
!isOptional &&
|
|
1142
|
+
modifiersProp !== undefined &&
|
|
1143
|
+
(modifiersProp.flags & 16777216 /* SymbolFlags.Optional */) !== 0;
|
|
1144
|
+
const lateFlag = modifiersProp !== undefined ? modifiersProp.checkFlags & 4096 /* CheckFlags.Late */ : 0;
|
|
1145
|
+
const prop = c.newSymbol(4 /* SymbolFlags.Property */ | (isOptional ? 16777216 /* SymbolFlags.Optional */ : 0), propName);
|
|
1146
|
+
prop.checkFlags =
|
|
1147
|
+
lateFlag |
|
|
1148
|
+
262144 /* CheckFlags.Mapped */ |
|
|
1149
|
+
(isReadonly ? 8 /* CheckFlags.Readonly */ : 0) |
|
|
1150
|
+
(stripOptional ? 524288 /* CheckFlags.StripOptional */ : 0);
|
|
1151
|
+
const symbolId = getSymbolId(prop);
|
|
1152
|
+
c.symbolContainingTypes.set(symbolId, t);
|
|
1153
|
+
c.symbolNameTypes.set(symbolId, propNameType);
|
|
1154
|
+
c.symbolKeyTypes.set(symbolId, keyType);
|
|
1155
|
+
if (modifiersProp !== undefined) {
|
|
1156
|
+
c.symbolSyntheticOrigins.set(symbolId, modifiersProp);
|
|
1157
|
+
if (shouldLinkPropDeclarations)
|
|
1158
|
+
prop.declarations = modifiersProp.declarations;
|
|
1159
|
+
}
|
|
1160
|
+
members.set(propName, prop);
|
|
1161
|
+
}
|
|
1162
|
+
else if (isValidIndexKeyType(c, propNameType) || (propNameType.flags & (1 /* TypeFlags.Any */ | 65536 /* TypeFlags.Enum */)) !== 0) {
|
|
1163
|
+
const indexKeyType = (propNameType.flags & (1 /* TypeFlags.Any */ | 32 /* TypeFlags.String */)) !== 0
|
|
1164
|
+
? c.stringType
|
|
1165
|
+
: (propNameType.flags & (64 /* TypeFlags.Number */ | 65536 /* TypeFlags.Enum */)) !== 0
|
|
1166
|
+
? c.numberType
|
|
1167
|
+
: propNameType;
|
|
1168
|
+
const propType = instantiateType(c, templateType, appendTypeMapping(t.mapper, typeParameter, keyType));
|
|
1169
|
+
const modifiersIndexInfo = getApplicableIndexInfo(c, modifiersType, propNameType);
|
|
1170
|
+
const isReadonly = (templateModifiers & 1 /* MappedTypeModifiers.IncludeReadonly */) !== 0 ||
|
|
1171
|
+
((templateModifiers & 2 /* MappedTypeModifiers.ExcludeReadonly */) === 0 &&
|
|
1172
|
+
modifiersIndexInfo !== undefined &&
|
|
1173
|
+
modifiersIndexInfo.isReadonly);
|
|
1174
|
+
indexInfos = appendIndexInfo(c, indexInfos, new IndexInfo(indexKeyType, propType, isReadonly, undefined, []), true);
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
const add = (keyType) => {
|
|
1178
|
+
const propNameType = nameType !== undefined
|
|
1179
|
+
? instantiateType(c, nameType, appendTypeMapping(t.mapper, typeParameter, keyType))
|
|
1180
|
+
: keyType;
|
|
1181
|
+
forEachType(propNameType, (u) => addForPropName(keyType, u));
|
|
1182
|
+
};
|
|
1183
|
+
if (isMappedTypeWithKeyofConstraintDeclaration(c, t))
|
|
1184
|
+
forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(c, modifiersType, 19456 /* TypeFlags.StringOrNumberLiteralOrUnique */, false, add);
|
|
1185
|
+
else
|
|
1186
|
+
forEachType(getLowerBoundOfKeyType(c, constraintType), add);
|
|
1187
|
+
setStructuredTypeMembers(c, t, members, [], [], indexInfos);
|
|
1188
|
+
keepPropertiesOnly(c, t, members);
|
|
1189
|
+
}
|
|
1190
|
+
export function getTypeOfMappedSymbol(c, symbol) {
|
|
1191
|
+
const id = getSymbolId(symbol);
|
|
1192
|
+
const cached = c.symbolTypes.get(id);
|
|
1193
|
+
if (cached !== undefined)
|
|
1194
|
+
return cached;
|
|
1195
|
+
const mappedType = c.symbolContainingTypes.get(id);
|
|
1196
|
+
if (!pushTypeResolution(c, symbol, 0 /* TypeSystemPropertyName.Type */)) {
|
|
1197
|
+
sideOf(c, mappedType).containsError = true;
|
|
1198
|
+
return c.errorType;
|
|
1199
|
+
}
|
|
1200
|
+
const templateType = getTemplateTypeFromMappedType(c, mappedTypeTarget(mappedType));
|
|
1201
|
+
const mapper = appendTypeMapping(mappedType.mapper, getTypeParameterFromMappedType(c, mappedType), c.symbolKeyTypes.get(id));
|
|
1202
|
+
let propType = instantiateType(c, templateType, mapper);
|
|
1203
|
+
// Optional properties get `undefined`; stripped optionality removes it.
|
|
1204
|
+
if (c.strictNullChecks &&
|
|
1205
|
+
(symbol.flags & 16777216 /* SymbolFlags.Optional */) !== 0 &&
|
|
1206
|
+
!maybeTypeOfKind(propType, 4 /* TypeFlags.Undefined */ | 16 /* TypeFlags.Void */))
|
|
1207
|
+
propType = getOptionalType(c, propType, true);
|
|
1208
|
+
else if ((symbol.checkFlags & 524288 /* CheckFlags.StripOptional */) !== 0)
|
|
1209
|
+
propType = removeMissingOrUndefinedType(c, propType);
|
|
1210
|
+
if (popTypeResolution(c)) {
|
|
1211
|
+
if (c.symbolTypes.get(id) === undefined)
|
|
1212
|
+
c.symbolTypes.set(id, propType);
|
|
1213
|
+
}
|
|
1214
|
+
else {
|
|
1215
|
+
if (c.symbolTypes.get(id) === undefined)
|
|
1216
|
+
c.symbolTypes.set(id, c.errorType);
|
|
1217
|
+
c.error(c.currentNode, 759 /* Diagnostics.Type_of_property_0_circularly_references_itself_in_mapped_type_1 */, symbolToString(c, symbol), typeToString(c, mappedType));
|
|
1218
|
+
}
|
|
1219
|
+
return c.symbolTypes.get(id);
|
|
1220
|
+
}
|
|
1221
|
+
function everyArrayOrTupleType(c, types) {
|
|
1222
|
+
for (const t of types)
|
|
1223
|
+
if (!isArrayType(c, t) && !isTupleType(t))
|
|
1224
|
+
return false;
|
|
1225
|
+
return true;
|
|
1226
|
+
}
|
|
1227
|
+
/** Instantiates homomorphic mapped type `t` for each constituent its type variable becomes. */
|
|
1228
|
+
function homomorphicMappedTypeInstantiator(c, t, typeVariable, mapper) {
|
|
1229
|
+
const instantiateConstituent = (s) => {
|
|
1230
|
+
if ((s.flags &
|
|
1231
|
+
(3 /* TypeFlags.AnyOrUnknown */ | 117964800 /* TypeFlags.InstantiableNonPrimitive */ | 1048576 /* TypeFlags.Object */ | 268435456 /* TypeFlags.Intersection */)) ===
|
|
1232
|
+
0 ||
|
|
1233
|
+
s === c.wildcardType ||
|
|
1234
|
+
isErrorType(c, s))
|
|
1235
|
+
return s;
|
|
1236
|
+
if (mappedNameTypeNode(t) === undefined) {
|
|
1237
|
+
if (isArrayType(c, s) ||
|
|
1238
|
+
((s.flags & 1 /* TypeFlags.Any */) !== 0 &&
|
|
1239
|
+
findResolutionCycleStartIndex(c, typeVariable, 4 /* TypeSystemPropertyName.ResolvedBaseConstraint */) < 0 &&
|
|
1240
|
+
hasArrayOrTupleConstraint(c, typeVariable)))
|
|
1241
|
+
return instantiateMappedArrayType(c, s, t, prependTypeMapping(typeVariable, s, mapper));
|
|
1242
|
+
if (isTupleType(s))
|
|
1243
|
+
return instantiateMappedTupleType(c, s, t, typeVariable, mapper);
|
|
1244
|
+
if ((s.flags & 268435456 /* TypeFlags.Intersection */) !== 0 && everyArrayOrTupleType(c, typesOf(s))) {
|
|
1245
|
+
const types = [];
|
|
1246
|
+
for (const u of typesOf(s))
|
|
1247
|
+
types.push(instantiateConstituent(u));
|
|
1248
|
+
return getIntersectionType(c, types);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
return instantiateAnonymousType(c, t, prependTypeMapping(typeVariable, s, mapper), undefined);
|
|
1252
|
+
};
|
|
1253
|
+
return instantiateConstituent;
|
|
1254
|
+
}
|
|
1255
|
+
export function instantiateMappedType(c, t, mapper, alias) {
|
|
1256
|
+
// For homomorphic `{ [P in keyof T]: X }`, the mapping depends on what T becomes.
|
|
1257
|
+
const typeVariable = getHomomorphicTypeVariable(c, t);
|
|
1258
|
+
if (typeVariable !== undefined) {
|
|
1259
|
+
const mappedTypeVariable = instantiateType(c, typeVariable, mapper);
|
|
1260
|
+
if (typeVariable.id !== mappedTypeVariable.id)
|
|
1261
|
+
return mapTypeWithAlias(c, getReducedType(c, mappedTypeVariable), homomorphicMappedTypeInstantiator(c, t, typeVariable, mapper), alias);
|
|
1262
|
+
}
|
|
1263
|
+
if (instantiateType(c, getConstraintTypeFromMappedType(c, t), mapper) === c.wildcardType)
|
|
1264
|
+
return c.wildcardType;
|
|
1265
|
+
return instantiateAnonymousType(c, t, mapper, alias);
|
|
1266
|
+
}
|
|
1267
|
+
function hasArrayOrTupleConstraint(c, typeVariable) {
|
|
1268
|
+
const constraint = getConstraintOfTypeParameter(c, typeVariable);
|
|
1269
|
+
return constraint !== undefined && everyType(constraint, (u) => isArrayType(c, u) || isTupleType(u));
|
|
1270
|
+
}
|
|
1271
|
+
function getModifiedReadonlyState(state, modifiers) {
|
|
1272
|
+
if ((modifiers & 1 /* MappedTypeModifiers.IncludeReadonly */) !== 0)
|
|
1273
|
+
return true;
|
|
1274
|
+
if ((modifiers & 2 /* MappedTypeModifiers.ExcludeReadonly */) !== 0)
|
|
1275
|
+
return false;
|
|
1276
|
+
return state;
|
|
1277
|
+
}
|
|
1278
|
+
function instantiateMappedArrayType(c, arrayType, mappedType, mapper) {
|
|
1279
|
+
const elementType = instantiateMappedTypeTemplate(c, mappedType, c.numberType, true, mapper);
|
|
1280
|
+
if (isErrorType(c, elementType))
|
|
1281
|
+
return c.errorType;
|
|
1282
|
+
return createArrayTypeEx(c, elementType, getModifiedReadonlyState(isReadonlyArrayType(c, arrayType), getMappedTypeModifiers(mappedType)));
|
|
1283
|
+
}
|
|
1284
|
+
function instantiateMappedTupleType(c, tupleType, mappedType, typeVariable, mapper) {
|
|
1285
|
+
const target = tupleTargetOf(tupleType);
|
|
1286
|
+
const infos = target.elementInfos;
|
|
1287
|
+
const fixedLength = target.fixedLength;
|
|
1288
|
+
const fixedMapper = fixedLength !== 0 ? prependTypeMapping(typeVariable, tupleType, mapper) : mapper;
|
|
1289
|
+
const modifiers = getMappedTypeModifiers(mappedType);
|
|
1290
|
+
const elementTypes = getElementTypes(c, tupleType);
|
|
1291
|
+
const newElementTypes = [];
|
|
1292
|
+
const newInfos = infos.slice();
|
|
1293
|
+
for (let i = 0; i < elementTypes.length; i++) {
|
|
1294
|
+
const flags = infos[i].flags;
|
|
1295
|
+
let mapped;
|
|
1296
|
+
if (i < fixedLength)
|
|
1297
|
+
mapped = instantiateMappedTypeTemplate(c, mappedType, getStringLiteralType(c, String(i)), (flags & 2 /* ElementFlags.Optional */) !== 0, fixedMapper);
|
|
1298
|
+
else if ((flags & 8 /* ElementFlags.Variadic */) !== 0)
|
|
1299
|
+
mapped = instantiateType(c, mappedType, prependTypeMapping(typeVariable, elementTypes[i], mapper));
|
|
1300
|
+
else
|
|
1301
|
+
mapped =
|
|
1302
|
+
getElementTypeOfArrayType(c, instantiateType(c, mappedType, prependTypeMapping(typeVariable, createArrayTypeEx(c, elementTypes[i], false), mapper))) ?? c.unknownType;
|
|
1303
|
+
if ((modifiers & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0) {
|
|
1304
|
+
if ((flags & 1 /* ElementFlags.Required */) !== 0)
|
|
1305
|
+
newInfos[i] = withFlags(newInfos[i], 2 /* ElementFlags.Optional */);
|
|
1306
|
+
}
|
|
1307
|
+
else if ((modifiers & 8 /* MappedTypeModifiers.ExcludeOptional */) !== 0) {
|
|
1308
|
+
if ((flags & 2 /* ElementFlags.Optional */) !== 0)
|
|
1309
|
+
newInfos[i] = withFlags(newInfos[i], 1 /* ElementFlags.Required */);
|
|
1310
|
+
}
|
|
1311
|
+
newElementTypes.push(mapped);
|
|
1312
|
+
}
|
|
1313
|
+
if (newElementTypes.includes(c.errorType))
|
|
1314
|
+
return c.errorType;
|
|
1315
|
+
return createTupleTypeEx(c, newElementTypes, newInfos, getModifiedReadonlyState(target.readonly, modifiers));
|
|
1316
|
+
}
|
|
1317
|
+
import { TupleElementInfo } from './checker-types.js';
|
|
1318
|
+
import { createDiagnosticForNode } from './checker.js';
|
|
1319
|
+
import { isJSLiteralType, isThisPropertyAccessInConstructor } from './checker-javascript.js';
|
|
1320
|
+
function withFlags(info, flags) {
|
|
1321
|
+
return new TupleElementInfo(flags, info.labeledDeclaration);
|
|
1322
|
+
}
|
|
1323
|
+
function instantiateMappedTypeTemplate(c, t, key, isOptional, mapper) {
|
|
1324
|
+
const templateMapper = appendTypeMapping(mapper, getTypeParameterFromMappedType(c, t), key);
|
|
1325
|
+
const propType = instantiateType(c, getTemplateTypeFromMappedType(c, mappedTypeTarget(t)), templateMapper);
|
|
1326
|
+
const modifiers = getMappedTypeModifiers(t);
|
|
1327
|
+
if (c.strictNullChecks &&
|
|
1328
|
+
(modifiers & 4 /* MappedTypeModifiers.IncludeOptional */) !== 0 &&
|
|
1329
|
+
!maybeTypeOfKind(propType, 4 /* TypeFlags.Undefined */ | 16 /* TypeFlags.Void */))
|
|
1330
|
+
return getOptionalType(c, propType, true);
|
|
1331
|
+
if (c.strictNullChecks && (modifiers & 8 /* MappedTypeModifiers.ExcludeOptional */) !== 0 && isOptional)
|
|
1332
|
+
return filterType(c, propType, (u) => (u.flags & 4 /* TypeFlags.Undefined */) === 0);
|
|
1333
|
+
return propType;
|
|
1334
|
+
}
|
|
1335
|
+
export function instantiateReverseMappedType(c, t, mapper) {
|
|
1336
|
+
return instantiateReverseMappedTypeOfInference(c, t, mapper);
|
|
1337
|
+
}
|
|
1338
|
+
export function resolveReverseMappedTypeMembers(c, t) {
|
|
1339
|
+
resolveReverseMappedTypeMembersOfInference(c, t);
|
|
1340
|
+
}
|
|
1341
|
+
// ---- Conditional types
|
|
1342
|
+
export function getTypeFromConditionalTypeNode(c, node) {
|
|
1343
|
+
if (c.resolvedTypes.get(node.id) === undefined) {
|
|
1344
|
+
const shape = node;
|
|
1345
|
+
const checkType = getTypeFromTypeNode(c, shape.checkType);
|
|
1346
|
+
const alias = getAliasForTypeNode(c, node);
|
|
1347
|
+
const all = getOuterTypeParameters(c, node, true);
|
|
1348
|
+
let outerTypeParameters = all;
|
|
1349
|
+
if (alias === undefined || alias.typeArguments.length === 0) {
|
|
1350
|
+
outerTypeParameters = [];
|
|
1351
|
+
for (const tp of all)
|
|
1352
|
+
if (isTypeParameterPossiblyReferenced(c, tp, node))
|
|
1353
|
+
outerTypeParameters.push(tp);
|
|
1354
|
+
}
|
|
1355
|
+
const root = new ConditionalRoot(node, checkType, getTypeFromTypeNode(c, shape.extendsType), (checkType.flags & 524288 /* TypeFlags.TypeParameter */) !== 0, getInferTypeParameters(c, node), outerTypeParameters, alias);
|
|
1356
|
+
const resolved = getConditionalType(c, root, undefined, false, undefined);
|
|
1357
|
+
c.resolvedTypes.set(node.id, resolved);
|
|
1358
|
+
root.instantiations.set(typeListHash(outerTypeParameters, undefined, 0), outerTypeParameters, undefined, 0, resolved);
|
|
1359
|
+
}
|
|
1360
|
+
return c.resolvedTypes.get(node.id);
|
|
1361
|
+
}
|
|
1362
|
+
function conditionalNode(root) {
|
|
1363
|
+
return root.node;
|
|
1364
|
+
}
|
|
1365
|
+
export function getConditionalTypeInstantiation(c, t, mapper, forConstraint, alias) {
|
|
1366
|
+
const root = t.root;
|
|
1367
|
+
if (root.outerTypeParameters.length === 0)
|
|
1368
|
+
return t;
|
|
1369
|
+
const typeArguments = [];
|
|
1370
|
+
for (const tp of root.outerTypeParameters)
|
|
1371
|
+
typeArguments.push(mapper.map(tp));
|
|
1372
|
+
const tag = forConstraint ? 1 : 0;
|
|
1373
|
+
const hash = typeListHash(typeArguments, alias, tag);
|
|
1374
|
+
let result = root.instantiations.get(hash, typeArguments, alias, tag);
|
|
1375
|
+
if (result === undefined) {
|
|
1376
|
+
const newMapper = newTypeMapper(root.outerTypeParameters, typeArguments);
|
|
1377
|
+
const checkType = root.checkType;
|
|
1378
|
+
const distributionType = root.isDistributive ? getReducedType(c, newMapper.map(checkType)) : undefined;
|
|
1379
|
+
// Distributive conditional types distribute over unions.
|
|
1380
|
+
if (distributionType !== undefined &&
|
|
1381
|
+
checkType !== distributionType &&
|
|
1382
|
+
(distributionType.flags & (134217728 /* TypeFlags.Union */ | 262144 /* TypeFlags.Never */)) !== 0)
|
|
1383
|
+
result = mapTypeWithAlias(c, distributionType, (u) => getConditionalType(c, root, prependTypeMapping(checkType, u, newMapper), forConstraint, undefined), alias);
|
|
1384
|
+
else
|
|
1385
|
+
result = getConditionalType(c, root, newMapper, forConstraint, alias);
|
|
1386
|
+
root.instantiations.set(hash, typeArguments, alias, tag, result);
|
|
1387
|
+
}
|
|
1388
|
+
return result;
|
|
1389
|
+
}
|
|
1390
|
+
function skipTypeParentheses(node) {
|
|
1391
|
+
while (node.kind === 197 /* SyntaxKind.ParenthesizedType */)
|
|
1392
|
+
node = getType(node);
|
|
1393
|
+
return node;
|
|
1394
|
+
}
|
|
1395
|
+
function isSimpleTupleType(node) {
|
|
1396
|
+
if (node.kind !== 190 /* SyntaxKind.TupleType */)
|
|
1397
|
+
return false;
|
|
1398
|
+
const elements = getElements(node).nodes;
|
|
1399
|
+
if (elements.length === 0)
|
|
1400
|
+
return false;
|
|
1401
|
+
for (const e of elements) {
|
|
1402
|
+
if (e.kind === 191 /* SyntaxKind.OptionalType */ || e.kind === 192 /* SyntaxKind.RestType */)
|
|
1403
|
+
return false;
|
|
1404
|
+
if (e.kind === 203 /* SyntaxKind.NamedTupleMember */ &&
|
|
1405
|
+
(getQuestionToken(e) !== undefined || getDotDotDotToken(e) !== undefined))
|
|
1406
|
+
return false;
|
|
1407
|
+
}
|
|
1408
|
+
return true;
|
|
1409
|
+
}
|
|
1410
|
+
function isDeferredType(c, t, checkTuples) {
|
|
1411
|
+
if (isGenericType(c, t))
|
|
1412
|
+
return true;
|
|
1413
|
+
if (checkTuples && isTupleType(t))
|
|
1414
|
+
for (const u of getElementTypes(c, t))
|
|
1415
|
+
if (isGenericType(c, u))
|
|
1416
|
+
return true;
|
|
1417
|
+
return false;
|
|
1418
|
+
}
|
|
1419
|
+
export function getPermissiveInstantiation(c, t) {
|
|
1420
|
+
if ((t.flags & (12713980 /* TypeFlags.Primitive */ | 3 /* TypeFlags.AnyOrUnknown */ | 262144 /* TypeFlags.Never */)) !== 0)
|
|
1421
|
+
return t;
|
|
1422
|
+
const key = cachedTypeKey(8 /* CachedTypeKind.PermissiveInstantiation */, t.id);
|
|
1423
|
+
let result = c.cachedTypesById.get(key);
|
|
1424
|
+
if (result === undefined) {
|
|
1425
|
+
result = instantiateType(c, t, c.permissiveMapper);
|
|
1426
|
+
c.cachedTypesById.set(key, result);
|
|
1427
|
+
}
|
|
1428
|
+
return result;
|
|
1429
|
+
}
|
|
1430
|
+
export function getConditionalType(c, root, mapper, forConstraint, alias) {
|
|
1431
|
+
let result;
|
|
1432
|
+
let extraTypes;
|
|
1433
|
+
let tailCount = 0;
|
|
1434
|
+
// Loops over nested conditional types in the false branch and tail-recursive resolutions.
|
|
1435
|
+
for (;;) {
|
|
1436
|
+
if (tailCount === 1000) {
|
|
1437
|
+
c.error(c.currentNode, 737 /* Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite */);
|
|
1438
|
+
return c.errorType;
|
|
1439
|
+
}
|
|
1440
|
+
const checkType = instantiateType(c, getActualTypeVariable(c, root.checkType), mapper);
|
|
1441
|
+
const extendsType = instantiateType(c, root.extendsType, mapper);
|
|
1442
|
+
if (checkType === c.errorType || extendsType === c.errorType)
|
|
1443
|
+
return c.errorType;
|
|
1444
|
+
if (checkType === c.wildcardType || extendsType === c.wildcardType)
|
|
1445
|
+
return c.wildcardType;
|
|
1446
|
+
const nodes = conditionalNode(root);
|
|
1447
|
+
const checkTypeNode = skipTypeParentheses(nodes.checkType);
|
|
1448
|
+
const extendsTypeNode = skipTypeParentheses(nodes.extendsType);
|
|
1449
|
+
const checkTuples = isSimpleTupleType(checkTypeNode) &&
|
|
1450
|
+
isSimpleTupleType(extendsTypeNode) &&
|
|
1451
|
+
getElements(checkTypeNode).nodes.length === getElements(extendsTypeNode).nodes.length;
|
|
1452
|
+
const checkTypeDeferred = isDeferredType(c, checkType, checkTuples);
|
|
1453
|
+
const falseForEveryInference = root.inferTypeParameters.length !== 0 &&
|
|
1454
|
+
!checkTypeDeferred &&
|
|
1455
|
+
!forConstraint &&
|
|
1456
|
+
isFalseForEveryInference(c, root, checkType, extendsType);
|
|
1457
|
+
let combinedMapper;
|
|
1458
|
+
if (root.inferTypeParameters.length !== 0 && !falseForEveryInference) {
|
|
1459
|
+
const inferenceMapper = inferTypesForConditional(c, root, checkType, extendsType, mapper, checkTypeDeferred);
|
|
1460
|
+
if (inferenceMapper === undefined)
|
|
1461
|
+
return c.errorType;
|
|
1462
|
+
combinedMapper = mapper !== undefined ? combineTypeMappers(c, inferenceMapper, mapper) : inferenceMapper;
|
|
1463
|
+
}
|
|
1464
|
+
const inferredExtendsType = combinedMapper !== undefined ? instantiateType(c, root.extendsType, combinedMapper) : extendsType;
|
|
1465
|
+
if (falseForEveryInference || (!checkTypeDeferred && !isDeferredType(c, inferredExtendsType, checkTuples))) {
|
|
1466
|
+
// Definitely false when even permissive instantiations aren't related.
|
|
1467
|
+
if (falseForEveryInference ||
|
|
1468
|
+
((inferredExtendsType.flags & 3 /* TypeFlags.AnyOrUnknown */) === 0 &&
|
|
1469
|
+
((checkType.flags & 1 /* TypeFlags.Any */) !== 0 ||
|
|
1470
|
+
!isTypeAssignableTo(c, getPermissiveInstantiation(c, checkType), getPermissiveInstantiation(c, inferredExtendsType))))) {
|
|
1471
|
+
if ((checkType.flags & 1 /* TypeFlags.Any */) !== 0 ||
|
|
1472
|
+
(forConstraint &&
|
|
1473
|
+
(inferredExtendsType.flags & 262144 /* TypeFlags.Never */) === 0 &&
|
|
1474
|
+
someType(getPermissiveInstantiation(c, inferredExtendsType), (u) => isTypeAssignableTo(c, u, getPermissiveInstantiation(c, checkType))))) {
|
|
1475
|
+
if (extraTypes === undefined)
|
|
1476
|
+
extraTypes = [];
|
|
1477
|
+
extraTypes.push(instantiateType(c, getTypeFromTypeNode(c, nodes.trueType), combinedMapper ?? mapper));
|
|
1478
|
+
}
|
|
1479
|
+
const falseType = getTypeFromTypeNode(c, nodes.falseType);
|
|
1480
|
+
if ((falseType.flags & 67108864 /* TypeFlags.Conditional */) !== 0) {
|
|
1481
|
+
const newRoot = falseType.root;
|
|
1482
|
+
if (newRoot.node.parent === root.node && (!newRoot.isDistributive || newRoot.checkType === root.checkType)) {
|
|
1483
|
+
root = newRoot;
|
|
1484
|
+
continue;
|
|
1485
|
+
}
|
|
1486
|
+
const tail = getTailRecursionRoot(c, falseType, mapper);
|
|
1487
|
+
if (tail !== undefined) {
|
|
1488
|
+
root = tail.root;
|
|
1489
|
+
mapper = tail.mapper;
|
|
1490
|
+
alias = undefined;
|
|
1491
|
+
if (tail.root.alias !== undefined)
|
|
1492
|
+
tailCount++;
|
|
1493
|
+
continue;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
result = instantiateType(c, falseType, mapper);
|
|
1497
|
+
break;
|
|
1498
|
+
}
|
|
1499
|
+
// Definitely true when restrictive instantiations are related.
|
|
1500
|
+
if ((inferredExtendsType.flags & 3 /* TypeFlags.AnyOrUnknown */) !== 0 ||
|
|
1501
|
+
isTypeAssignableTo(c, getRestrictiveInstantiation(c, checkType), getRestrictiveInstantiation(c, inferredExtendsType))) {
|
|
1502
|
+
const trueType = getTypeFromTypeNode(c, nodes.trueType);
|
|
1503
|
+
const trueMapper = combinedMapper ?? mapper;
|
|
1504
|
+
const tail = getTailRecursionRoot(c, trueType, trueMapper);
|
|
1505
|
+
if (tail !== undefined) {
|
|
1506
|
+
root = tail.root;
|
|
1507
|
+
mapper = tail.mapper;
|
|
1508
|
+
alias = undefined;
|
|
1509
|
+
if (tail.root.alias !== undefined)
|
|
1510
|
+
tailCount++;
|
|
1511
|
+
continue;
|
|
1512
|
+
}
|
|
1513
|
+
result = instantiateType(c, trueType, trueMapper);
|
|
1514
|
+
break;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
// Neither definitely true nor false: defer.
|
|
1518
|
+
const deferred = new ConditionalType(c.nextTypeId(), root, instantiateType(c, root.checkType, mapper), instantiateType(c, root.extendsType, mapper), mapper, combinedMapper);
|
|
1519
|
+
deferred.alias = alias ?? (mapper !== undefined ? instantiateTypeAlias(c, root.alias, mapper) : root.alias);
|
|
1520
|
+
deferred.checking = c;
|
|
1521
|
+
result = deferred;
|
|
1522
|
+
break;
|
|
1523
|
+
}
|
|
1524
|
+
return extraTypes !== undefined ? getUnionType(c, extraTypes.concat([result])) : result;
|
|
1525
|
+
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Whether the extends check of a conditional type with `infer` type parameters is false for every
|
|
1528
|
+
* inference, so that inference can be skipped and the false branch taken as it would be. This is so
|
|
1529
|
+
* when the check and extends types are references to the same class or interface, which are related
|
|
1530
|
+
* by the variances of its type parameters, and a type argument without `infer` type parameters is a
|
|
1531
|
+
* primitive or literal type that doesn't relate to the check type's: relating the type arguments
|
|
1532
|
+
* then fails whatever is inferred for the others. The relation doesn't fall back to comparing
|
|
1533
|
+
* structure: no variance is unmeasurable or unreliable, and no covariant type argument can be
|
|
1534
|
+
* `void` after inference.
|
|
1535
|
+
*/
|
|
1536
|
+
function isFalseForEveryInference(c, root, checkType, extendsType) {
|
|
1537
|
+
if ((checkType.flags & 1048576 /* TypeFlags.Object */) === 0 ||
|
|
1538
|
+
(extendsType.flags & 1048576 /* TypeFlags.Object */) === 0 ||
|
|
1539
|
+
(checkType.objectFlags & 4 /* ObjectFlags.Reference */) === 0 ||
|
|
1540
|
+
(extendsType.objectFlags & 4 /* ObjectFlags.Reference */) === 0)
|
|
1541
|
+
return false;
|
|
1542
|
+
const target = checkType.target;
|
|
1543
|
+
if (target !== extendsType.target)
|
|
1544
|
+
return false;
|
|
1545
|
+
const args = getReferenceArgs(c, root);
|
|
1546
|
+
if (args === null || isMarkerType(c, checkType) || isMarkerType(c, extendsType))
|
|
1547
|
+
return false;
|
|
1548
|
+
const variances = getVariances(c, target);
|
|
1549
|
+
if (variances.length === 0)
|
|
1550
|
+
return false;
|
|
1551
|
+
for (const v of variances)
|
|
1552
|
+
if ((v & 24 /* VarianceFlags.AllowsStructuralFallback */) !== 0)
|
|
1553
|
+
return false;
|
|
1554
|
+
const sourceArgs = getTypeArguments(c, checkType);
|
|
1555
|
+
const targetArgs = getTypeArguments(c, extendsType);
|
|
1556
|
+
let unrelated = false;
|
|
1557
|
+
for (let i = 0; i < args.kinds.length; i++) {
|
|
1558
|
+
const kind = args.kinds[i];
|
|
1559
|
+
const variance = i < variances.length ? variances[i] & 7 /* VarianceFlags.VarianceMask */ : 1 /* VarianceFlags.Covariant */;
|
|
1560
|
+
if (variance === 1 /* VarianceFlags.Covariant */) {
|
|
1561
|
+
if (kind === 3 /* ReferenceArgKind.Other */)
|
|
1562
|
+
return false;
|
|
1563
|
+
if (kind === 0 /* ReferenceArgKind.Fixed */ && (targetArgs[i].flags & 16 /* TypeFlags.Void */) !== 0)
|
|
1564
|
+
return false;
|
|
1565
|
+
// An `infer` type parameter that occurs once infers the check type's type argument, or its
|
|
1566
|
+
// constraint if that isn't assignable to it.
|
|
1567
|
+
if (kind === 1 /* ReferenceArgKind.Infer */) {
|
|
1568
|
+
if (someType(sourceArgs[i], (t) => (t.flags & 16 /* TypeFlags.Void */) !== 0))
|
|
1569
|
+
return false;
|
|
1570
|
+
const constraint = getConstraintOfTypeParameter(c, args.inferTypeParameters[i]);
|
|
1571
|
+
if (constraint !== undefined && (constraint.flags & (16 /* TypeFlags.Void */ | 132644864 /* TypeFlags.Instantiable */)) !== 0)
|
|
1572
|
+
return false;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
if (!unrelated &&
|
|
1576
|
+
kind === 0 /* ReferenceArgKind.Fixed */ &&
|
|
1577
|
+
variance !== 4 /* VarianceFlags.Independent */ &&
|
|
1578
|
+
isPrimitiveOrLiteralTypeArgument(sourceArgs[i]) &&
|
|
1579
|
+
isPrimitiveOrLiteralTypeArgument(targetArgs[i]))
|
|
1580
|
+
unrelated = !isTypeArgumentRelated(c, sourceArgs[i], targetArgs[i], variance);
|
|
1581
|
+
}
|
|
1582
|
+
return unrelated;
|
|
1583
|
+
}
|
|
1584
|
+
/** A primitive or literal type, or a union of them: relating them doesn't relate any structure. */
|
|
1585
|
+
function isPrimitiveOrLiteralTypeArgument(t) {
|
|
1586
|
+
if ((t.flags & 134217728 /* TypeFlags.Union */) === 0)
|
|
1587
|
+
return (t.flags & 536346624 /* TypeFlags.StructuredOrInstantiable */) === 0;
|
|
1588
|
+
for (const u of typesOf(t))
|
|
1589
|
+
if ((u.flags & 536346624 /* TypeFlags.StructuredOrInstantiable */) !== 0)
|
|
1590
|
+
return false;
|
|
1591
|
+
return true;
|
|
1592
|
+
}
|
|
1593
|
+
/** Whether type arguments relate by `variance`, as when relating references to the same type. */
|
|
1594
|
+
function isTypeArgumentRelated(c, s, t, variance) {
|
|
1595
|
+
switch (variance) {
|
|
1596
|
+
case 1 /* VarianceFlags.Covariant */:
|
|
1597
|
+
return isTypeAssignableTo(c, s, t);
|
|
1598
|
+
case 2 /* VarianceFlags.Contravariant */:
|
|
1599
|
+
return isTypeAssignableTo(c, t, s);
|
|
1600
|
+
case 3 /* VarianceFlags.Bivariant */:
|
|
1601
|
+
return isTypeAssignableTo(c, t, s) || isTypeAssignableTo(c, s, t);
|
|
1602
|
+
default:
|
|
1603
|
+
return isTypeAssignableTo(c, s, t) && isTypeAssignableTo(c, t, s);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
function getReferenceArgs(c, root) {
|
|
1607
|
+
if (root.referenceArgs === undefined)
|
|
1608
|
+
root.referenceArgs = computeReferenceArgs(c, root);
|
|
1609
|
+
return root.referenceArgs;
|
|
1610
|
+
}
|
|
1611
|
+
function computeReferenceArgs(c, root) {
|
|
1612
|
+
const declared = root.extendsType;
|
|
1613
|
+
if ((declared.objectFlags & 4 /* ObjectFlags.Reference */) === 0 || declared.alias !== undefined || isTupleType(declared))
|
|
1614
|
+
return null;
|
|
1615
|
+
const target = declared.target;
|
|
1616
|
+
if (target === c.globalArrayType ||
|
|
1617
|
+
target === c.globalReadonlyArrayType ||
|
|
1618
|
+
target.symbol === undefined ||
|
|
1619
|
+
(target.symbol.flags & (32 /* SymbolFlags.Class */ | 64 /* SymbolFlags.Interface */)) === 0)
|
|
1620
|
+
return null;
|
|
1621
|
+
const node = skipTypeParentheses(conditionalNode(root).extendsType);
|
|
1622
|
+
if (node.kind !== 184 /* SyntaxKind.TypeReference */)
|
|
1623
|
+
return null;
|
|
1624
|
+
const argNodes = node.typeArguments?.nodes ?? [];
|
|
1625
|
+
const typeArgs = getTypeArguments(c, declared);
|
|
1626
|
+
if (typeArgs.length !== argNodes.length)
|
|
1627
|
+
return null;
|
|
1628
|
+
const kinds = [];
|
|
1629
|
+
const inferTypeParameters = [];
|
|
1630
|
+
for (let i = 0; i < argNodes.length; i++) {
|
|
1631
|
+
const argNode = argNodes[i];
|
|
1632
|
+
let kind = 0 /* ReferenceArgKind.Fixed */;
|
|
1633
|
+
let inferTypeParameter;
|
|
1634
|
+
for (const tp of root.inferTypeParameters) {
|
|
1635
|
+
const declaration = tp.symbol?.declarations[0];
|
|
1636
|
+
if (declaration === undefined ||
|
|
1637
|
+
isNodeDescendantOf(declaration, argNode) ||
|
|
1638
|
+
containsTypeParameterReference(c, tp, argNode)) {
|
|
1639
|
+
kind = 3 /* ReferenceArgKind.Other */;
|
|
1640
|
+
break;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
if (kind === 3 /* ReferenceArgKind.Other */) {
|
|
1644
|
+
const inner = skipTypeParentheses(argNode);
|
|
1645
|
+
if (inner.kind === 196 /* SyntaxKind.InferType */) {
|
|
1646
|
+
const tp = root.inferTypeParameters.find((t) => t.symbol.declarations[0] === inner.typeParameter);
|
|
1647
|
+
if (tp !== undefined && tp.symbol.declarations.length === 1 && !isReferencedInOtherArg(c, tp, argNodes, i)) {
|
|
1648
|
+
kind = 1 /* ReferenceArgKind.Infer */;
|
|
1649
|
+
inferTypeParameter = tp;
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
else if (isObjectTypeWithObjectInstantiations(typeArgs[i]))
|
|
1653
|
+
kind = 2 /* ReferenceArgKind.Object */;
|
|
1654
|
+
}
|
|
1655
|
+
kinds.push(kind);
|
|
1656
|
+
inferTypeParameters.push(inferTypeParameter);
|
|
1657
|
+
}
|
|
1658
|
+
return new ReferenceArgs(kinds, inferTypeParameters);
|
|
1659
|
+
}
|
|
1660
|
+
function isReferencedInOtherArg(c, tp, argNodes, index) {
|
|
1661
|
+
for (let j = 0; j < argNodes.length; j++)
|
|
1662
|
+
if (j !== index && containsTypeParameterReference(c, tp, argNodes[j]))
|
|
1663
|
+
return true;
|
|
1664
|
+
return false;
|
|
1665
|
+
}
|
|
1666
|
+
/** References other than tuples and anonymous object types other than mapped types instantiate to object types. */
|
|
1667
|
+
function isObjectTypeWithObjectInstantiations(t) {
|
|
1668
|
+
return ((t.flags & 1048576 /* TypeFlags.Object */) !== 0 &&
|
|
1669
|
+
(t.objectFlags & (4 /* ObjectFlags.Reference */ | 16 /* ObjectFlags.Anonymous */)) !== 0 &&
|
|
1670
|
+
(t.objectFlags & (32 /* ObjectFlags.Mapped */ | 1024 /* ObjectFlags.ReverseMapped */)) === 0 &&
|
|
1671
|
+
!isTupleType(t));
|
|
1672
|
+
}
|
|
1673
|
+
class TailRoot {
|
|
1674
|
+
root;
|
|
1675
|
+
mapper;
|
|
1676
|
+
constructor(root, mapper) {
|
|
1677
|
+
this.root = root;
|
|
1678
|
+
this.mapper = mapper;
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
function getTailRecursionRoot(c, newType, newMapper) {
|
|
1682
|
+
if ((newType.flags & 67108864 /* TypeFlags.Conditional */) === 0 || newMapper === undefined)
|
|
1683
|
+
return undefined;
|
|
1684
|
+
const conditional = newType;
|
|
1685
|
+
const newRoot = conditional.root;
|
|
1686
|
+
if (newRoot.outerTypeParameters.length === 0)
|
|
1687
|
+
return undefined;
|
|
1688
|
+
const typeParamMapper = combineTypeMappers(c, conditional.mapper, newMapper);
|
|
1689
|
+
const typeArguments = [];
|
|
1690
|
+
for (const t of newRoot.outerTypeParameters)
|
|
1691
|
+
typeArguments.push(typeParamMapper.map(t));
|
|
1692
|
+
const newRootMapper = newTypeMapper(newRoot.outerTypeParameters, typeArguments);
|
|
1693
|
+
const newCheckType = newRoot.isDistributive ? newRootMapper.map(newRoot.checkType) : undefined;
|
|
1694
|
+
if (newCheckType === undefined ||
|
|
1695
|
+
newCheckType === newRoot.checkType ||
|
|
1696
|
+
(newCheckType.flags & (134217728 /* TypeFlags.Union */ | 262144 /* TypeFlags.Never */)) === 0)
|
|
1697
|
+
return new TailRoot(newRoot, newRootMapper);
|
|
1698
|
+
return undefined;
|
|
1699
|
+
}
|
|
1700
|
+
export function getTrueTypeFromConditionalType(c, t) {
|
|
1701
|
+
const side = sideOf(c, t);
|
|
1702
|
+
if (side.resolvedTrueType === undefined)
|
|
1703
|
+
side.resolvedTrueType = instantiateType(c, getTypeFromTypeNode(c, conditionalNode(t.root).trueType), t.mapper);
|
|
1704
|
+
return side.resolvedTrueType;
|
|
1705
|
+
}
|
|
1706
|
+
export function getFalseTypeFromConditionalType(c, t) {
|
|
1707
|
+
const side = sideOf(c, t);
|
|
1708
|
+
if (side.resolvedFalseType === undefined)
|
|
1709
|
+
side.resolvedFalseType = instantiateType(c, getTypeFromTypeNode(c, conditionalNode(t.root).falseType), t.mapper);
|
|
1710
|
+
return side.resolvedFalseType;
|
|
1711
|
+
}
|
|
1712
|
+
/** `[getTrueTypeFromConditionalType(t), getFalseTypeFromConditionalType(t)]`, created once. */
|
|
1713
|
+
export function getBranchTypesFromConditionalType(c, t) {
|
|
1714
|
+
const side = sideOf(c, t);
|
|
1715
|
+
let types = side.resolvedBranchTypes;
|
|
1716
|
+
if (types === undefined) {
|
|
1717
|
+
types = [getTrueTypeFromConditionalType(c, t), getFalseTypeFromConditionalType(c, t)];
|
|
1718
|
+
side.resolvedBranchTypes = types;
|
|
1719
|
+
}
|
|
1720
|
+
return types;
|
|
1721
|
+
}
|
|
1722
|
+
export function getInferredTrueTypeFromConditionalType(c, t) {
|
|
1723
|
+
const side = sideOf(c, t);
|
|
1724
|
+
if (side.resolvedInferredTrueType === undefined)
|
|
1725
|
+
side.resolvedInferredTrueType =
|
|
1726
|
+
t.combinedMapper !== undefined
|
|
1727
|
+
? instantiateType(c, getTypeFromTypeNode(c, conditionalNode(t.root).trueType), t.combinedMapper)
|
|
1728
|
+
: getTrueTypeFromConditionalType(c, t);
|
|
1729
|
+
return side.resolvedInferredTrueType;
|
|
1730
|
+
}
|
|
1731
|
+
export function getDefaultConstraintOfConditionalType(c, t) {
|
|
1732
|
+
const side = sideOf(c, t);
|
|
1733
|
+
if (side.resolvedDefaultConstraint === undefined) {
|
|
1734
|
+
// The default constraint is the union of the branches, ignoring `never` branches from the check.
|
|
1735
|
+
const trueConstraint = getInferredTrueTypeFromConditionalType(c, t);
|
|
1736
|
+
const falseConstraint = getFalseTypeFromConditionalType(c, t);
|
|
1737
|
+
side.resolvedDefaultConstraint =
|
|
1738
|
+
(trueConstraint.flags & 1 /* TypeFlags.Any */) !== 0
|
|
1739
|
+
? falseConstraint
|
|
1740
|
+
: (falseConstraint.flags & 1 /* TypeFlags.Any */) !== 0
|
|
1741
|
+
? trueConstraint
|
|
1742
|
+
: getUnionType(c, [trueConstraint, falseConstraint]);
|
|
1743
|
+
}
|
|
1744
|
+
return side.resolvedDefaultConstraint;
|
|
1745
|
+
}
|
|
1746
|
+
export function getConstraintOfConditionalType(c, t) {
|
|
1747
|
+
return hasNonCircularBaseConstraint(c, t) ? getConstraintFromConditionalType(c, t) : undefined;
|
|
1748
|
+
}
|
|
1749
|
+
export function getConstraintFromConditionalType(c, t) {
|
|
1750
|
+
return (getConstraintOfDistributiveConditionalType(c, t) ??
|
|
1751
|
+
getDefaultConstraintOfConditionalType(c, t));
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* For `T extends U ? X : Y` with a constrained check type T, the instantiation with T replaced by its
|
|
1755
|
+
* constraint, which distributes over a union constraint and may reduce; `T extends undefined ? never : T`
|
|
1756
|
+
* removes `undefined` from T. Restrictive instantiations have none: their type parameters are `unknown`.
|
|
1757
|
+
*/
|
|
1758
|
+
export function getConstraintOfDistributiveConditionalType(c, t) {
|
|
1759
|
+
const side = sideOf(c, t);
|
|
1760
|
+
if (side.resolvedConstraintOfDistributive === undefined) {
|
|
1761
|
+
side.resolvedConstraintOfDistributive = c.noConstraintType;
|
|
1762
|
+
if (t.root.isDistributive && !isRestrictiveInstance(c, t)) {
|
|
1763
|
+
let constraint = getSimplifiedType(c, t.checkType, false);
|
|
1764
|
+
if (constraint === t.checkType)
|
|
1765
|
+
constraint = getConstraintOfTypeLocal(c, constraint);
|
|
1766
|
+
if (constraint !== undefined && constraint !== t.checkType) {
|
|
1767
|
+
const instantiated = getConditionalTypeInstantiation(c, t, prependTypeMapping(t.root.checkType, constraint, t.mapper), true, undefined);
|
|
1768
|
+
if ((instantiated.flags & 262144 /* TypeFlags.Never */) === 0)
|
|
1769
|
+
side.resolvedConstraintOfDistributive = instantiated;
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
return side.resolvedConstraintOfDistributive !== c.noConstraintType
|
|
1774
|
+
? side.resolvedConstraintOfDistributive
|
|
1775
|
+
: undefined;
|
|
1776
|
+
}
|
|
1777
|
+
/** Whether `source` matches the pattern of `target`, with placeholders related by `compareTypes`. */
|
|
1778
|
+
export function isTypeMatchedByTemplateLiteralType(c, source, target, compareTypes) {
|
|
1779
|
+
const inferences = inferTypesFromTemplateLiteralType(c, source, target);
|
|
1780
|
+
if (inferences === undefined)
|
|
1781
|
+
return false;
|
|
1782
|
+
const compare = compareTypes ?? ((s, t) => isTypeAssignableTo(c, s, t));
|
|
1783
|
+
for (let i = 0; i < inferences.length; i++)
|
|
1784
|
+
if (!isValidTypeForTemplateLiteralPlaceholder(c, inferences[i], target.types[i], compare))
|
|
1785
|
+
return false;
|
|
1786
|
+
return true;
|
|
1787
|
+
}
|
|
1788
|
+
function isValidTypeForTemplateLiteralPlaceholder(c, source, target, compare) {
|
|
1789
|
+
if ((target.flags & 268435456 /* TypeFlags.Intersection */) !== 0) {
|
|
1790
|
+
for (const t of typesOf(target))
|
|
1791
|
+
if (t !== c.emptyTypeLiteralType && !isValidTypeForTemplateLiteralPlaceholder(c, source, t, compare))
|
|
1792
|
+
return false;
|
|
1793
|
+
return true;
|
|
1794
|
+
}
|
|
1795
|
+
if ((target.flags & 32 /* TypeFlags.String */) !== 0 || compare(source, target))
|
|
1796
|
+
return true;
|
|
1797
|
+
if ((source.flags & 1024 /* TypeFlags.StringLiteral */) !== 0) {
|
|
1798
|
+
const value = source.stringValue;
|
|
1799
|
+
return (((target.flags & 64 /* TypeFlags.Number */) !== 0 && isValidNumberString(value, false)) ||
|
|
1800
|
+
((target.flags & 128 /* TypeFlags.BigInt */) !== 0 && isValidBigIntString(value, false)) ||
|
|
1801
|
+
((target.flags & (8192 /* TypeFlags.BooleanLiteral */ | 12 /* TypeFlags.Nullable */)) !== 0 &&
|
|
1802
|
+
value === target.intrinsicName) ||
|
|
1803
|
+
((target.flags & 8388608 /* TypeFlags.StringMapping */) !== 0 && isMemberOfStringMapping(c, source, target)) ||
|
|
1804
|
+
((target.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0 &&
|
|
1805
|
+
isTypeMatchedByTemplateLiteralType(c, source, target, compare)));
|
|
1806
|
+
}
|
|
1807
|
+
if ((source.flags & 4194304 /* TypeFlags.TemplateLiteral */) !== 0) {
|
|
1808
|
+
const texts = source.texts;
|
|
1809
|
+
return (texts.length === 2 &&
|
|
1810
|
+
texts[0] === '' &&
|
|
1811
|
+
texts[1] === '' &&
|
|
1812
|
+
compare(source.types[0], target));
|
|
1813
|
+
}
|
|
1814
|
+
return false;
|
|
1815
|
+
}
|
|
1816
|
+
/** Whether `source` is a member of a string mapping type such as `Uppercase<string>`. */
|
|
1817
|
+
export function isMemberOfStringMapping(c, source, target) {
|
|
1818
|
+
if ((target.flags & 1 /* TypeFlags.Any */) !== 0)
|
|
1819
|
+
return true;
|
|
1820
|
+
if ((target.flags & (32 /* TypeFlags.String */ | 4194304 /* TypeFlags.TemplateLiteral */)) !== 0)
|
|
1821
|
+
return isTypeAssignableTo(c, source, target);
|
|
1822
|
+
if ((target.flags & 8388608 /* TypeFlags.StringMapping */) !== 0) {
|
|
1823
|
+
// Applying the target's mappings must leave the source unchanged, and the source must belong
|
|
1824
|
+
// to the innermost unmapped type.
|
|
1825
|
+
const mappings = [];
|
|
1826
|
+
let inner = target;
|
|
1827
|
+
while ((inner.flags & 8388608 /* TypeFlags.StringMapping */) !== 0) {
|
|
1828
|
+
mappings.push(inner.symbol);
|
|
1829
|
+
inner = inner.target;
|
|
1830
|
+
}
|
|
1831
|
+
let mapped = source;
|
|
1832
|
+
for (let i = mappings.length - 1; i >= 0; i--)
|
|
1833
|
+
mapped = getStringMappingType(c, mappings[i], mapped);
|
|
1834
|
+
return mapped === source && isMemberOfStringMapping(c, source, inner);
|
|
1835
|
+
}
|
|
1836
|
+
return false;
|
|
1837
|
+
}
|
|
1838
|
+
/** The generic mapped type an instantiated mapped type came from, or the type itself. */
|
|
1839
|
+
export function mappedTypeTarget(t) {
|
|
1840
|
+
const target = t.target;
|
|
1841
|
+
return target !== undefined ? target : t;
|
|
1842
|
+
}
|