Skip to content

type check dynamic imports from constant string literal #60593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
scarf005 opened this issue Nov 25, 2024 · 2 comments
Closed
6 tasks done

type check dynamic imports from constant string literal #60593

scarf005 opened this issue Nov 25, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@scarf005
Copy link

πŸ” Search Terms

dynamic import constant string infer type any

βœ… Viability Checklist

⭐ Suggestion

statically analyze type of dynamic import if path is statically analyzable, such that

export {}

const polars = "nodejs-polars" as const
//    ^? const polars: "nodejs-polars"

const pl = await import(polars)
//    ^? const pl: typeof import("file:///node_modules/nodejs-polars/bin/index")

as type of polars is "nodejs-polars", not string, typescript should treat import("nodejs-polars") the same as import(polars).

πŸ“ƒ Motivating Example

denoland/deno#26965

export {}

const polars = "nodejs-polars" as const

const pl = await import("nodejs-polars")
//    ^? const pl: typeof import("file:///node_modules/nodejs-polars/bin/index")

const pl2 = await import(polars)
//    ^? const pl2: typeof import("file:///node_modules/nodejs-polars/bin/index")

Playground Link

πŸ’» Use Cases

  1. What do you want to use this for?

writing type safe javascript module, with version string abstracted away to manage versions easily

const preact = "preact@10.24.3"
const { h, render } = await import(`https://esm.sh/${preact}`)
const { computed, effect, signal } = await import(`https://esm.sh/@preact/signals@1.3.0?deps=${preact}`)

console.log(`preact version is ${preact}`)
  1. What shortcomings exist with current approaches?
  2. What workarounds are you using in the meantime?

due to type check not working in above example, version string has to be duplicated, which might cause bug when naively regex-replacing.

import { h, render }  from "https://esm.sh/preact@10.24.3"
import { computed, effect, signal } from "https://esm.sh/@preact/signals@1.3.0?deps=preact@10.24.3"

console.log("preact version is preact@10.24.3")
@MartinJohns
Copy link
Contributor

Duplicate of #32401.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Nov 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants