microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.2k stars 12.51k forks source link

A new generic type can be used for file path completion(intellisense) #60105

Open xushengfeng opened 1 month ago

xushengfeng commented 1 month ago

πŸ” Search Terms

"path intellisense","path completion","asset path completion"

βœ… Viability Checklist

⭐ Suggestion

Add a tool generics so that some function parameters can enable file path type hints

some assets in ../assets

function getImageUrl(name:T<"../assets">) {
...
return ...
}

getImageUrl("// intellisense

πŸ“ƒ Motivating Example

Enable custom file path completion through generics

πŸ’» Use Cases

  1. What do you want to use this for?

When I used vite static resource introduction, I used a function to wrap it, but the function parameters could not achieve type hints like import.

the function like:

function getImageUrl(name:string) { // lost file intellisense
  return new URL(`./dir/${name}.png`, import.meta.url).href
}
  1. What shortcomings exist with current approaches?

Only import statements support path completion and cannot be customized

  1. What workarounds are you using in the meantime?

i make a vscode extension

MartinJohns commented 1 month ago

Essentially a duplicate of #42054.

xushengfeng commented 1 month ago

This seems to be different from other proposals. In addition to path hints, path mapping is also a functional requirement. Using generics allows you to better handle path mappings, which is particularly useful when working with resources.