Title: Import type over generic argument · Issue #31090 · microsoft/TypeScript · GitHub
Open Graph Title: Import type over generic argument · Issue #31090 · microsoft/TypeScript
X Title: Import type over generic argument · Issue #31090 · microsoft/TypeScript
Description: Search Terms import type, generic, module string type Suggestion To allow for giving a generic argument as an import() type's argument, something like function
Open Graph Description: Search Terms import type, generic, module string type Suggestion To allow for giving a generic argument as an import() type's argument, something like function
X Description: Search Terms import type, generic, module string type Suggestion To allow for giving a generic argument as an import() type's argument, something like function <Mod extends string>(obj: u...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31090
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Import type over generic argument","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the \"Common Feature Requests\" section.\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\nimport type, generic, module string type\r\n\r\n## Suggestion\r\n\r\nTo allow for giving a generic argument as an import() type's argument, something like\r\n`function \u003cMod extends string>(obj: unknown, modPath: Mod): obj is import(Mod);`\r\n\r\n## Use Cases\r\nWould allow generic functions that type inference from any module path string literal to the type exported by that module. This is helpful for encapsulating good module loading practices. For instance, in a large AMD application I work on, we need to manage the application footprint by not loading modules that aren't needed for the current execution path. There are a number of utility functions we could write around handling modules to support good behavior on this front.\r\n\r\n\r\n## Examples\r\nOne of the signatures of AMD's require() could be typed as:\r\n`function require\u003cMod extends string>(path: Mod): import(Mod);`\r\nThis actually does work today if that declaration is put in a .d.ts file and skipLibCheck is set to true. If a string literal is passed, the return type is inferred from the path; if non-literal, the return type is any. In fact, if this worked but disallowed anything other than a module path, it would be super handy for typo avoidance. I'm pretty sure what's happening now is accidental, though, since any more complex use yields a \"string literal expected\" error on the import type. \r\n\r\nLikewise, I'm pretty sure a really useful signature for define() could be done with a mapped type and tuple typed-rest params, something like:\r\n```\r\ntype ImportsOf\u003cPaths extends string[]> = { P in keyof Paths: import(Paths[P]) };\r\nfunction define\u003cPaths extends string[]>(paths: Paths, callback: (...args: ImportsOf\u003cPaths>)=>void);\r\n```\r\n\r\nSpecifically, the function I wanted to write with this was a type-narrowing instanceof check over a module path. Given a module that exports a constructor:\r\n```\r\nfunction isModuleInstance\u003cMod extends string>(obj: unknown, path: Mod): obj is InstanceType\u003cimport(Mod)> {\r\n const modValue = require(path);\r\n return modValue && obj instanceof modValue\r\n}\r\n```\r\nIf the module for a constructor hasn't been loaded, we can assume no object has been constructed by it, so this technique is very handy for keeping down the module load footprint in applications with lots of modules.\r\n\r\nA question for discussion would be the value of an import-type on a non-literal argument, or a non-module argument for that matter. My initial impression is if the import() type can't be resolved, it should evaluate to never and produce an error just like import('some_garbage_string_i_made_up')\". That might require the type checker to be given a stricter bound than \"extends string\", though. If necessary, a user-visible built-in type that means \"a string representing a known module\" seems like it might be useful in lots of contexts.\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [X ] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [X ] This could be implemented without emitting different JS based on the types of the expressions\r\n* [X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [Seems like it to me] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n","author":{"url":"https://github.com/stephenfhunt","@type":"Person","name":"stephenfhunt"},"datePublished":"2019-04-24T15:11:51Z","url":"https://github.com/microsoft/TypeScript/issues/31090"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f6b2f0e5-2db9-1b1c-ce47-7fefca2c6f97 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C38:76F02:2C0D0B2:38553F6:6B01AE25 |
| html-safe-nonce | bfbc519930a32f1aeacebe8d8b69c15ca0dcff5ab8312fb3e68b577d406093d5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzM4Ojc2RjAyOjJDMEQwQjI6Mzg1NTNGNjo2QjAxQUUyNSIsInZpc2l0b3JfaWQiOiI3ODU5MDIwNDc4MTU1OTU5ODQ1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 683f7c556334fa4d360b2ef1356bca7ccc48e73a4950e03176b0ff5bb48e491c |
| hovercard-subject-tag | issue:436754582 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/31090/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3620babafeabab25b1483f4fbeb44f097800acb15ff4db770bf87f70293e51d/microsoft/TypeScript/issues/31090 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3620babafeabab25b1483f4fbeb44f097800acb15ff4db770bf87f70293e51d/microsoft/TypeScript/issues/31090 |
| og:image:alt | Search Terms import type, generic, module string type Suggestion To allow for giving a generic argument as an import() type's argument, something like function |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stephenfhunt |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width