René's URL Explorer Experiment


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 (obj: unknown, modPath: Mod): obj is import(Mod); Use Cas...

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 (obj: unknown, mo...

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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f6b2f0e5-2db9-1b1c-ce47-7fefca2c6f97
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9C38:76F02:2C0D0B2:38553F6:6B01AE25
html-safe-noncebfbc519930a32f1aeacebe8d8b69c15ca0dcff5ab8312fb3e68b577d406093d5
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzM4Ojc2RjAyOjJDMEQwQjI6Mzg1NTNGNjo2QjAxQUUyNSIsInZpc2l0b3JfaWQiOiI3ODU5MDIwNDc4MTU1OTU5ODQ1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac683f7c556334fa4d360b2ef1356bca7ccc48e73a4950e03176b0ff5bb48e491c
hovercard-subject-tagissue:436754582
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/31090/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f3620babafeabab25b1483f4fbeb44f097800acb15ff4db770bf87f70293e51d/microsoft/TypeScript/issues/31090
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f3620babafeabab25b1483f4fbeb44f097800acb15ff4db770bf87f70293e51d/microsoft/TypeScript/issues/31090
og:image:altSearch Terms import type, generic, module string type Suggestion To allow for giving a generic argument as an import() type's argument, something like function (obj: unknown, mo...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamestephenfhunt
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/31090#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F31090
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F31090
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/31090
Reloadhttps://github.com/microsoft/TypeScript/issues/31090
Reloadhttps://github.com/microsoft/TypeScript/issues/31090
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/31090
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/31090
Import type over generic argumenthttps://github.com/microsoft/TypeScript/issues/31090#top
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/stephenfhunt
stephenfhunthttps://github.com/stephenfhunt
on Apr 24, 2019https://github.com/microsoft/TypeScript/issues/31090#issue-436754582
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Apr 24, 2019https://github.com/microsoft/TypeScript/issues/31090#event-2297808400
https://github.com/Raynos
Jake Verbaten (Raynos)https://github.com/Raynos
on Jun 20, 2019https://github.com/microsoft/TypeScript/issues/31090#issuecomment-503957273
https://github.com/njakob
Nicolas Jakob (njakob)https://github.com/njakob
on Mar 12, 2020https://github.com/microsoft/TypeScript/issues/31090#issuecomment-598403081
https://github.com/ExE-Boss
ExE Boss (ExE-Boss)https://github.com/ExE-Boss
on Jul 15, 2020https://github.com/microsoft/TypeScript/issues/31090#issuecomment-658806715
https://github.com/thetutlage
Harminder Virk (thetutlage)https://github.com/thetutlage
on Aug 21, 2020https://github.com/microsoft/TypeScript/issues/31090#issuecomment-678386730
https://github.com/JanVoracek
Jan Voráček (JanVoracek)https://github.com/JanVoracek
on May 24, 2021https://github.com/microsoft/TypeScript/issues/31090#issuecomment-847104158
https://github.com/avidcoder123
Umar Mohammad (avidcoder123)https://github.com/avidcoder123
on Jun 12, 2021https://github.com/microsoft/TypeScript/issues/31090#issuecomment-860081047
https://github.com/Peeja
Petra Jaros (Peeja)https://github.com/Peeja
on Sep 2, 2021https://github.com/microsoft/TypeScript/issues/31090#issuecomment-911819996
https://github.com/kommander
Sebastian (kommander)https://github.com/kommander
on Oct 8, 2022https://github.com/microsoft/TypeScript/issues/31090#issuecomment-1272413492
https://github.com/pi0
Pooya Parsa (pi0)https://github.com/pi0
on Nov 1, 2024https://github.com/microsoft/TypeScript/issues/31090#issuecomment-2451695724
jitihttps://github.com/unjs/jiti
https://github.com/louwers
Bart Louwers (louwers)https://github.com/louwers
on Apr 9, 2025https://github.com/microsoft/TypeScript/issues/31090#issuecomment-2789040833
https://github.com/jkomyno
Alberto Schiabel (jkomyno)https://github.com/jkomyno
on Jun 24, 2025https://github.com/microsoft/TypeScript/issues/31090#issuecomment-3001769736
https://github.com/exoRift
Arthur Neuman (exoRift)https://github.com/exoRift
on Mar 16, 2026https://github.com/microsoft/TypeScript/issues/31090#issuecomment-4069332050
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/31090
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/31090
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.